#!/usr/bin/env python # coding: utf-8 # # Lecture 4, Part 2 Exercises # # # Exercises on variable scope. # # #### 1) There are two kinds of variables in a Python program depending on where they are defined and how long they exist. What are the kinds of variables in the Python program? # # # 1. What are variables defined outside of a function called? **answer here** # 2. What are variables that only exist during the execution of a function called? **answer here** # # #### 2) What is the outcome of running the following? Try to think of before running the cell. # # In[ ]: # 2A # Predict what will be printed, then run this cell to see the actual result. def reverse(a, b): a, b = b, a return a, b x, y = 246, 802 reverse(x, y) # Now consider this similar function. Try to think of what will be printed before running the cell. # In[ ]: # 2B # Predict what will be printed, then run this cell to see the actual result. def incorrect_reverse(a, b): a = b b = a return a, b x, y = 246, 802 incorrect_reverse(x, y) # Please explain the difference between the two. # In[ ]: # YOUR ANSWER HERE # # #### 3) What is the outcome of running the code below? Try to think of the answer before running the code. # # In[ ]: # Think of the answer, then run this cell to see the actual result. def change_value(x, y): a = 100 b = 200 return x, y a = 10 b = 20 x, y = change_value(a, b) print(x, y) # #### 3) What about this code? Try to think of the answer before running the code. # In[ ]: # Think of the answer, then run this cell to see the actual result. def change_value(x, y): x = 100 y = 200 a = 10 b = 20 change_value(a, b) print(a, b) # #### 4) One of the three print statements from the code below will not run because the code has an error. # # ```python # a = 2 # def my_func(a): # z = a * 5 # return a, z # print(my_func(3)) # print(a) # print(z) # ``` # # Figure out which one it is and explain the error without running the code. # * *DOUBLE CLICK HERE TO WRITE YOUR EXPLANATION* # #### 5) The `global` keyword is used to access and change the values of global variables inside functions. Taking note of that try to predict the outcome of the following code. # In[ ]: var = 0 def MyFunc1(): var = 10 def MyFunc2(): global var var = 3.14 MyFunc1() print(var) MyFunc2() print(var) # ## Challenge Problems # # ### Question 1 # # Now let's work on some fun problems. Write a code after reading the instruction carefully. The hints will help ease the question. # # The code below is incomplete. You should complete it so that it performs the tasks mentioned below. # # Implement given functions: deposit(), withdrawal(), and bank() so that your program works like an ATM. # # Your program should ask user What do you want to do? deposit(d) or withdrawal(w) or balance check(c)? first and then: # # If user input is empty string, '', then quit this function using return. # If user input is 'w', then ask the amount of money to be withdrawn and withdraw it. # If user input is 'd', then ask the amount of money to be deposited and deposit it. # If user input is 'c', then check the current balance. # # Hints: # # 1) We have created a global variable "balance" that will have the value of the money you have in the bank. You need to access this variable everytime you need to deposit, withdraw, or check your balance. # # 2) Normally global variables can be accessed but not changed from inside functions. so, Use global keyword to access and change global variables from inside function. # # 3) There can not be a negative balance in the bank. # In[ ]: balance = 0 def deposit(amount): # YOUR CODE HERE # Write code here to add the `amount` to the `balance`. print("You have deposited" + str(balance) + "birr") pass def withdraw(amount): # YOUR CODE HERE # Write code here to withdraw the `amount` from the `balance`. # If the amount is larger than the balance, make sure to print a message # telling the customer that the withdrawal is improper. # As an example for how to write print statements, # see the print line written above in the deposit function. pass def bank(): while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process == "d": amount = float(input("How much do you want to deposit?")) deposit(amount) if process == "c": print('your current balance is ' + str(balance) + 'birr') if process == "w": amount = float(input("How much do you want to withdraw?")) withdraw(amount) if process == "stop": break else: print("Please type d, w, c, or stop, then press enter.") bank() bank() # ### Question 2 # # Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. # Sample String : 'The quick Brow Fox' # # Expected Output : # # No. of Upper case characters : 3 # # No. of Lower case Characters : 12 # In[ ]: def upper_lower(s): # Your code here. upper_lower("The quick Brow Fox") # ### Question 3 # Write a Python function that takes a list and returns a new list with unique elements of the first list. # # Sample List : `[1, 2 ,3, 3, 3, 3, 4, 5]` # # Unique List : `[1, 2, 3, 4, 5]` # In[ ]: lst = [1, 2, 3, 3, 3, 3, 4, 5] def unique_lst(l): # Your code here. unique_lst(lst) # ### Question 4 # Write a Python function to check whether a number is perfect or not. # # Note: In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive # divisors, that is, the sum of its positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself). # # Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and `1 + 2 + 3 = 6`. Equivalently, the number 6 is equal to half the sum of all its positive divisors: `( 1 + 2 + 3 + 6 ) / 2 = 6`. # # The next perfect number is `28 = 1 + 2 + 4 + 7 + 14`. This is followed by the perfect numbers 496 and 8128. # In[ ]: def is_perfect(n): # Your code here. is_perfect(6) # ### Question 5 # Write a Python function to check whether a string is a pangram or not. # # Note : Pangrams are words or sentences containing every letter of the alphabet at least once. # For example : "The quick brown fox jumps over the lazy dog" # In[ ]: def is_pangram(s): # Your code here. is_pangram('The quick brown fox jumps over the lazy dog')