0% found this document useful (0 votes)
10 views7 pages

solutions

Uploaded by

yousufhossain881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

solutions

Uploaded by

yousufhossain881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SOLUTIONS

1. Write a Python program to print your name.


print("Proma")

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

4. Compare two numbers (e.g, 7 and 5) using relational operators.


variable_1 = 5​
variable_2 = 7​

'''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")

5. Identify whether a number is in between the range of 80 and 100.


variable = int(input())​
if variable>=80 and variable<=100:​
print("YES")​
else: ​
print("NO")
6. Write a program that will the take the length and width of a rectangle from the user and print the area
of the rectangle.
length = float(input("Length of rectangle: "))​
width = float(input("Width of rectangle: "))​

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

8. Write a program to identify whether a number is odd or even.


num = 5​
if num%2==0:​
print("even")​
else: print("odd")

9. Write a program to identify the maximum between two numbers.


variable_1 = float(input('Integer number 1: '))​
variable_2 = float(input("Integer number 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.

10. Write a program to calculate the sum of first 10 natural numbers.

using for loop -


num = 10​
sum = 0​
for i in range(num+1):​
sum+=i​

print(sum)

using while loop -


num = 10​
sum = 0​
i = 0​
while i< num+1:​
sum+=i​
i+=1​

print(sum)

11. Write a program to calculate the sum of first 10 even numbers.

using for loop -


num = 10​
sum = 0​
for i in range((num*2)+1):​
if i%2==0:​
sum+=i​

print(sum)

using while loop -


num = 10​
sum = 0​
i = 0​
while i < (num*2)+1:​
if i%2==0:​
sum+=i​
i+=1​

print(sum)
12. Write a program to calculate the factorial of a number.

using for loop -


num = 9​
fact = 1​
for i in range(1, num+1):​
fact=fact*i​

print(fact)

using while loop -


num = 9​
fact = 1​
i = 1​
while i<num+1:​
fact=fact*i​
i+=1​

print(fact)

13. Write nested loops to print the multiplication table.

using for loop -


num = 10​
print("Multiplication Table: ")​
for i in range(1,num+1):​
for j in range(1,num+1):​
print(i,"X",j,"=",i*j)​
print()

using while loop -


num = 10​
i = 1​
print("Multiplication Table: ")​
while i<num+1:​
j = 1​
while j<num+1:​
print(i,"X",j,"=",i*j)​
j+=1​
i+=1​

print()

14. Create a list of string that contains the days of a week.


days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"]

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​

return summation, subtraction, multiplication, division​

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))

You might also like