0% found this document useful (0 votes)
8 views

PYTHON PROGRAMS

Uploaded by

saniyasanu0412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PYTHON PROGRAMS

Uploaded by

saniyasanu0412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Add Two Numbers


#This program adds two numbers

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

2. Python Program to Calculate the Area of a Triangle


b = int(input("Input the base : "))
h = int(input("Input the height : "))

# Calculate the area of the triangle using the formula: (base


* height) / 2.
area = b * h / 2

# Print the calculated area of the triangle.


print("area = ", area)

Output:
Input the base : 20
Input the height : 40
area = 400.0

3.Python program to swap two variables

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

4.Python program to check if year is a leap year or not

year = int(input("Enter a year: "))

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

# change the values of num1, num2 and num3


# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#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)

Output
The largest number is 14.0

7. Python program to display all the prime numbers within an interval

lower = 900
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):


# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997

8.Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


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

factorial = 1

# 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:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output
The factorial of 7 is 5040

9. Multiplication table (from 1 to 10) in Python

num = 12

# To take input from the user


# num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10


for i in range(1, 11):
print(num, 'x', i, '=', num*i)

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

10. Python program to check if the number is an Armstrong number or not

# take input from the user


num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output 1
Enter a number: 663
663 is not an Armstrong number

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

12. Python Program to Find the Factors of a Number

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

print(f"Factors of {num} is :", end=" ")


for i in range(1, num+1):
if num % i == 0:
print(i, end=",")

Output
Enter a number: 12
Factor of 12 is : 1,2,3,4,6,12,

13. Python Program to Display Calendar

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

combined = list1 + list2 + list3 + list4


print(combined)

output:
['Apples', 'Oranges', 'Bananas', 'Pears', 'Grapes', 'Pineapples',
'Tangerines', 'Apricots', 'Pomegranates', 'Watermelons',
'Raspberries']

You might also like