Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python Lab Notebook

The document is an index of Python programming exercises dated from August 12 to August 27, 2024. It includes various tasks such as calculating sums, areas, and factorials, as well as checking for even/odd numbers, leap years, and prime numbers. Each task is accompanied by example code and input/output demonstrations.

Uploaded by

Ajit Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Lab Notebook

The document is an index of Python programming exercises dated from August 12 to August 27, 2024. It includes various tasks such as calculating sums, areas, and factorials, as well as checking for even/odd numbers, leap years, and prime numbers. Each task is accompanied by example code and input/output demonstrations.

Uploaded by

Ajit Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

INDEX

S. No. Date Programs Page No. Signature

1 12.08.2024 To display the sum of two numbers 1

2 12.08.2024 Calculate area of a circle 1

3 12.08.2024 Accept a number from keyboard and test 1


whether the given number is even or odd

4 12.08.2024 To display numbers from 1 to 10 using while 1-2


loop

5 12.08.2024 To display even numbers between x and y 2

6 20.08.2024 To display the elements of a list using a for 2


loop

7 20.08.2024 Write a Python function to swap two 2-3


numbers

8 20.08.2024 Write a Python function to find the greatest 3


of 3 numbers

9 20.08.2024 Find the factorial of a given number 3

10 20.08.2024 Calculate the cube of all numbers from 1 to 3-4


a given number

11 27.08.2024 Write a Python program to check if a year is 4


a leap year or not

12 27.08.2024 Write a Python program to check if a 4


number is prime or not

13 27.08.2024 Write a Python program to display the 4-5


multiplication table

14 27.08.2024 Write a Python program to convert 5


Fahrenheit to Celsius without function
1. To display the sum of two numbers.

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


b = int(input("Enter second number: "))
sum_ = a + b
print("Sum:", sum_)

Input: Enter first number: 5


Enter second number: 3
Output: Sum: 8

2. Calculate area of a circle.

import math
radius = float(input("Enter radius of the circle: "))
area = math.pi * radius ** 2
print("Area:", area)

Input: Enter radius of the circle: 7


Output: Area: 153.93804002589985

3. Accept a number from keyboard and test whether the given number is even or odd.

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


if num % 2 == 0:
print("Even")
else:
print("Odd")

Input: Enter a number: 4


Output: Even

4. To display numbers from 1 to 10 using while loop.

i = 1
while i <= 10:
print(i)
i += 1
Output: 1
2
3
4
5
6
7
8
9
10

5. To display even numbers between x and y.

x = int(input("Enter start number: "))


y = int(input("Enter end number: "))
for num in range(x, y + 1):
if num % 2 == 0:
print(num)

Input: Enter start number: 3


Enter end number: 9
Output: 4
6
8

6. To display the elements of a list using a for loop.

my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)

Output: 1
2
3
4
5

7. Write a Python function to swap two numbers.

def swap(a, b):


return b, a
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
x, y = swap(x, y)
print("Swapped values:", x, y)

Input: Enter first number: 8


Enter second number: 12
Output: Swapped values: 12 8

8. Write a Python function to find the greatest of 3 numbers.

def greatest_of_three(a, b, c):


return max(a, b, c)

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
z = int(input("Enter third number: "))
print("Greatest number:", greatest_of_three(x, y, z))

Input: Enter first number: 5


Enter second number: 9
Enter third number: 3
Output: Greatest number: 9

9. Find the factorial of a given number.

import math
num = int(input("Enter a number: "))
print("Factorial:", math.factorial(num))

Input: Enter a number: 5


Output: Factorial: 120

10. Calculate the cube of all numbers from 1 to a given number.

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


for i in range(1, num + 1):
print(i ** 3)
Input: Enter a number: 4
Output: 1
8
27
64

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

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


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

Input: Enter a year: 2020


Output: Leap year

12. Write a Python program to check if a number is prime or not.

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


if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print("Not prime")
break
else:
print("Prime")
else:
print("Not prime")

Input: Enter a number: 29


Output: Prime

13. Write a Python program to display the multiplication table.

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


for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Input: Enter a number: 7
Output: 7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

14. Write a Python program to convert Fahrenheit to Celsius without function.

fahrenheit = float(input("Enter temperature in Fahrenheit: "))


celsius = (fahrenheit - 32) * 5/9
print("Temperature in Celsius:", celsius)

Input: Enter temperature in Fahrenheit: 98


Output: Temperature in Celsius: 36.666666666666664

You might also like