Document

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

1. Initialize 2 variables and print addition, subtraction, multiplication and division of it.

a = 10

b=5

print("Addition:", a + b)

print("Subtraction:", a - b)

print("Multiplication:", a * b)

print("Division:", a / b)

2. Accept a number and print square of it.

n = int(input("Enter a number: "))

print("Square:", n ** 2)

3. Accept a number and check whether it is positive or negative.

n = int(input("Enter a number: "))

if n > 0:

print("The number is positive.")

elif n < 0:

print("The number is negative.")

else:

print("The number is zero.")

4. Accept a number and check whether it is even or odd number.

n = int(input("Enter a number: "))

if n % 2 == 0:

print("The number is even.")

else:

print("The number is odd.")

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.

m1 = int(input("Enter marks of subject 1: "))

m2 = int(input("Enter marks of subject 2: "))

m3 = int(input("Enter marks of subject 3: "))


m4 = int(input("Enter marks of subject 4: "))

total = m1 + m2 + m3 + m4

percentage = total / 4

print("Percentage:", percentage)

if percentage >= 70:

print("Distinction")

elif percentage >= 60:

print("First class")

elif percentage >= 50:

print("Second class")

elif percentage >= 40:

print("Pass class")

else:

print("Fail")

6. Accept 2 numbers and print the largest from it.

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

if a > b:

print("The largest number is", a)

elif a < b:

print("The largest number is", b)

else:

print("The numbers are equal.")

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

n = int(input("Enter a number (1 to 7): "))

days = {1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday"}

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

m = int(input("Enter a month (1 to 12): "))

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

10. Accept 3 numbers and print smallest number from it.

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

c = int(input("Enter the third number: "))

if a < b and a < c:

print("The smallest number is", a)

elif b < a and b < c:

print("The smallest number is", b)

elif c < a and c < b:

print("The smallest number is", c)

else:

print("The numbers are equal.")

11. Accept 3 numbers and print largest number from it.


x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
z = int(input("Enter the third number: "))
if x>y>z:
print("The largest number is", a)
elif x<y>z:
print("The largest number is", y)
elif x<y<z:
print("The largest number is", z)
else:
print("The numbers are equal.")

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

m = int(input("Enter a month (1 to 12): "))

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:

print("Days =", days[m])

else:

print("Invalid month.")

n=int(input("Entre a month number: "))


if((n==1)or(n==3)or(n==5)or(n==7)or(n==8)or(n==10)or(n==12)):
print("31 days")
elif((n==4)or(n==6)or(n==9)or(n==11)):
print("30 days")
elif(n==2):
print("28 days")
else:
print("N/A")

Entre a month number: 2


28 days

Print square of first 10 numbers using while loop.

n=1

while n <= 10:

print("Square of", n ** 2)

n=n+1

1
4
9
16
25
36
49
64
81
100

13. Print sq by taking number from user


x=input("entre a no: ")
x=int(x)
print(x**2)
entre a no: 5
25

14. Print sum of first 10 numbers

s=0

for n in range(1, 11):

s=s+n

print("Sum of first 10 numbers is", s)

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

17. Print cube of first 10 numbers using for loop.

for n in range(1, 11):

print("Cube of", n, "is", n ** 3)

for i in range (1,10):


print (i*i*i)
1
8
27
64
125
216
343
512
729

18. Print all even numbers from first 20 numbers.

for i in range (2,21,2):

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

n = int(input("Enter a number to print its multiplication table: "))

for i in range(1,11):

print(n,"x",i,"=",n*i)

Enter a number to print its multiplication table: 3


3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

20. Accept a number and Print factorial of it , using for loop

n = int(input("Enter a number to print its factorial : "))

fact=1

for i in range(1,n+1):

fact=fact*i

print(n,"!","=",fact)

Enter a number to print its factorial : 4


4 ! = 1
4 ! = 2
4 ! = 6
4 ! = 24

 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

22. Accept a number and check whether it is Prime number or not.


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

23. Print factorial of first 10 numbers.

for n in range(1, 11):

f=1

for i in range(1, n + 1):


f=f*i

print("The factorial of", n, "is", f)

24. Print prime numbers from given range.

low = int(input("Enter the lower limit: "))

high = int(input("Enter the upper limit: "))

print("The prime numbers from", low, "to", high, "are:")

for n in range(low, high + 1):

is_prime = True

for i in range(2, int(n ** 0.5) + 1):

# check if the number is divisible by the current divisor

if n % i == 0:

is_prime = False

break

if is_prime and n > 1:

print(n, end=" ")

print()

25. Accept a number till user enter 0 and print multiplication table of given number.

while True:

n = int(input("Enter a number (0 to stop): "))

if n == 0:

break

print("The multiplication table of", n, "is:")

for i in range(1, 11):

print(n, "x", i, "=", n * i)

print()

26. Print the following output using loops

1
22

333

4444

for row in range(5):

for column in range(row):

print(row," ",end=" ")

print("")

27. Print the following output using loops

1
1 2
1 2 3
1 2 3 4

for row in range(0,6):


for column in range(1,row):
print(column," ",end=" ")
print("")
28. Print the following output using loops

1234

123

12

for row in range(5,0,-1):

for column in range(1,row):

print(column," ",end=" ")

print("")

28. Print the following output using for loop

****

***

**

for row in range(5,0,-1):

for column in range(1,row):

print("*",end=" ")
print("")

29. Print the following output using loops

***

*****

*******

for row in range(5):

for column in range(row):

print("* ",end=" ")

print("")

for row in range(5):


for column in range(row):
print("* ",end=" ")
print("")

*
* *
* * *
* * * *

30. Print the following output using loops

AB

ABC

ABCD

for row in range(5):

for column in range(row):

print(chr(64+row),end=" ")

print("")

for row in range(5):


for column in range(row):
print(chr(64+row),end=" ")
print("")

A
B B
C C C
D D D D

31. Print the following output using loops

23

456

7 8 9 10

c=1

for n in range(1, 5):

for i in range(c, c + n):

print(i, end=" ")

c=c+n

print()

32. Print the following output using loops

10

101

1010

for n in range(1, 5):

for i in range(n):

if i % 2 == 0:

print(1, end=" ")

else:

print(0, end=" ")

print()

33. Print the following output using loops


1

12

123

1234

12345

1234

123

12

for n in range(1, 6):

for i in range(1, n + 1):

print(i, end=" ")

print()

for n in range(4, 0, -1):

for i in range(1, n + 1):

print(i, end=" ")

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

Else statement with for loop


for i in range(5):
print(i)
else:
print("else statment")
0
1
2
3
4
else statment

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

Enter your name:disha


Hello disha

 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

 odd even user


def check(n):
if(n%2==0):
print("even")
else:
print("odd")
check(8)

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

num = int(input("Enter a number: "))


table(num)
Enter a number: 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

 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"

n1 = float(input("Enter the first number: "))


n2 = float(input("Enter the second number: "))
op = input("Enter the operator (+, -, *, /): ")
result = calculation(n1, n2, op)
print("Result:", result)

Enter the first number: 10


Enter the second number: 100
Enter the operator (+, -, *, /): /
Result: 0.1

 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 = float(input("Enter a number: "))


y = float(input("Enter a number: "))

x = math.ceil(x)
y = math.floor(y)

print(x,y)

Enter a number: 4.3


Enter a number: 4.3
5 4

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

Current Year 2024


Current Month 3
Current Day 6

 time
from datetime import datetime
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Time:", t)

Time: 16:38:12

 date and time


from datetime import datetime
now = datetime.now()
s1 = now.strftime("%m/%d/%Y, %H:%M:%S")
s2 = now.strftime("%d/%m/%Y, %H:%M:%S")
print(s1)
print(s2)

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


from datetime import datetime
date = datetime(2024, 2, 27)
day_of_week = date_obj.strftime("%A")
print(date_obj.strftime("%d %B %Y,") + day_of_week)

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

print("Welcome to the Guessing Game!")


print(f"I've picked a number between 1 and 10. You have
{attempts} attempts to guess it.")

while attempts > 0:


try:
guess = int(input("Enter your guess: "))
if guess == secret_number:
print(f"Congratulations! You guessed it. The
secret number was {secret_number}.")
break
elif guess < secret_number:
print("Try a higher number.")
else:
print("Try a lower number.")
attempts -= 1
print(f"You have {attempts} attempts left.")
except ValueError:
print("Invalid input. Please enter a valid integer.")

if attempts == 0:
print(f"Sorry, you're out of attempts. The secret number
was {secret_number}.")

if __name__ == "__main__":
main()

Welcome to the Guessing Game!


I've picked a number between 1 and 10. You have 5 attempts to guess it.
Enter your guess: 5
Try a higher number.
You have 4 attempts left.
Enter your guess: 8
Congratulations! You guessed it. The secret number was 8.

Strings

 Welcome
s="welcome"
print(s[0:3])
print(s[3:])
print(s[3:6])
print(s[5:])
wel
come
com
me

 Hello for loop


s="Hello"
for i in range(0,5):
print(s[i])
H
e
l
l
o

 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

 Hello while loop


"Hello"
i=1
while(i<6):
print(s[0:i])
i=i+1
H
He
Hel
Hell
Hello

 Hello while loop


s = "Hello"
i=5
while i > 0:
print(s[0:i])
i -= 1
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)

repeat_string("hello" " ", 3)


hello hello hello
 Repeat string user

def repeat_string():

s = input("Enter the string: ")

n = int(input("Enter the number of times to repeat: "))

print((s + " ") * n)

repeat_string()
Enter the string: d
Enter the number of times to repeat: 6
d d d d d d

 Repeat string main


def repeat_string(s, n):
print((s+" ") * n)

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"

s = input("Enter a string: ")


ch = input("Enter a character to check: ")
print(checks(s, ch))
Enter a string: Disha
Enter a character to check: D
yes

 Update
s="hello, world"
print(s)

s=s[0:]+ " " "hello"


print("Update")
print(s)
hello, world
Update
hello, world hello

 Update
s="hello, world"
print(s)

s="welcome to new world"


print("Update")
print(s)
hello, world
Update
welcome to new world

 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"

s = input("Enter a string: ")


s1= input("Enter choice (l for lower, u for upper, s for swapcase, c for capitalize): ")

result = convert_string(s, s1)

print("Converted string:", result)


Enter a string: diSHA
Enter choice (l for lower, u for upper, s for swapcase, c for
capitalize): s
Converted string: DIsha

 Occurs
def count_substring_occurrences(string, substring):
count = string.count(substring)
return count

string = input("Enter a string: ")


substring = input("Enter a substring: ")

occurrences = count_substring_occurrences(string, substring)


print("The substring '{}' occurs {} times in the string.".format(substring, occurrences))
Enter a string: How is your day and mom
Enter a substring: How is your day
The substring 'How is your day' occurs 1 times in the string.

s="Disha Nashine"

ch="a"

print(s.count(ch))
2

You might also like