solutions
solutions
2. Create a variable name and assign your_ name as the value of it.
name = "Proma"
your_name = name
3. Assign 24 and 13 into two variables. Then, perform addition, subtraction, multiplication, and division,
and print the result.
variable_1 = 24
variable_2 = 13
summation = variable_1+variable_2
subtraction = variable_1 - variable_2
multiplication = variable_1*variable_2
division = variable_1/variable_2
'''The question is a bit vague. For simplicity, let’s redefine the problem
as finding
if the first number is smaller/greater/equal to the second number.
'''
if variable_1>variable_2:
print("Greater")
elif variable_1<variable_2:
print("Smaller")
else:
print("Equal")
area = length*width
print("Area of rectangle: ", area)
7. Write a program that will take two integer numbers from user and print the result of addition,
subtraction, multiplication, and division.
variable_1 = int(input('Integer number 1: '))
variable_2 = int(input("Integer number 2: "))
summation = variable_1+variable_2
subtraction = variable_1 - variable_2
multiplication = variable_1*variable_2
division = variable_1/variable_2
if variable_1>=variable_2:
print(variable_1)
else: print(variable_2)
Note that, The solutions for the FOR and WHILE loop chapters are provided together since those
contain the same problems.
print(sum)
print(sum)
print(sum)
print(sum)
12. Write a program to calculate the factorial of a number.
print(fact)
print(fact)
15. Create a list of numbers and calculate the sum of the numbers.
list = [10,9,8,7]
sum = 0
for i in list:
sum+=i
print(sum)
16. Write a python program that counts the number of words in a given sentence. For example, the
number of words in “I love my country” is 4.
str = "I love my country"
count = 0
i=0
while i<len(str):
if str[i] == ' ':
count+=1
''' the following while block will omit extra spaces'''
while i<len(str) and str[i] ==' ':
i+=1
continue
i+=1
print(count+1)
17. Write a python program that replaces the all occurrences of a with the character *. For example,
“banana” will be changed to “b*n*n*”.
str = "banana"
new_str = str.replace("a","*")
print(new_str)
18. Write a python program to remove every vowel followed by a consonant. For example, “umbrella”
will be changed to “umbrll”.
vowels='aeiou'
str = 'umbrella'
new_str = ''
i = 0
while i<len(str)-1:
if str[i] not in vowels and str[i+1] in vowels:
new_str+=str[i]
i=i+2
else:
new_str+=str[i]
i=i+1
print(new_str)
19. Write a function that takes two integers as arguments and performs the addition, subtraction,
multiplication, and division returns the results.
def operations(variable_1,variable_2):
summation = variable_1+variable_2
subtraction = variable_1 - variable_2
multiplication = variable_1*variable_2
division = variable_1/variable_2
print(operations(2,3))
20. Write a function that takes a number and returns whether the number is odd or even.
def odd_or_even(num):
if num%2==0:
return "even"
else: return "odd"
print(odd_or_even(6))
21. Write a function that takes a list of numbers and returns the sum of these numbers.
def summation(list):
sum = 0
for i in list:
sum+=i
return sum
list = [10,9,8,7]
print(summation(list))