Exercise A: basic python#


A.1: Jupyter#

a = 0
# Run this cell using 'Shift'+'Enter

b = 0 # this is a markdown cell, change it to code (‘Esc’ then ‘y’) and execute (‘Ctrl’+’Enter’)

# Insert a cell below this one ('Esc' then 'b'), 
# write a=1 and execute with 'Shift'+'Enter' (which will run and move to next) 
# Delete this cell ('Esc' then 'd' then 'd')
### Change this code cell to markdown ('Esc' then 'm') and execute with 'Shift'+'Enter'

A.2 Variables#

# Create variable called var_a with the value 2.0
# Now change the value of the variable var_a to "my_string"
# check the type of the variable var_a by using the type() function

A.3 Lists#

# Create a list called LA with three elements: 1.0, "txt" and 3 
# Create a variable LB and set it equal to LA
# Change the first element of LB to "b"
# Print out the first element of LA. Did it change? Why? 
# Append a new element None to the list LA
# Print the list LB
# Create a new list LC, set it to the sum of LA and LB and print the result

A.4 Tuples#

# Create a tuple T with two elements "1" and 2.0
# Create two variables t1, t2 and set them to the first and second element of T
# Change the first element to 1.0. What happens? Why?

A.5 Dictionaries#

# Define an empty dictionary D with dict() 
# add a key-value pair using D[key] = value, with your initials and 
# full name as key and value
# add a another key-value pair with initials and name of a colleague
# Print the value of the first item using square brackets []
# Define the same dictionary an alternative way using D={k1:v1, k2:v2}
# print all the keys using the keys() method on the D object, D.___()
# print all the items with with items()

A.6 Control structures#

v1 = 4.0
v2 = 5.0
# write the code that will print the statement "great" if v2 is greater than v1
# write the code that will do the same as above but also has an else statement "not so great"
L = ["Hello", "World"]
# write a for loop that loops through the elements of L and prints them