#!/usr/bin/env python # coding: utf-8 # # Exercises for day 2a # # You will be working on a series of exercises for the material covered in lecture 2a. # # We covered: # - Variables # - Variable reassignment # - Printing # - Comments # # Remember, programming variables are NOT mathematical variables! # ## Exercise 1 # # We will start with simple variable exercises. In order to see the value, make sure to put the variable at the end # # Assign the following values to `x`, one per cell: # - `523798` # - `True` # - `"this is a string"` # And make sure it's visible in Jupyter! # In[ ]: # In[ ]: # In[ ]: # # Exercise 2 # # We will now focus on variable reassignment. For each of the following code blocks, guess what the output will be. Then, check your answer in Jupyter. # # 1. Code block 1 # ```python # x = 7 # x = 3 # x # ``` # # 2. Code block 2 # ```python # x = 7 # y = x # y # ``` # # 3. Code block 3 # ```python # x = 7 # y = x # x # ``` # In[ ]: # In[ ]: # In[ ]: # # Exercise 3 # # As with exercise 2, for each of the following code blocks, guess what the output will be. Write your guesses as comments in the code cells below! Then, check your answer in Jupyter. # # 1. Code block 1 # ```python # x = 3 # y = x # x = 4 # y # ``` # # 2. Code block 2 # ```python # x = 3 # y = x # x = 4 # x # ``` # In[ ]: # My guess for y: # In[ ]: # My guess for x: # # Exercise 4 # # As with exercises 2 and 3, for each of the following code blocks, guess what the output will be. Write your guesses as comments in the code cells below! Then, check your answer in Jupyter. # # 1. Code block 1 # ```python # x = "hello" # y = " world" # x + y # ``` # # 2. Code block 2 # ```python # x = "hello" # y = " world" # z = x + y # x = "bye" # z # ``` # # 3. Code block 3 # ```python # x = True # y = 3 # z = x + y # z # ``` # In[ ]: # In[ ]: # In[ ]: # ## Exercise 5 # # What happens when you try to do a computation with a variable which you have not yet given a value? # Run the following code: # # ```python # a = 5 # x = a + b # x # ``` # In[ ]: # What do you need to add to the code so that it does not give an error? # In[ ]: # ## Exercise 6 # Given this code: # ``` # x = 12 # y = 5 # z = y # y = 12 # x = 1 + z # z = x + y + z # ``` # What is the value of x, y and z? Write your guesses as comments in the code cells below! # # Then verify your solution by running the code above and adding a print statement for each of the variables at the end so that you can see the values of the variables. # In[ ]: # ## Exercise 7: Variables # ### 7.1 # Define a variable ```x```, that has a value of ```5```. # In[ ]: # ### 7.2 # Define a variable ```y```, that has a value of ```x```. # In[ ]: # ### 7.3 # What is the value of ```y```? # In[ ]: # ### 7.4 # Change ```x``` to ```4```. What is the value of ```y```? # In[ ]: # ### 7.5 # Define a variable ```z```, that has a value of the sum of ```x``` and ```y``` # In[ ]: # ### 7.6 # Assign ```x``` another value, ```12```. What are the values of ```y``` and ```z```? # In[ ]: # ### 7.7 # Define a variable ```x``` and assign it the value of ```False```, **but without using the keyword `False`**. # # Hint: You can do it using one special operator. # In[ ]: # ## Exercise 8: Tricky variable name # ### 8.1 # Given the following lines of code, what is the value of the variable ```seven```? # ```python # seven = 8 # ``` # In[ ]: # ### 8.2 # Given the following lines of code, are the variables ```shiro``` and ```Shiro``` equivalent? # In[ ]: shiro = 10 Shiro = 12 # How can you look at the values of these variables? Use the `print` function to print the values of both! # In[ ]: # ## Exercise 9 # # Look at the following code # ```python # x = 99 # x /= 10 # x *= 50 # y = x # y += 5 # x -= y # y %= x # # ``` # What is ```x``` and ```y```? Try to do it in your head (or on paper) first and write your guesses as comments in the cell below. Only then try running it with code (use print statements to print the values of `x` and `y`). # In[ ]: # ## Exercise 10: Maths and Increment operations # ### 10.1 # Define a variable ```x``` that has the value ```10``` times ```20```. # # In[ ]: # ### 10.2 # Now increment `x` by 3. # In[ ]: # Can you increment `x` by 3 in a different way (using a different operator)? # In[ ]: # ### 10.3 # Find the remainder of ```78``` divided by ```14``` using one operator from lecture. For example - # # $16 = 5 * 3 + 1$ (1 is the remainder). # In[ ]: # ### 10.4 # Given ```x```, make ```x``` 10 times smaller using one operation. Return a value that has the type ```int```. # In[ ]: # ### 10.5 # Write one line of code each that modifies the variable containing `"Shiro"` so that it contains the new value # # a. ```"ShiroShiroShiro"``` # # b. ```"Shiro3"``` # In[ ]: shiro = "Shiro" # Add one line of code here that modifies shiro # In[ ]: shiro = "Shiro" # Add one line of code here that modifies shiro # ## Exercise 11: Commenting code # # The following code computes the area of the shape below. # # ![](shape.png) # # However, it is not very easy to understand what happens in each line. Add a comment at the end of each line explaining what is going on! # In[ ]: a = 3 s = 3**2 d = 0.5*3.14*(a/2)**2 t = 3**0.5 / 4 * a**2 aa = s + d + t aa # Adding comments to complicated code is generally good practice, because it helps other people (and also yourself if you read your code in the future) to understand your code more easily. # # One other thing that helps make code easier to understand is to use variable names that represent what is stored in the variable. For example, instead of `aa`, you could use `total_area`. Copy the code from above and rename the variables that are used in the code to make it easier to understand! # # Conditionals # # Conditionals refers to `if`, `elif`, `else` statements, which the next few exercises are about. # #