SOME IMPORTANT PYTHON CODES
1. Check if entered number is a valid mobile number:
n=input("Enter Mobile Number: ")
b=len(n)
c=n.isdigit()
if b==10 and c==True:
print("Valid Mobile number")
else:
print("Invalid Number")
2. Input a number and print its cube (n3):
n=int(input("Enter number for cube: "))
print("Cube of number is:", n**3)
3. Marks grading system:
a=int(input("Enter your number: "))
if a>=90:
print("A+")
elif a>=80:
print("A")
elif a>=70:
print("B+")
elif a>=60:
print("B")
else:
print("fail")
4. Check if user is 18+ or not (eligible for vote):
age=int(input("Enter Your Age: "))
if(age >= 18):
print("You're an adult!")
else:
print("You're not adult!")
5. Change KM unit to Miles:
km=int(input("Enter KM Value: "))
print("Miles:",km * 0.621371)
6. For loop printing number side by side:
for i in range(1, 113):
print(i, end="\t")
7. Check if the entered number is odd or even:
a=int(input("Enter number : "))
if a%2==0:
print(a, "Is Even Number")
else:
print(a, "Is Odd Number")
8. Palindrome (same spelling even if read from backwards, e.g. racecar):
x=input("Enter your words: ")
if x==x[::-1]:
print("Yes")
else:
print("No")
9. Calculate simple interest:
p=int(input("Enter Principal Amount: "))
r=int(input("Enter Rate of Interest: "))
t=int(input("Time in Years: "))
si=(p*r*t)/100
print("Simple Interest Is:", si)
10.Sum of all even and odd numbers from an entered range:
a=int(input("Starting: "))
b=int(input("Ending: "))
c=0
d=0
for i in range(a, b+1):
if i%2==0:
c+=i
elif i%2!=0:
d+=i
print("Sum of even numbers from", a, "to", b, "is:", c)
print("Sum of odd numbers from", a, "to", b, "is:", d)
11.Swap the values of variables:
x=int(input("Num 1"))
y=int(input("Num 2"))
z=int(input("Num 3"))
print("Your Values", x, y, z)
x, y, z = y, z, x
print("Swapped Values", x, y, z)
12.Check if a number is Armstrong or not:
a=int(input("Enter Num: "))
b=0
c=a
while c > 0:
b+=(c%10)**3
c = c // 10
if b == a:
print("Armstrong")
else:
print("Not Armstong")
13.Determine AM or PM from 24-hour time entry:
a=float(input("Enter Time: "))
if a<=12:
print("AM")
elif a<=24:
print("PM")
elif a==24:
print("AM")
else:
print("Invalid Time")
14.Calculate the area of a triangle:
h=float(input("Enter Height: "))
b=float(input("Enter Base: "))
print("Area of triangle is:", 1/2 * b * h, "unit sq.")
15.Take 4 input and print out the greater one:
a=float(input("Enter 1st number: "))
b=float(input("Enter 2nd Number: "))
c=float(input("Enter 3rd number: "))
d=float(input("Enter 4th Number: "))
if a>b and a>c and a>d:
print(a, "is greater")
elif b>a and b>c and b>d:
print(b, "is greater")
elif c>a and c>b and c>d:
print(c ,"is greater")
else:
print(d, "is greater")
16.Fibonacci series:
n=int(input("Enter number: "))
n1=0
n2=1
print(n1)
print(n2)
for i in range(2, n):
n3=n1+n2
n1=n2
n2=n3
print(n3)
17.Enter a number and sum all the digits (e.g. 123 = 1+2+3):
t=int(input("enter a number"))
s=0
while t > 0:
d=t%10
s=s+d
t=t//10
print("sum of number is", s)
Thanks for going through this document, I hope you found it helpful. All formatting,
and design have been prepared by Atif Arman. For further details, you can scan the
QR code provided aside: