Basics Python#

Note

If you have not yet set up Python on your computer, you can execute this tutorial in your browser via Google Colab. Click on the rocket in the top right corner and launch “Colab”. If that does not work download the .ipynb file and import it in Google Colab

Basics#

Comments are anything that comes after the “#” symbol

a = 1  # assign integer 1 to variable a
b = "hello"  # assign string "hello" to variable b

We can print variables to see their values:

# how to we see our variables?
print(a)
print(b)
1
hello

All variables are objects. Every object has a type. To find out what type your variables are

print(type(a))
print(type(b))
<class 'int'>
<class 'str'>

Objects can have methods:

b.capitalize()
'Hello'

Math#

Basic arithmetic and boolean logic:

# addition / subtraction
1 + 1 - 5
-3
# multiplication
5 * 10
50
# division
1 / 2
0.5
# exponentiation
2**4
16

Comparisons#

We can compare objects using comparison operators, and we’ll get back a boolean (i.e. True/False) result:

2 < 3
True
"energy" == "power"
False

Booleans#

We also have so-called “boolean operators” or “logical operators” which also evaluate to either True or False:

True and True
True
True or not False
True

Conditionals#

Conditionals allow a program to make decisions. They dictate the flow of execution based on whether certain conditions are met.

Note

In Python, indentation is mandatory and blocks of code are closed by the indentation level.

x = 100
if x > 0:
    print("Positive Number")
elif x < 0:
    print("Negative Number")
else:
    print("Zero!")
Positive Number

Loops#

Loops tell a program to perform repetitive tasks. They govern the flow of execution by repeatedly processing a block of code, often until a certain condition is reached.

Note

In Python, we always count from 0!

for carrier in ["electricity", "hydrogen", "methane"]:
    print(carrier, len(carrier))
electricity 11
hydrogen 8
methane 7

Lists#

l = ["electricity", "hydrogen", "methane"]

Accessing items from a list:

l[0] # first
'electricity'
l[-1] # last
'methane'
l[:2] # first two
['electricity', 'hydrogen']
l[::2] # every other
['electricity', 'methane']

Dictionaries#

This is another useful data structure. It maps keys to values.

d = {
    "name": "Reuter West",
    "capacity": 564,
    "fuel": "hard coal",
}
# access a value
d["capacity"]
564
# test for the presence of a key
"fuel" in d
True
# add a new key
d["technology"] = "CHP"
d
{'name': 'Reuter West',
 'capacity': 564,
 'fuel': 'hard coal',
 'technology': 'CHP'}

Now we have the building blocks we need to do basic programming in Python.

Exercises#

Task 1: What is 5 to the power of 5?

Hide code cell content

5**5
3125

Task 2: Create a list with the names of every planet in the solar system (in order). Have Python tell you how many planets there are in the list.

Hide code cell content

planets = [
    "Mercury",
    "Venus",
    "Earth",
    "Mars",
    "Jupyter",
    "Saturn",
    "Uranus",
    "Neptune",
]

Task 3: Create a dictionary that contains the main facts about the following power plant in Berlin. Use this dictionary to access the main fuel type.

Hide code cell content

rw = {
    "Country": "Germany",
    "Electricity Capacity": 564,
    "Heat Capacity": 878,
    "Technology": "Combined heat and power (CHP)",
    "Main Fuel": "Hard coal",
    "Vattenfall ownership share": "100%",
    "Status": "In Operation",
}

Hide code cell content

rw["Main Fuel"]
'Hard coal'