Difference between revisions of "Hello World!"

From Sustainability Methods
 
(19 intermediate revisions by the same user not shown)
Line 44: Line 44:
 
'''
 
'''
 
print('Imagine a larger executable code portion here')
 
print('Imagine a larger executable code portion here')
 +
</syntaxhighlight>
 +
The output will be:
 +
<syntaxhighlight lang="text" line>
 +
Imagine a larger executable code portion here
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
'''Note:''' If a code cell doesn't produce real output but contains a multiline comment, this comment will be displayed as output.
 
'''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:
 +
 +
<syntaxhighlight lang="Python" line>
 +
print('Hello World') # This code will run first
 +
print('Hello Friends') # This will be second
 +
print('We love coding') # This will be last
 +
</syntaxhighlight>
 +
 +
The output will be:
 +
<syntaxhighlight lang="text" line>
 +
Hello World
 +
Hello Friends
 +
We love coding
 +
</syntaxhighlight>
 +
 +
==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:
 +
 +
<syntaxhighlight lang="Python" line>
 +
#Addition
 +
print(20 + 5)
 +
</syntaxhighlight>
 +
Output:
 +
<syntaxhighlight lang="text" line>
 +
25
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang="Python" line>
 +
#Multiplication
 +
print(72 / 9)
 +
</syntaxhighlight>
 +
Output:
 +
<syntaxhighlight lang="text" line>
 +
72
 +
</syntaxhighlight>
 +
 +
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.
 +
 +
<syntaxhighlight lang="Python" line>
 +
# Non-functioning code
 +
print(My name is John)
 +
</syntaxhighlight>
 +
 +
Output:
 +
 +
  <span style="color:blue;">Input  In [1]</span>
 +
  <span style="color:orange;">print(My name is John)</span>
 +
        ^
 +
  <span style="color:red;">SyntaxError: invalid syntax</span>
 +
 +
"SyntaxError: invalid syntax" means that the code in the cell does not adhere to the rules of the Python programming language and, consequently, cannot be understood by the computer. To fix the code, we can insert quotation marks.
 +
 +
<syntaxhighlight lang="Python" line>
 +
# Non-functioning code
 +
print("My name is John")
 +
</syntaxhighlight>
 +
 +
Output:
 +
<syntaxhighlight lang="text" line>
 +
# Fixed code
 +
print('My name is John')
 +
</syntaxhighlight>
 +
 +
Most of the time, you can fix errors using Python's error messages. If that doesn't work, use a search engine and search for the error message. Especially on Stack Overflow (https://stackoverflow.com/) the solutions for many common error messages are described. Also, you can ask your own questions on Stackoverflow if your problem is not there yet. Another option is to get a large language model (e.g. ChatGPT) to explain and fix the error or to generate the code in the first place. For more in-depth information how to handle error messages and how to use large language models visit.
 +
 +
==References==
 +
To be added soon
 +
 +
The author of this entry is Wanja Tolksdorf
 +
 +
[[Category: Python]] [[Category: Python Basics]]

Latest revision as of 14:01, 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

"SyntaxError: invalid syntax" means that the code in the cell does not adhere to the rules of the Python programming language and, consequently, cannot be understood by the computer. To fix the code, we can insert quotation marks.

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

Output:

# Fixed code
print('My name is John')

Most of the time, you can fix errors using Python's error messages. If that doesn't work, use a search engine and search for the error message. Especially on Stack Overflow (https://stackoverflow.com/) the solutions for many common error messages are described. Also, you can ask your own questions on Stackoverflow if your problem is not there yet. Another option is to get a large language model (e.g. ChatGPT) to explain and fix the error or to generate the code in the first place. For more in-depth information how to handle error messages and how to use large language models visit.

References

To be added soon

The author of this entry is Wanja Tolksdorf