The document contains various Python programs demonstrating basic programming concepts such as addition, square root calculation, checking odd/even numbers, variable swapping, temperature conversion, leap year determination, factorial calculation, Armstrong number check, and more. Each program includes code snippets and expected outputs for user inputs. The document serves as a practical guide for beginners to learn Python programming through examples.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views12 pages
Python Programs Class 11 and 12 For Knowledge
The document contains various Python programs demonstrating basic programming concepts such as addition, square root calculation, checking odd/even numbers, variable swapping, temperature conversion, leap year determination, factorial calculation, Armstrong number check, and more. Each program includes code snippets and expected outputs for user inputs. The document serves as a practical guide for beginners to learn Python programming through examples.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
This program adds twonumbers — | ~
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print(‘The sum of {0} and {1} is {2}'.format(num1, num2, sum))
OUTPUT
The sum of 1.5 and 6.3 is 7.8
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8°
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ynum_sqrt))
+ Python Program to calculate the square root
7 —
# Note: change this value for a different result
num =8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5 , ‘m_sart))
print("The square root of %0.3f is %0.3f'96(num ,num_
OUTPUT -
The square root of 8.000 is 2.828# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
OUTPUT
Output 1
Enter a number: 43
43 is Odd
Output 2
Enter a number: 18
18 is Even
?ython program to swap two variables
v7
x25
y=10
# To take inputs from the user
x= input('Enter value of x: ')
#y = input('Enter value of y:')
#t create a temporary variable and swap the values
temp=x
wey
y=temp \
Print('The value of x after swapping: {}'.format(x))
print("The value of y after swapping: {}'.format(y))
OUTPUT
The value of x after swapping: 10
The value of y after swapping: 5! Python Program to convert temperature in celsius to fahrenheit
| ee
# change this value fora different result
celsius = 37.5
# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit’
%(celsius,fahrenheit))
OUTPUT
37.5 degree Celsius is equal to 99.5 degree Fahrenheit
# Python program to check if year is a leap year or not
i
year = 2000
# To get year (integer input) from the user
# year = int(input("Enter a year: "))
# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))
# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print("{0} is not a leap year" format(year))
OUTPUT
2000 is a leap year
} python program to find the factorial of a number provided by the user.
achange the value for a different result
num =7
#To take input from the user
#num = int(input("Enter a number: "))
factorial = 1
#t check if the number is negative, positive or zero
if num <0:
print("Sorry, factorial does not exist for negative numbers"
elif num == 0:
print("The factorial of 0 is 1")
else:
fori in range(1,num +
factorial = factorial*i
print("The factorial of",num,"is" factorial)
OUTPUT
The factorial of 7 is 5040
Python program to check if the number is an Armstrong number or not
2
# take input from the user
num = int{input("Enter a numb
# initialize sum
sum =0
# find the sum of the cube of each digit
temp =num
while temp > 0:
sum 4= digit ** 3
temp //=10
# display the result
if num == sum:
print(num,"is an Armstrong number”)
else:
print(num,'
OUTPUT
Enters number: 663
(663 is not an Armstrong number
~ #Sum of natural numbers up to pury
“is not an Armstrong number")num =16
ifnum <0:
print("Enter a positi
‘else: Positive number")
sum=0
# use while loop to i
Pp to iterate
while(num > 0): until zero
sum += num
num -=1
print("The sum is", sum)
OUTPUT
The sum is 136
‘from enum import Enum
class Day(Enum):
_ MONDAY = 1
TUESDAY =2
WEDNESDAY = 3
# print the enum member
print(Day. MONDAY)
# get the name of the enum member
print(Day.MONDAY.name) il
ber
# get the value of the enum mempe!
print(Day.MONDAY value)
OUTPUT
Day. MONDAY
MONDAY
aPython Program to Create a Countdown Timer
mport time
of countdown(time_sec):
while time_sec:
mins, secs = divmod(time_sec, 60)
timeformat = ‘{:02d}:{:02d}' format(mins, secs)
print(timeformat, end="\r')
time.sleep(1)
time_sec -=1
print("stop")
countdown(5)
2 ‘ow to List Even Numbers 1-100 on Python?
‘or i in range(1,101):
of i%2==@:
orint(Z)To check if a number is positive, negative or zero:
num = int(input("Enter a number: "))
if num > 0:
print("{O} is positive" .format(num))
elif num == 0:
print("{O} is zero" format(num))
else:
print("{0} is negative" .format(num))- To find the largest number among three numbers:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
To print the Fibonacci sequence:
nterms = int(input("How many terms? ")
ni, n2=0,1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto", nterms, ":")
print(n1)_.. Check if a number is prime:
num = int(input("Enter a number: "))
ifnum > 1:
for iin range(2, num):
if (num % i) == 0:
print(num, "is not a prime number”)
break
else:
print(num, "is a prime number")
else:
~
ZZ‘ print(num, "is not a prime number")
Find the sum of natural numbers
num = int(input("Enter a number: "))
if num <0:
print("Enter a positive number")
>ython program to calculate the cube of a number.
num = int(input(“Enter a number to calculate cube : “)
print(“cube =”,num*num*num)
Python program to check given character is a vowel or
consonant.
Ch=input(“Enter a character to check vowel or consonant :”)
if(ch == ‘a’ or ch ‘e’ or ch == ‘i’ or ch == ‘0’ or ch ==‘u’ or
ch == ‘A’ or ch == ‘E’ or ch == “’ or ch == ‘O’ or ch == ‘U’):
#if ‘if’ condition satisfy
print(“Given character”, ch ,”is vowel”)
else:
#if ‘if? co!
“Given character”,ch,
ndition does not satisfy the condition
“is consonant”)
print(\
H aseyeors 2 Pan Porras of cinoge]
a=Int(input(“enter the length:”))
b=Int(input(“enter the breadth”)
Perimeter=2*(a+b)
Print(Perimeter)
2rogram to find area of rectangle?
a=Int(input(“enter the length:”))
b=Int(input(“enter the breadth”))
area=a*b
Print(area)
agram to find perimeter of square?
a=int(input (“enter the value of side of square:”))
b=4*a
Print(b) :
yrogram to find area of square?
a= int(input(“enter the value of side of square:”))
b=a*a
au Print(b)
arogram to find circumference of circle?
rint(input(“enter the radius of circle:”))
L circumference=2*22/7*"
il ?
vrogram to find area of circle?reint(input(“enter the radius of circle:”))
area=22/7*r*r
program to find the radius of circle when circumference
is given?
circumference=int(input(“enter the circumference of circle”)
radius = circumference / (2 * 22/7)
Print(radius)
rogram to find the diameter of circle?
r=int(input(“enter the radius of circle:”))
diameter=2*r
print(diameter) ae
rogram to find the side of square: when peimeter is
given?
area= int(input(“enter the aréa of square: a
side=area/4 '
Print(side) :