PYTHON PROGRAMS
PYTHON PROGRAMS
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
Output:
Input the base : 20
Input the height : 40
area = 400.0
x=5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x=y
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
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Output
Enter a year: 2022
2022 is not a leap year
6.Python program to find the largest number among the three input
numbers
Output
The largest number is 14.0
lower = 900
upper = 1000
factorial = 1
Output
The factorial of 7 is 5040
num = 12
Output
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
Output 2
Enter a number: 407
407 is an Armstrong number
11.Python Program to Find the Sum of Natural Numbers
num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output
The sum is 136
Output
Enter a number: 12
Factor of 12 is : 1,2,3,4,6,12,
import calendar
def printcalendar(year):
# printing calendar
print(calendar.calendar(year))
# driver program to test above function
year = 2019
printcalendar(year)
Output :
Calendar of 2019
January February
March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We
Th Fr Sa Su
1 2 3 4 5 6 1 2 3
1 2 3
7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6
7 8 9 10
14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13
14 15 16 17
21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20
21 22 23 24
28 29 30 31 25 26 27 28 25 26 27
28 29 30 31
April May
June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We
Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 5
1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5
6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12
13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19
20 21 22 23
29 30 27 28 29 30 31 24 25 26
27 28 29 30
July August
September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We
Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4
1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4
5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11
12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18
19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25
26 27 28 29
30
October November
December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We
Th Fr Sa Su
1 2 3 4 5 6 1 2 3
1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4
5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11
12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18
19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25
26 27 28 29
30 31
Program:
import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))
Output:
14. Python Program to Count the Number of Each Vowel
# string of vowels
vowels = 'aeiou'
ip_str = 'Hello, have you tried our tutorial section yet?'
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}. fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
Output
{'o': 5, 'i': 3, 'a': 2, 'e': 5, 'u': 3}
list1 = ["Apples", "Oranges", "Bananas"]
list2 = ["Pears", "Grapes", "Pineapples"]
list3 = ["Tangerines", "Apricots"]
list4 = ["Pomegranates", "Watermelons", "Raspberries"]
output:
['Apples', 'Oranges', 'Bananas', 'Pears', 'Grapes', 'Pineapples',
'Tangerines', 'Apricots', 'Pomegranates', 'Watermelons',
'Raspberries']