Difference between revisions of "Hello World!"

From Sustainability Methods
Line 99: Line 99:
  
 
Output:
 
Output:
  <span style="color:blue;">Input  In [1]</span>
+
<span style="color:red;">Input  In [1]</span>
  <span style="color:yellow;">print(My name is John)</span>
+
 
                                    ^
+
<span style="color:blue;">print(My name is John)</span>
  <span style="color:red;"></span> SyntaxError: invalid syntax</span>
+
            ^
 +
<span style="color:green;">SyntaxError: invalid syntax</span>

Revision as of 13:54, 13 September 2024

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. To print any kind of text, you have to put the text in quotation marks " " or ' ' . 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.

Comments

As you have seen above, comments are used in programs to explain code. This helps other people to understand your code and reminds yourself of what you coded in the past. As you've seen already, in Python, a hash (#) is used to begin comments. Python disregards comments when we execute the code cell.

#I am writing a comment. Python is great and I want to learn more

It is also possible to write a multiline comment. If we want to write such a comment, we need to enclose the text with three quotation marks on each side.

'''
This is a multiline comment.
This can be useful, for example, if you are writing a larger program
and want to record some introductory information or assumptions,
or to "exclude" several lines of code so that they are not executed.
'''
print('Imagine a larger executable code portion here')

The output will be:

Imagine a larger executable code portion here

Note: If a code cell doesn't produce real output but contains a multiline comment, this comment will be displayed as output.

Execution Order

The following section is probably clear to most of you, as we also read from top to bottom. In a Python program, the Python interpreter processes the code from top to bottom. Let's look at the following small example:

print('Hello World') # This code will run first
print('Hello Friends') # This will be second
print('We love coding') # This will be last

The output will be:

Hello World
Hello Friends
We love coding

Basic Arithmetic with Python

With the `print()` function, you can also produce aritmetic calculations; however, note that the numbers and operators are NOT inside quotation marks. Check the following examples:

#Addition
print(20 + 5)

Output:

25
#Multiplication
print(72 / 9)

Output:

72

In this entry you can learn more about how to perform simple and complex mathematical operations.

Error messages

Often we write code that does not work as intended and produces an error message. This is completeley normal and part of the process. For example, we have created a code cell that should generate the following output: `My name is John`. Unfortunately it produces an error message.

# Non-functioning code
print(My name is John)

Output: Input In [1]

print(My name is John)

            ^

SyntaxError: invalid syntax