Exercises on variable scope.
# PLEASE RUN THE FOLLOWING CODE FIRST.
%config InteractiveShell.ast_node_interactivity="none"
# 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)
# 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.
# YOUR ANSWER HERE
# 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)
# 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)
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.
In Python, the input function reads from the user( not necessarily the programmer) and returns the inputs as string values.
Example:
my_name = input("Please enter your name.")
In the example above, users can type in their names, and the input will be stored in the my_name
variable.
# Try running the following code and see the result.
my_name = input("Please enter your name.")
print(my_name)
Values returned from the input
function are always strings.
Before running the following block of code, see if you can spot an error.
age = input("How old are you?")
print("Your current age is " + age)
ageNextYear = age + 1
print("Next year, your age will be " + ageNextYear)
# How can you fix the above problem? Edit the code below.
age = input("How old are you?")
print("Your current age is " + age)
ageNextYear = age + 1
print("Next year, your age will be " + ageNextYear)
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:
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.
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.
There can not be a negative balance in the bank.
balance = 0
def deposit(amount):
# YOUR CODE HERE
# Write code here to add the `amount` to the `balance`.
print("You have deposited" + str(balance) + "JMD")
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) + 'JMD')
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()
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
def upper_lower(s):
# Your code here.
pass
upper_lower("The quick Brow Fox")
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]
lst = [1, 2, 3, 3, 3, 3, 4, 5]
def unique_lst(l):
# Your code here.
pass
unique_lst(lst)
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.
def is_perfect(n):
# Your code here.
pass
is_perfect(6)
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"
def is_pangram(s):
# Your code here.
pass
is_pangram('The quick brown fox jumps over the lazy dog')