#!/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. # #
# # ## Exercise 12 # # Define a list `l`. Write code that prints "This list is empty" if the list is empty (has no elements), and "This list has N elements" (where N is the number of elements) otherwise. Make sure your code works even if you change `l`. # In[ ]: # ## Exercise 13 # # Define a **positive floating point** number `x`. # # Write code that computes and prints the **rounded** version of that `x`. Make sure it works for all values of `x`. # # Example: # - `x = 1.2` --> 1 # - `x = 12.6` --> 13 # - `x = 0.5` --> 1 # # Hint: You can **round down** a number by converting it to an integer. Could that be useful here? # In[ ]: # ## Exercise 14 # Different **ages** correspond to different **school** in Ethiopia. This shows these categories: # # 0 - 5 No School # # 6 - 13 Elementary School # # 14 - 18 High School # # 19 - 22 University # # 23 - 100 No School # # Write a variable for Daniel that stores his age (an integer between 0 and 100). # # Now **using this variable**, how would you write a **conditional** to check if Daniel is in **elementary** school? If this is `True`, **print** the string `"Elementary School"`. # In[ ]: # Write code here to verify if Daniel is in "Elementary School" using conditionals (`if`, `elif`, `else`) # Similar to above, how would you write a **conditional** to check if Daniel is in the `"No School"` category? Make sure to verify both cases (`"Elementary School"` and `"No School"`) and if the age is in those ranges, **print** the string `"No School"` and `"Elementary School"` respectively. # In[ ]: # Write code here to verify if Daniel is in "Elementary School" or "No School" using conditionals (`if`, `elif`, `else`) # Write a **conditional** that prints `"No School"` if the person is not in school or `"School"` if the person is in school. # In[ ]: # Now using the code pieces you just wrote and your knowledge of **conditionals**, write a **conditional** that prints the category of school they are in (string). # Test it for different ages to make sure that it works as intended. # In[ ]: # ## Exercise 15 # The sign of a number is +1 if the number is positive, -1 if the number is negative, and 0 if it is 0. # # Define a variable `n1`, and write code that computes and prints its sign # In[ ]: # Define another variable `n2`, and write code that computes and prints the absolute value of that number. # In[ ]: # Using the code from above, write code that takes two variables `n1` and `n2` and computes and prints the number `n2` but with the sign of `n1`. # # Example: # - `n1 = 16` and `n2 = -2` --> 2 # - `n1 = 16` and `n2 = -14` --> 14 # - `n1 = -16` and `n2 = -2` --> -2 # - `n1 = -16` and `n2 = 2` --> -2 # - `n1 = 0` and `n2 = 2` --> 0 # In[ ]: