Python Language
Program 1:
print("My First Program")
Program 2:
# symbol is used for comments
n=10
print("Value of N=",n)
Program 3: Input runtime values
ch=input("Enter a character")
name=input("Enter your Name")
n=int(input("Enter a number"))
print("Value of ch",ch)
print("Name=",name)
print("Number=",n)
Program 4:
n1=int(input("Enter first number"))
n2=int(input("Enter second number"))
print("Result of sum=",n1+n2)
print("Result of Subtration=",n1-n2)
print("Result of Multiplication=",n1*n2)
print("Result of Division=",n1/n2)
print("Result of Remainder=",n1%n2)
print("Result of Exponentiation=",n1**n2)
print("Result of Floor Division=",n1//n2)
Program 5: multiple variables value initialization
n1,n2=5,2
print("Result of sum=",n1+n2)
print("Result of Subtration=",n1-n2)
print("Result of Multiplication=",n1*n2)
print("Result of Division=",n1/n2)
print("Result of Remainder=",n1%n2)
print("Result of Exponentiation=",n1**n2)
print("Result of Floor Division=",n1//n2)
Program 6:
n=5
n+=2
print("Result of sum=",n)
n-=2
print("Result of Subtration=",n)
n*=2 ; print("Result of Multiplication=",n)
n/=2 ; print("Result of Division=",n)
n %=2 ;print("Result of Remainder=",n)
n=5;n**=2 ; print("Result of Exponentiation=",n)
n=5;n//=2 ; print("Result of Floor Division=",n)
program 7:
k=int(input("Enter Kilometers"))
m=k*1000
print("Meters=",m)
Program 8:
c=float(input("Enter Temprature in Celsius"))
f=9.0/5.0*c+32
print("Temprature in Fahrenheit=",f)
Program 9:
x = 10
if x > 5:
print("x is greater than 5")
Program 10:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
Program 11:
x=int(input("Enter first number"))
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but not greater than 10")
else:
print("x is not greater than 5")
Program 12
If else if in one line
a=int(input("Enter first number"))
b=int(input("Enter first number"))
print("A") if a > b else print("=") if a == b else print("B")
Program 13:
a=int(input("Enter first number"))
b=int(input("Enter second number"))
c=int(input("Enter second number"))
if a>b:
if a>c:
print("A is greatest")
else:
print("C is greatest")
else:
if b>c:
print("B is greatest")
else:
print("C is greatest")
Program 14:
a=int(input("Enter first number"))
b=int(input("Enter second number"))
c=int(input("Enter second number"))
if a>b and a>c:
print("A is greatest")
elif b>c and b>c:
print("B is greatest")
else:
print("C is greatest")
Program 15:
a=int(input("Enter first number"))
b=int(input("Enter second number"))
op=input("Enter Operator")
match op:
case "+":
print("Result of sum=",a+b)
case "-":
print("Result of sum=",a-b)
case "*":
print("Result of sum=",a*b)
case "/":
print("Result of sum=",a/b)
case _: # underscore is for default case
print("Incorrect Operator")
Program 16:
While Loop
number = 1
while number <= 10:
print(number)
number += 1
Program 17:
number = 1
sum = 0
while number <= 10:
print(number)
sum=sum + number
number += 1
print("Sum=",sum)
Program 18:
n = int(input("Enter a number"))
f = 1
c = 1
while c <= n:
f = f * c
c+=1
print("Factorial=",f)
Program 19:
n = int(input("Enter a number"))
c = 1
while c <= 10:
print(n,"*",c,"=",n*c)
c+=1
Program 20:
For Loop
friends = ["Ahmad", "All", "Hassan"]
for n in friends:
print("As-Salaam-Alaikum", n)
Program 21:
cart = [
{"item": "Apple", "price": 0.5, "quantity": 4},
{"item": "Banana", "price": 0.2, "quantity": 10},
{"item": "Orange", "price": 0.75, "quantity": 3}
]
total_price = 0
for product in cart:
item_total = product["price"] * product["quantity"]
total_price += item_total
print(f"{product['item']} total: ${item_total:.2f}")
print (f"Total price of all items: ${total_price:.2f}")
Program 22:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
Program 23:
word = "hello"
for char in word:
print(char)
output:
h
e
l
l
o
Program 24:
for i in range(5):
print(i)
output
0
1
2
3
4
Program 25:
for i in range(1,6):
print(i)
output
1
2
3
4
5
Program 26:
for i in range(1,11,2):
print(i)
output
1
3
5
7
9
Program 27:
for i in range(5):
print(i, end=" ")
output
01234
Functions
Program 28:
def greet(name):
print("As-Salaam-Alaikum", name)
# Function invoking means call the function by name and perform the required task For example.
greet ("kashif")
Program 29:
def greet(name = "Student") :
return "As-Salaam-Alaikum,"+ name +"!"
print(greet( )) # Output: As-Salaam-Alaikum,Student!
print(greet("Umer ")) # Output: As-Salaam-Alaikum,Umer!
Program 30:
def introduce(name , age) :
return "My name is " + name + " and I am " + str(age) + " Years old."
print(introduce(name = "Ali",age = 20))
Program 31: