0% found this document useful (0 votes)
14 views5 pages

Python Programs

The document contains a series of Python programming exercises that cover basic calculations, tuple traversal, finding the largest element in a list, displaying odd numbers, working with dictionaries, calculating factorials, summing natural numbers, printing patterns, and checking the sign of numbers. Each exercise includes sample code and expected output. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

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

Python Programs

The document contains a series of Python programming exercises that cover basic calculations, tuple traversal, finding the largest element in a list, displaying odd numbers, working with dictionaries, calculating factorials, summing natural numbers, printing patterns, and checking the sign of numbers. Each exercise includes sample code and expected output. The programs demonstrate fundamental programming concepts and operations in Python.

Uploaded by

p2k44p4hsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMS

9. Write a program to perform basic calculations.


a=int(input("Enter first value:"))
b=int(input("Enter second value:"))
print("The sum of the two numbers is: ", a + b)
print("The difference of the two numbers is: ", a - b) print("The product of the two
numbers is: ", a * b) print("The quotient of the division operation is: ", a / b)
print("The remainder of the division operation is: ", a % b)
print("The result of the floor division is: ", a // b) print(a, "raised to the power", b,
"is:", a ** b)
Output:
Enter first value:20
Enter second value:2
The sum of the two numbers is: 22
The difference of the two numbers is: 18
The product of the two numbers is: 40
The quotient of the division operation is: 10.0 The remainder of the division
operation is: 0
The result of the floor division is: 10
20 raised to the power 2 is: 400

10.
11.

12. Write a program to traverse a tuple.

fruits=(“apple”, “banana”, “cherry”)


for i in range(len(fruits)):
print(fruits[i])

Output of the program:


Apple
Banana
Cherry
13. write a program to find the largest element in the list(without in built
function).

a=[5,6,12,20,6,1,35,9]
largest=a[0]
i=0
while i<len(a)
if a[i] > largest:
largest=a[i]
i=i+1
print (“the largest element is:”)
print(largest)

Output:
The largest element is 35

14. WAP that displays all the odd numbers from list.
List1=[10,23,44,5,65,93,78]
for num in List1:
if num%2!=0:
print(num,end = “ “)
output of the program:
25 5 65 93

15. WAP to display the dictionary items.


Birthday_list ={‘adhvika’:’24 Aug’ , ‘ankur’:’10 Apr’ , ‘tina’:’12 may’}
for i in Birthday_list
print(i, ‘:’, Birthday_list[i])

output
adhvika : 24 Aug
ankur : 10 Apr
tina : 12 may

[Link] a program to obtain a number n from a user and find its factorial.
n= int(input (“enter a positive number to find its factorial:”))
f=1
for i in range (1, n+1)
f=f*i
print(“n! = ”, f)

output
enter a positive number to find its factorial : 5
n!=120

[Link] that prints the sum of first 10 natural numbers.


i=1
sum = 0
while(i<=10):
sum = sum + i
i=i+1
print (“The sum of first 10 natural numbers is:” , sum)

Output
The sum of first 10 natural numbers is: 55

[Link] to print the following pattern . Obtain the number of rows from the user.
*
**
***
****
*****
Rows= int(input(“enter number of rows:”))
for i= in range(rows):
for j in range(i+1)
print(“*”, end=’’)
print(“\n”)

output
Enter no of rows : 5
*
**
***
****
*****

19. WAP to check whether the number entered by user is positive , negative or zero.
Using if –else

num = float(input(“Enter a number:”))


if num >= 0:
if num == 0:
print(“Zero”)
else:
print(“positive number”)
else:
print(“negative number”)

output
Enter any number: -87
Number is negative.
Enter any number: 0
Number is zero
Enter any number:
1106
Number is positive.

20. WAP to check whether the number entered by user is positive , negative or zero.
Using if-elif
Num= int(input(“enter any number:”))
If( num > 0):
Print(“Number is positive”)
Elif (num < 0):
Print(“Number is negative”)
Else:
Print(“number is zero”)

Output
Enter any number: -87
Number is negative.
Enter any number: 0
Number is zero
Enter any number:
1106
Number is positive

21. Write a program to calculate area of circle, take radius from use
#user input: radius
r= float(input("Enter radius of circle"))
a=3.14 *r**2
print (a," [Link]")
Output:
Enter radius of circle 5.
78.5 [Link]

You might also like