Python Practical File
Submitted By :- Submitted To :-
Ajeet kumar Er. Manmeet Kaur
Gu-2024-3605
Btech cse 3A
SR. PAGE
TITLE SIGNATURE
NO. NO.
Write a program to print Hello World.
1. 3
Write a program to design the calculator.
2. 3
Write a program to find the square root of a
3. number. 4
Write a Python program to print the following string
4. in a specific format - 4
Write a program to swap two numbers using a third
5. variable. 5
Write a program to swap two numbers without
6. using any third variable. 6
Python Program to Check if a Number is Odd or
7. Even. 7
Write a Python program to print the table of a
8. number. 7
Write a Python program to print numbers from 1
9. to 10, skipping even numbers and stopping at 7. 8
Write a Python program that prints the first 10
10. integers and their squares using a while loop. 9
Write a Python function to find factorial of a
11 number. 10
Write a Python function to find the Simple Interest
12. of the number. 11
Write a Python function to calculate the sum of
three given numbers, if the values are equal then
13. 12
return thrice of their sum
Write a Python function to concatenate two strings
14. 13
Write a Python program to demonstrate the usage
15. of input,output and error modules. 14
Write a Python program to demonstrate the usage
of the Maths module by converting the radians to
16. 15
degree and degree to radians.
Program to show the usage of dir() function
17. 16
Program to check if a number is within a given
18. range 16
Program to show the usage of time module
19. 17
Program to subtract five days from the current
date
20. 17
Program to count positive and negative numbers
21. in a list 18
Program to find all possible combinations of a list
22. with three elements 18
Program to find the size of a Tuple
23. 19
Practical 1
Aim- Write a program to print Hello World.
print ("hello world!") #print hello world
OUTPUT:
Practical 2
Aim- Write a program to design the calculator.
a = int(input("Enter first element")) #input from user
b = int(input("Enter second element")) #input from user
choice = int(input("Enter your choice(1-4):\[Link]\[Link]\[Link]\[Link]\n\n"))
if choice == 1:
print(a+b) #addition of two elements
elif choice == 2:
print(a-b) #sustraction of two elements
elif choice == 3:
print(a*b) #multiplication of two elements
elif choice == 4:
print(a/b) #division of two elements
else:
print("invalid choice")
OUTPUT:
Practical 3
Aim - Write a program to find the square root of a number.
import math
a = float(input("Enter a number:")) #input from user for square root
sqrt_a = [Link](a) #sqrt(a) is pre defined function in math
print(f"the square root of {a} is {sqrt_a}")
OUTPUT:
Practical 4
Aim - Write a Python program to print the following string in a specific
format
print("Twinkle Twinkle Little Star,")
print("How I Wonder What You Are,")
print()
print("\tUp Above The World So High,")
print("\tLike A Diamond,In The Sky.")
print()
print("Twinkle Twinkle Little Star,")
print("How I Wonder What You Are,")
OUTPUT:
Practical 5
Aim - Write a program to swap two numbers using a third variable.
a = int(input("Enter first number: ")) #input from user for first number
b = int(input("Enter second number: ")) #input from user for second number
print("\nBefore swapping:")
print("a =", a)
print("b =", b)
temp = a #swapping numbers
a = b #swapping numbers
b = temp #swapping numbers
print("\nAfter swapping:")
print("a =", a)
print("b =", b)
OUTPUT:
Practical 6
Aim - Write a program to swap two numbers without using any third
variable.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("\nBefore swapping:")
print("a =", a)
print("b =", b)
a = a + b #swapping numbers
b = a – b #swapping numbers
a = a – b #swapping numbers
print("\nAfter swapping:")
print("a =", a)
print("b =", b)
OUTPUT:
Practical 7
Aim - Python Program to Check if a Number is Odd or Even.
a = int(input("Enter a number: "))
if a % 2 == 0:
print(f"{a} is Even")
else:
print(f"{a} is Odd")
OUTPUT:
Practical 8
Aim - Write a Python program to print the table of a number.
a = int(input("Enter a number: "))
print(f"\nMultiplication Table of {a}:\n")
for i in range (1,11):
print(f"{a} x {i} = {a * i}")
OUTPUT:
Practical 9
Aim - Write a Python program to print numbers from 1 to 10, skipping
even numbers and stopping at 7.
for i in range(1, 11):
if i % 2 == 0:
continue
if i == 7:
break
print(i)
OUTPUT:
Practical 10
Aim - Write a Python program that prints the first 10 integers and their
squares using a while loop.
i=1
while i < 11:
print(f"{i} = {i**2}")
i += 1
OUTPUT:
Practical - 11
Aim: Write a Python function to find factorial of a number.
def find_factorial(n):
if n<0:
return "Factorial is not defined for negative numbers."
factorial = 1
for i in range(1,n+1):
factorial*= i
return factorial
num =int(input("Enter a number: "))
result = find_factorial(num)
print(f"The factorial of {num} is : {result}")
OUTPUT:
Practical - 12
Aim: Write a Python function to find the Simple Interest of the number.
def simple_interest(p,r,t):
interest = p*r*t / 100
return interest
p = float(input("Enter the principal amount: "))
r = float(input("Enter the rate of interest: (in %) "))
t = float(input("Enter the time: (in years) "))
SI = simple_interest(p,r,t)
print(f"The Simple Interest is: {SI}")
OUTPUT:
Practical - 13
Aim: Write a Python function to calculate the sum of three given numbers,
if the values are equal then return thrice of their sum.
def custom_sum(a,b,c):
if a==b==c:
return 3 * (a+b+c)
else:
return (a+b+c)
a = int(input("Enter a number a: "))
b = int(input("Enter a number b: "))
c = int(input("Enter a number c: "))
S = custom_sum(a,b,c)
print(f"\nThe result is: {S}")
OUTPUT:
Practical - 14
Aim: Write a python function to Concatenate two strings.
string1 = "Hello, "
string2 = "Elias!"
result = string1 + string2
print(result)
OUTPUT:
Practical - 15
Aim: Write a Python program to demonstrate the usage of input,output
and error modules.
import sys
print("Enter some text (type 'exit' to quit):")
for line in [Link]:
line = [Link]()
if line == "exit":
[Link]("Exiting program...\n")
break
elif line == "":
[Link]("Error: You entered an empty line!\n")
else:
[Link](f"Output: {line}\n")
OUTPUT:
Practical - 16
Aim: Write a Python program to demonstrate the usage of the Maths
module by converting the radians to degree and degree to radians.
import math
print("1. Convert Radians to Degrees")
print("2. Convert Degrees to Radians")
choice = int(input("Enter your choice (1 or 2): "))
if choice == 1:
radians_value = float(input("Enter value in radians: "))
degrees_value = [Link](radians_value) #Convert radians to degree
print(f"{radians_value} radians is equal to {degrees_value} degrees")
elif choice == 2:
degrees_value = float(input("Enter value in degrees: "))
radians_value = [Link](degrees_value) #Convert degree to radians
print(f"{degrees_value} degrees is equal to {radians_value} radians")
else:
print("Invalid choice! Please enter 1 or 2.")
OUTPUT:
Practical 17
Aim : Program to show the usage of dir() function
example = "Hello"
print("Attributes and methods for string objects are:")
print(dir(example))
OUTPUT:
Practical 18
Aim : Program to check if a number is within a given range
import math
num = int(input("Enter a number: "))
lower = 10
upper = 50
if num in range(lower, upper + 1):
print(f"{num} is within the range {lower} to {upper}")
else:
print(f"{num} is outside the range {lower} to {upper}")
OUTPUT :
Practical 19
Aim : Program to show the usage of time module
import time
print("Time in seconds since epoch:", [Link]())
print("Local time:", [Link]())
print("Waiting for 2 seconds...")
[Link](2)
print("Program resumed!")
OUTPUT:
Practical : 20
Aim : Program to subtract five days from the current dat
from datetime import date, timedelta
today = [Link]()
print("Current date:", today)
new_date = today - timedelta(days=5)
print("Date after subtracting 5 days:", new_date)
Practical : 21
Aim : Program to count positive and negative numbers in a list
numbers = [10, -5, 7, -3, 0, 12, -8]
positive_count = 0
negative_count = 0
for num in numbers:
if num > 0:
positive_count += 1
elif num < 0:
negative_count += 1
print("List:", numbers)
print("Count of positive numbers:", positive_count)
print("Count of negative numbers:", negative_count)
OUTPUT :
Practical : 22
Aim : Program to find all possible combinations of a list with three
elements
import itertools
my_list = [1, 2, 3]
combinations = list([Link](my_list, 3))
print("List:", my_list)
print("All possible combinations (3 elements):")
for combo in combinations:
print(combo)
OUTPUT :
Practical : 23
Aim : Program to find the size of a Tuple
my_tuple = (10, 20, 30, 40, 50)
print("Tuple:", my_tuple)
size = len(my_tuple)
print("Size of the tuple is:", size)