Document
Document
Document
a = 10
b=5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Square:", n ** 2)
if n > 0:
elif n < 0:
else:
if n % 2 == 0:
else:
5. Write a python program to enter marks of 4 subjects (out of 100) and calculate percentage & print
class. If percentage is i) Above 70 then print distinction. ii) Below 70 and above 60 then print first
class. iii) Below 60 and above 50 then print second class. iv) Above 40 then print pass class. v) Else
Fail.
total = m1 + m2 + m3 + m4
percentage = total / 4
print("Percentage:", percentage)
print("Distinction")
print("First class")
print("Second class")
print("Pass class")
else:
print("Fail")
if a > b:
elif a < b:
else:
7. Classify a person's age group and determine if they are eligible to vote. A person under 18 is a minor,
between 18 and 65 is an adult, and above 65 is a senior. Only adults and seniors are eligible to vote.
# vote
age=int(input("entre the age : "))
if(age<18):
print("Minor - Not eligible")
else:
if (age<=65):
print("Adult - Eligible")
else:
print("Senior - Eligible")
entre the age : 90
Senior - Eligible
8. Accept a number (1 to 7) and print day of week. e.g. Enter a number : 5 Friday
if n in days:
print(days[n])
else:
print("Invalid number.")
9. Accept a month (1 to 12) and print month in words.(use match statement) e.g. Enter the Month : 5
May
match m:
case 1:
print("January")
case 2:
print("February")
case 3:
print("March")
case 4:
print("April")
case 5:
print("May")
case 6:
print("June")
case 7:
print("July")
case 8:
print("August")
case 9:
print("September")
case 10:
print("October")
case 11:
print("November")
case 12:
print("December")
case _:
print("Invalid month.")
else:
entre a no : 2
entre a no : 6
entre a no : 2
y is largest
12. Write a python program which accept month (1 to 12) and print number of days in that month. e.g.
Enter the Month = 5 Days = 31
days = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
if m in days:
else:
print("Invalid month.")
n=1
print("Square of", n ** 2)
n=n+1
1
4
9
16
25
36
49
64
81
100
s=0
s=s+n
sum = 0
num = 1
while num <=10:
sum = sum + num
num = num + 1
print("The sum of the first 10 numbers is", sum)
The sum of the first 10 numbers is 6
15. Accept a number till user enter 0 and check whether entered number is even or not (use while loop).
n=1
while(n!=0):
n=int(input("enter a number(0 to exit):"))
if(n%2==0):
print("even")
else:
print("odd")
enter a number(0 to exit):6
even
enter a number(0 to exit):6
even
enter a number(0 to exit):6
even
enter a number(0 to exit):0
even
16. Accept a number and print it’s all divisors using while loop. E.g. Enter a number : 12 Divisors of 12
are : 1,2,3,4,6,12
n= int(input("enter a no"))
i=1
print("Diviors of",n,"are")
while i<=n:
if n%i==0:
print(i)
i=i+1
enter a no12
Diviors of 12 are
1
2
3
4
6
12
print(i)
1
3
5
7
9
11
13
15
17
19
19. Accept a number and Print multiplication table of it , using for loop
for i in range(1,11):
print(n,"x",i,"=",n*i)
fact=1
for i in range(1,n+1):
fact=fact*i
print(n,"!","=",fact)
reverse factorial
n = int(input("Enter a number to print its factorial : "))
fact=1
for i in range(n,1,-1):
fact=fact*i
print(n,"!","=",fact)
Enter a number to print its factorial : 4
4 ! = 4
4 ! = 12
4 ! = 24
21. Accept a number till user entered 0, check whether number is Positive or Negative.
while(True):
n=int(input("entre a number : "))
if(n==0):
break
elif(n>0):
print("positive number")
else:
print("negative number")
entre a number : 3
positive number
entre a number : -3
negative number
entre a number : 0
f=1
is_prime = True
if n % i == 0:
is_prime = False
break
print()
25. Accept a number till user enter 0 and print multiplication table of given number.
while True:
if n == 0:
break
print()
1
22
333
4444
print("")
1
1 2
1 2 3
1 2 3 4
1234
123
12
print("")
****
***
**
print("*",end=" ")
print("")
***
*****
*******
print("")
*
* *
* * *
* * * *
AB
ABC
ABCD
print(chr(64+row),end=" ")
print("")
A
B B
C C C
D D D D
23
456
7 8 9 10
c=1
c=c+n
print()
10
101
1010
for i in range(n):
if i % 2 == 0:
else:
print()
12
123
1234
12345
1234
123
12
print()
print()
for i in range(5):
for j in range(1,i+1):
print(j," ",end=" ")
print("")
1
1 2
1 2 3
1 2 3 4
break statment
i=1
while i<5:
print(i)
if i==3:
break
i+=1
1
2
3
statement
while loop
x=1
while x<=10:
print(x)
x=x+1
1
2
3
4
5
6
7
8
9
10
10 to 1
x=10
while x>=1:
print(x)
x=x-1
10
9
8
7
6
5
4
3
2
1
i=1
j=0
while(i<5):
j=j+i
print(j)
i=i+1
1
3
6
10
For loop
for i in range(5):
print(i)
0
1
2
3
4
for i in range(1,5):
print(i)
1
2
3
4
for i in range(1,5,2):
print(i)
1
3
Nested loop
for i in range(2):
print("i=",i)
for j in range(11,14):
print(j)
i= 0
11
12
13
i= 1
11
12
13
i=5
j=0
while(i>0):
j=j+i
print('j=',j)
i=i-1
else:
print('i=',i)
j= 5
j= 9
j= 12
j= 14
j= 15
i= 0
continue
i=0
while i<5:
i+=1
if i==2:
continue
print(i)
1
3
4
5
Pass statement
for x in "hello":
pass
print("last letter is", x)
last letter is o
functions
def greetings():
name = input("Enter your name:")
print("Hello", name)
greetings()
odd even
def check():
if(n%2==0):
print("even")
else:
print("odd")
n=int(input("Enter a number:"))
check()
Enter a number:8
Even
largest
def large(n1, n2):
if (n1>n2):
print("Largest No is:", n1)
elif (n1<n2):
print("Largest No is:", n2)
else:
print("Both equal")
n1=int(input("Enter a number:"))
n2=int(input("Enter a number:"))
large(n1,n2)
Enter a number:80
Enter a number:99
Largest No is: 99
table
def table(number):
for i in range(1, 11):
print(number, "x", i, "=", number * i)
calculation
def calculation(n1, n2, op):
match op:
case '+': return n1 + n2
case '-': return n1 - n2
case '*': return n1 * n2
case '/':
if n2 == 0:
return "Division not possible"
else:
return n1 / n2
case _: return "Invalid operator"
game
def print_divisors(n):
print("Divisors of", n, "are:")
for i in range(1, n + 1):
if n % i == 0:
print(i)
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
while True:
print("\nMenu:")
print("1. Print divisors of a number")
print("2. Check if a number is prime")
print("3. Calculate factorial of a number")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
num = int(input("Enter a number: "))
print_divisors(num)
elif choice == '2':
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is prime")
else:
print(num, "is not prime")
elif choice == '3':
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid option. Please choose again.")
Menu:
1. Print divisors of a number
2. Check if a number is prime
3. Calculate factorial of a number
4. Exit
Enter your choice: 1
Enter a number: 8
Divisors of 8 are:
1
2
4
8
Menu:
1. Print divisors of a number
2. Check if a number is prime
3. Calculate factorial of a number
4. Exit
maths
sq root
import math
n = int(input("Enter a number: "))
print(math.sqrt(n))
Enter a number: 9
3.0
ceil,floor
import math
x = math.ceil(x)
y = math.floor(y)
print(x,y)
pi,e
import math
x=math.pi
y=math.e
print(x,y)
3.141592653589793 2.718281828459045
today date
import datetime
td=datetime.date.today()
print(td)
2024-03-06
today year month day
from datetime import date
today=date.today()
print("Current Year", today.year)
print("Current Month", today.month)
print("Current Day", today.day)
time
from datetime import datetime
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Time:", t)
Time: 16:38:12
03/06/2024, 16:41:24
06/03/2024, 16:41:24
todays
from datetime import datetime
now = datetime.now()
s1 = now.strftime("%d/%B/%Y, %A")
print(s1)
06/March/2024, Wednesday
27 February 2024,Tuesday
Random
import random
r=random.randint(1,20)
print(r)
Game
import random
def main():
secret_number = random.randint(1, 10)
attempts = 5
if attempts == 0:
print(f"Sorry, you're out of attempts. The secret number
was {secret_number}.")
if __name__ == "__main__":
main()
Strings
Welcome
s="welcome"
print(s[0:3])
print(s[3:])
print(s[3:6])
print(s[5:])
wel
come
com
me
Hello
s="Hello"
for i in range(0,5):
print(s[0:i+1])
s="Hello"
for i in range(0,6):
print(s[0:i])
H
He
Hel
Hell
Hello
H
He
Hel
Hell
Hello
Hello
s="Hello"
for i in range(5,0,-1):
print(s[0:i])
Hello
Hell
Hel
He
H
3 strings
s1="python"
s2="end"
s3="sem"
print(s1+" "+s2+s3)
python endsem
2 strings accept
s1=input("enter : ")
s2=input("enter : ")
s3=input("enter : ")
print(s1+" "+s2+" "+s3)
enter : hello
enter : disha
enter : nashine
hello disha nashine
Repeat strings(s,n)
def repeat_string(s, n):
print(s * n)
def repeat_string():
repeat_string()
Enter the string: d
Enter the number of times to repeat: 6
d d d d d d
def main():
s = input("Enter : ")
n = int(input("Enter the number of times to repeat: "))
repeat_string(s, n)
if __name__ == "__main__":
main()
Enter : G
Enter the number of times to repeat: 3
G G G
String
s="Communication"
print(s[4:7])
print(s[1:3])
print(s[0:3])
print(s[7:])
uni
om
Com
cation
Check
def checks(s, ch):
if ch in s:
return "yes"
else:
return "no"
Update
s="hello, world"
print(s)
Update
s="hello, world"
print(s)
L,u,c,s
def convert_string(string, choice):
if choice == 'l':
return string.lower()
elif choice == 'u':
return string.upper()
elif choice == 's':
return string.swapcase()
elif choice == 'c':
return string.capitalize()
else:
return "Invalid choice"
Occurs
def count_substring_occurrences(string, substring):
count = string.count(substring)
return count
s="Disha Nashine"
ch="a"
print(s.count(ch))
2