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

Python Practicals

The document contains a series of practical Python programs covering various topics such as temperature conversion, weight conversion, leap year check, and prime number checks. It includes both simple scripts and functions, as well as file handling and class definitions. Each program is designed to demonstrate fundamental programming concepts and user input handling.

Uploaded by

aadityadobar
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)
5 views

Python Practicals

The document contains a series of practical Python programs covering various topics such as temperature conversion, weight conversion, leap year check, and prime number checks. It includes both simple scripts and functions, as well as file handling and class definitions. Each program is designed to demonstrate fundamental programming concepts and user input handling.

Uploaded by

aadityadobar
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
You are on page 1/ 4

Python Practical Programs

1. Temperature Conversion

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


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

2. Kilogram to Pound Conversion

K = float(input("Enter weight in Kilograms: "))


P = 2.2 * K
print("Weight in Pounds:", P)

3. Largest of Three Numbers

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


b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print("Largest number is:", max(a, b, c))

4. Leap Year Check

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

5. Sum of Digits

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


total = sum(int(d) for d in str(abs(num)))
print("Sum of digits:", total)

6. Reverse Digits

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


reversed_num = int(str(abs(num))[::-1])
print("Reversed digits:", reversed_num)

7. Day of Week Using Match-Case

day = int(input("Enter day number (1-7): "))


match day:
case 1: print("Monday")
case 2: print("Tuesday")
case 3: print("Wednesday")
case 4: print("Thursday")
case 5: print("Friday")
case 6: print("Saturday")
case 7: print("Sunday")
case _: print("Invalid day")

8. Sum of First N Natural Numbers

N = int(input("Enter N: "))
print("Sum:", N * (N + 1) // 2)

9. Factorial of a Number

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


fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial:", fact)

10. Prime Check

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


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

11. First N Prime Numbers

N = int(input("Enter N: "))
count = 0
num = 2
while count < N:
if all(num % i != 0 for i in range(2, int(num**0.5)+1)):
print(num, end=' ')
count += 1
num += 1

12. Max and Min in a List

nums = list(map(int, input("Enter numbers separated by space: ").split()))


print("Max:", max(nums))
print("Min:", min(nums))

13. Function to Check Prime

def is_prime(n):
return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5)+1))

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


print("Prime" if is_prime(num) else "Not Prime")

14. Function to Sum Digits

def sum_digits(n):
return sum(int(d) for d in str(abs(n)))

print(sum_digits(int(input("Enter number: "))))

15. Function to Reverse Digits

def reverse_digits(n):
return int(str(abs(n))[::-1])

print(reverse_digits(int(input("Enter number: "))))

16. Recursive Factorial

def factorial(n):
return 1 if n == 0 else n * factorial(n-1)

print(factorial(int(input("Enter number: "))))

17. String Length and Reverse

s = input("Enter a string: ")


print("Length:", len(s))
print("Reversed:", s[::-1])

18. File Handling for Student Records

with open("students.txt", "w") as f:


for _ in range(3):
roll = input("Enter Roll No: ")
name = input("Enter Name: ")
f.write(f"{roll},{name}\n")

with open("students.txt", "r") as f:


print("\nRecords in file:")
for line in f:
print(line.strip())

19. Class with Attributes and Methods

class Student:
def __init__(self):
self.branch = ""

def input_student(self):
self.branch = input("Enter branch: ")

def display_student(self):
print("Branch:", self.branch)

s = Student()
s.input_student()
s.display_student()

You might also like