Hello World!

From Sustainability Methods
Revision as of 13:24, 13 September 2024 by Gustavo (talk | contribs) (→‎Hello World)

Your first line of code

When learning a new programming language, normally we start with a simple “hello world” example. This one line of code will ensure that the environment is set up correctly.

# This is an executable code cell
print(’hello, world!”)

After executing the line above, you should see the Python prints hello, world! Yay!

Breaking down the code cell above, we divide it into two parts: 1. Code cell with a comment:

# This is an executable code cell

The # at the beginning of a line in a code cell starts a comment in Python. Comments can contain arbitrary text and are often used to explain parts of the code in plain language. When a line that consists of a comment is executed, it doesn't produce an error or output.

2. Code cell with actual code:

print(’hello, world!”)

We use the Python print() function with the argument 'Hello World'. This function displays the text inside the parentheses (= the argument) below the code cell. It's a rather unexciting function, but it's our first "real" Python code that produces output.


As you may have noticed, the code cell with the comment does not produce any output, but the second one does. So, we can draw three conclusions:

1. Not every code cell produces an output. 2. If a cell does produce an output, it appears below the cell. 3. Both cells seem to contain valid Python code since we didn't see any error messages.


Note: the “print ()” above is a function, and you passed the argument ‘hello, world!’ inside the print function to tell Python what to print. You just learned new keywords: function ("print()", argument("hello, world"), and statement (the whole line).

Comments

Besides writing code, it is always recommended to put a comment on your line of code. Not only that it helps other people to understand your code better, but more importantly it acts as a reminder for you of what you have written! Trust me, even experienced programmers will forget their lines of code after a few weeks.

To write comments in Python, use the hashtag symbol # before writing your comment. When you run your code, Python will ignore everything past the # on that particular line.

#Practice writing a comment
print (’hello, Eren’) #this prints my name
#print (’asdf’)