Version 1.1. Prepared by Makzan. Updated at 2021 February.
Welcome to level 1 of Python. In this notebook, we will take a look at storing results in variables.
There are 3 examples in the following code snippets, they are 3 keys 🔑 key_result_1
, 🔑 key_result_2
, 🔑 key_result_3
. Please try to complete the notebooks and use the key_results
in the 🤖 boss level.
Variables are used to store calculated result and temporary result.
key_result_1 = "Welcome to CM540"
Now we can print the variable out
print(key_result_1)
There are several common types in Python. They are int
, float
, str
, boolean
, None
, list
, dict
.
variable = 123
type(variable)
variable = 12.3
type(variable)
variable = "Hello"
type(variable)
str
variable = True
type(variable)
bool
variable = None
type(variable)
NoneType
variable = [1,2,3,4]
type(variable)
list
variable = {'key': 'value'}
type(variable)
dict
Here is another example of storing the calculated result of a+b
into c
.
# Defining variables
a = 1
b = 2
c = a + b
print(c)
3
Given a=1900
and b=90
. What is a+b
?
Please replace None
in the following code snippet into a+b
.
a=1900
b=90
key_result_2 = None
print(key_result_2)
None
Expected result |
---|
1990 |
key_result_2 = a+b
current_version = 3.9
👾 You will get an error of can only concatenate str not float to str in the followling line. No worry, we will fix it.
# DON'T COPY. Not good code example.
print("Hello" + "Python" + current_version)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-21-0a9a1ee7f842> in <module> 1 # DON'T COPY. Not good code example. 2 ----> 3 print("Hello" + "Python" + current_version) TypeError: can only concatenate str (not "float") to str
# DON'T COPY. Not good code example.
print("Hello" + "Python" + str(current_version))
HelloPython3.9
Wait. Where are the spaces?
print(f"Hello Python {current_version}")
Hello Python 3.9
Now please store the "Hello Python 3.9" into key_result_3
.
key_result_3 = None
print(key_result_3)
None
Expected result |
---|
Hello Python 3.9 |
key_result_3 = f"Hello Python {current_version}"
From the examples above, you should already computed the key results. Let’s use the key results here.
print(key_result_1)
Welcome to CM540
print(key_result_2)
None
print(key_result_3)
None
Optional: We can store the 3 results into a list:
results = [key_result_1, key_result_2, key_result_3]
We can inspect the list values by typing the variable name in a cell. That works because Jupyter notebook display the result of last line of each cell if it is not a variable assignment.
results
['Welcome to CM540', None, None]
Once you get the key results correct, you will be able to run the following final_result
and get the expected result.
final_result = f"{key_result_1}! Python was first released in {key_result_2}. {key_result_3}"
print(final_result)
Welcome to CM540! Python was first released in None. None
Expected Result |
---|
Welcome to CM540! Python was first released in 1990. Hello Python 3.9 |
In this notebook, we leanred to store result in variable.
Let’s move to the next level: Collection.