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

Python Practical File

The document contains 15 Python programs submitted for a Bachelor of Technology degree. The programs cover topics like adding numbers, calculating factorials, simple and compound interest, checking Armstrong numbers, finding circle area, printing prime numbers in a range, working with dictionaries, making a basic calculator, printing odd numbers in a range, finding string length, and more.

Uploaded by

dietwithdubey
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)
47 views

Python Practical File

The document contains 15 Python programs submitted for a Bachelor of Technology degree. The programs cover topics like adding numbers, calculating factorials, simple and compound interest, checking Armstrong numbers, finding circle area, printing prime numbers in a range, working with dictionaries, making a basic calculator, printing odd numbers in a range, finding string length, and more.

Uploaded by

dietwithdubey
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/ 19

A

Laboratory File
On
Python (VAC)

Submitted
For
Bachelor of Technology
In
Computer Science & Engineering
Batch : 2022-2026

At

Aravali College of Engineering & Management

Submitted To Submitted By
Mrs. Sudha Ma’am Aditya Kumar Dubey
Assistant Professor CSE 2nd Year – A
CSE Roll No. : 22011004003
Aditya Kumar Dubey (22011004003)

Python

INDEX

Sr.No. Name of Program Page Signature


No.
1. Write a program to add Two Numbers. 1
2. Write a Program for factorial of a number. 2
3. Write a program for Simple Interest and Compound Interest. 3
4. Write a program to check Armstrong Number. 4
5. Write a program to Find area of a Circle. 5
6. Write a program to print all Prime Numbers in an Interval. 6
7. Demonstrate the different ways of creating dictionary 7
objects with suitable example programs. Demonstrate the
following functions/methods which operates on disctonary
in Python with suitable examples: i) dict() ii) len() iii) clear()
iv) get() v) pop() vi) pop item() vii) keys() viii) values() ix)
items() x) copy() xi) update().
8. Write a program to make a Simple Calculator 9
9. Write a Program to print All Odd numbers in a range. 11
10. Write a program to print positive numbers in a list. 12
11. Write a program for merging two Dictionaries. 13
12. Write a Program to find a length of a String in Python 14
13. Write a program to print duplicates from a list of integers. 15
14. Write a Program to generate Random Numbers form 1 to 20 16
and Append them to the List.
15. Write a program to find Sum of Array. 17
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 1
Write a python Program to add two numbers .

# This program adds two numbers

def add_two_numbers(num1, num2):


return num1 + num2

# Get input from user


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Call function to add two numbers


result = add_two_numbers(num1, num2)

# Print the result


print("The sum of {0} and {1} is {2}".format(num1, num2, result))

OutPut :
Enter first number: 3
Enter second number: 7
The sum of 3 and 7 is 10

Page No.1
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]

Program - 2
Write a python program for a factorial of a number.

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

# Get input from user


num = int(input("Enter a non-negative integer: "))

# Check if input is valid


if num < 0:
print("Invalid input. Please enter a non-negative integer.")
else:
# Call function to calculate factorial
result = factorial(num)

# Print the result


print("The factorial of", num, "is", result)

OutPut :
Enter a non-negative integer: 5
The factorial of 5 is 120

Page No.2
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 3
Write a program for Simple interest and Compound Interest.
def simple_interest(p, r, t):
return (p * r * t) / 100
def compound_interest(p, r, t, n):
return p * (1 + r/n)**(n*t) - p
# Get input from user
principal = float(input("Enter the principal amount in Rupees: "))
rate = float(input("Enter the annual interest rate (as a percentage): "))
time = float(input("Enter the time in years: "))
compound_times = int(input("Enter the number of times interest is compounded per year: "))

# Calculate simple interest


si = simple_interest(principal, rate, time)

# Calculate compound interest


ci = compound_interest(principal, rate, time, compound_times)

# Print the results


print("The simple interest is: Rs.{:.2f}".format(si))
print("The compound interest is: Rs.{:.2f}".format(ci))

OutPut :
Enter the principal amount in Rupees: 1000
Enter the annual interest rate (as a percentage): 5
Enter the time in years: 2
Enter the number of times interest is compounded per year: 4
The simple interest is: Rs.100.00
The compound interest is: Rs.105.13

Page No.3
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 4
Write a python program to check Armstrong number.
def is_armstrong(n):
# Convert the number to a string
num_str = str(n)
# Calculate the sum of the cubes of each digit
sum_cubes = sum(int(digit)**len(num_str) for digit in num_str)
# Check if the sum of the cubes is equal to the original number if
sum_cubes == n:
return True
else:
return False

# Get input from user


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

# Check if the number is an Armstrong number


if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")

OutPut :
Enter a number: 153
153 is an Armstrong number

Page No.4
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 5
Write a Phyton program to find area of a circle.
import math
def area_of_circle(radius):
return math.pi * (radius**2)

# Get input from user


radius = float(input("Enter the radius of the circle: "))

# Calculate the area of the circle


area = area_of_circle(radius)

# Print the result


print("The area of the circle with radius", radius, "is", area)

OutPut :
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is 78.53981633974483

Page No.5
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program – 6
Write a Python Program to print all Prime Numbers In an Interval.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def print_primes_in_interval(start, end):
for num in range(start, end+1):
if is_prime(num):
print(num)
# Get input from user
start = int(input("Enter the starting number of the interval: "))
end = int(input("Enter the ending number of the interval: "))
# Print all the prime numbers in the interval
print_primes_in_interval(start, end)
OutPut :
Enter the starting number of the interval: 10
Enter the ending number of the interval: 50
11
13
17
19
23
29
31
37
41
43
47

Page No.6
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 7
Demonstrate the different ways of creating dictionary object with suitable example programmes.
Demonstrate the following functions/methods which operators on dictionary in python with suitable
examples: i)dict() ii)Len() iii)clear() iv)get() v)pop() vi)pop item() vii)keys() viii)values() ix)items()
x)copy() xi)update().

# Creating dictionary object using dict()


dict1 = dict([('a', 1), ('b', 2), ('c', 3)])
print("Dictionary created using dict():", dict1)

# Creating dictionary object using curly braces {}


dict2 = {'a': 1, 'b': 2, 'c': 3}
print("Dictionary created using curly braces: ", dict2)

# Creating dictionary object using ** operator


dict3 = {**dict1, **dict2}
print("Dictionary created using ** operator: ", dict3)

# Using len() function to get the number of key-value pairs in a dictionary


print("Number of key-value pairs in dict3:", len(dict3))

# Using clear() method to remove all key-value pairs from a dictionary


dict3.clear()
print("Dictionary after clearing all key-value pairs: ", dict3)

# Using get() method to retrieve the value associated with a key


print("Value associated with key 'a' in dict2: ", dict2.get('a'))

# Using pop() method to remove a key-value pair and retrieve the value
print("Value associated with key 'b' in dict2 before pop(): ", dict2.get('b'))
dict2.pop('b')
print("Value associated with key 'b' in dict2 after pop(): ", dict2.get('b'))

# Using popitem() method to remove and retrieve a random key-value pair


print("Key-value pair popped from dict2: ", dict2.popitem())

Page No.7
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
# Using keys() method to get a list of all keys in a dictionary
print("Keys in dict2: ", list(dict2.keys()))

# Using values() method to get a list of all values in a dictionary


print("Values in dict2: ", list(dict2.values()))

# Using items() method to get a list of all key-value pairs in a dictionary


print("Key-value pairs in dict2: ", list(dict2.items()))

# Using copy() method to create a shallow copy of a dictionary


dict4 = dict2.copy()
print("Dictionary after creating a shallow copy: ", dict4)

# Using update() method to update a dictionary with the key-value pairs from another
dictionary
dict5 = {'d': 4, 'e': 5}
dict4.update(dict5)
print("Dictionary after updating with dict5: ", dict4)

OutPut :
Dictionary created using dict(): {'a': 1, 'b': 2, 'c': 3}
Dictionary created using curly braces: {'a': 1, 'b': 2, 'c': 3}
Dictionary created using ** operator: {'a': 1, 'b': 2, 'c': 3}
Number of key-value pairs in dict3: 0
Dictionary after clearing all key-value pairs: dict()
Value associated with key 'a' in dict2: 1
Value associated with key 'b' in dict2 before pop(): 2
Value associated with key 'b' in dict2 after pop(): None
Key-value pair popped from dict2: ('c', 3)
Keys in dict2: ['a']
Values in dict2: [1]
Key-value pairs in dict2: [('a', 1)]
Dictionary after creating a shallow copy: {'a': 1}
Dictionary after updating with dict5: {'a': 1, 'd': 4, 'e': 5}

Page No.8
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]

Program - 8
Write a Python program to Make a simple Calculator.
def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
raise ValueError("Cannot divide by zero")
return x / y

# Get input from user


num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

# Perform calculation based on operator


if operator == "+":
result = add(num1, num2)

elif operator == "-":


result = subtract(num1, num2)

elif operator == "*":


result = multiply(num1, num2)

Page No.9
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
elif operator == "/":
result = divide(num1, num2)
else:
print("Invalid operator")
result = None
# Print the result
if result is not None:
print("Result: ", result)

OutPut :
Enter first number: 10
Enter operator (+, -, *, /): +
Enter second number: 5
Result: 15.0

Page No.10
Roll No.: 22011004003
22(22011kumKumz[2201
1004003]
Program - 9
Write a python Program to print all odd numbers in a range.
# Get input from user
start = int(input("Enter the starting number of the range: "))
end = int(input("Enter the ending number of the range: "))

# Print all the odd numbers in the range


for num in range(start, end+1):
if num % 2 != 0:
print(num)

OutPut :
Enter the starting number of the range: 10
Enter the ending number of the range: 20
11
13
15
17
19

Page No.11
Program – 10
Write a Python Program to print Positive numbers in a list.

numbers = [34, 1, 0, -23, 12, -88]


for num in numbers:
if num > 0:
print("Python:", num)

# Output:
# Python: 34
# Python: 1
# Python: 12

Page No.12
Program – 11
Write a Python Program to merging two dictonaries.

dict1 = {'a': 1, 'b': 2}


dict2 = {'c': 3, 'd': 4}

merged_dict = dict1.copy()
merged_dict.update(dict2)

print(merged_dict)

#OutPut
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Page No.13
Program – 12
Find length of a string in python.

string = "Hello, World!"


length = len(string)

print("Length of the string:", length)

#OutPut
Length of the string: 13

Page No.14
Program – 13
Write a program to print duplicates from a list of integers.

from collections import Counter


numbers = [5, 10, 15, 18, 20, 25, 30, 30, 40, 50, 50, 50]
counter = Counter(numbers)
duplicates = [item for item, count in counter.items() if count > 1]
print("Duplicates:")
for d in duplicates:
print(d)

#Output
Duplicates:
30
50

Page No.15
Program – 14
Write a program to generate Random numbers from 1 to 20 and Append them to the list.
import random
random_numbers = []
for i in range(10):
random_number = random.randint(1, 20)
random_numbers.append(random_number)
print("Randomized list is: ", random_numbers)

#Output
Randomized list is: [15, 18, 11, 19, 12, 14, 17, 16, 13, 10]

Page No.16
Program – 15
Write a program to find sum of Array.

numbers = [1, 2, 3, 4, 5]
sum = 0
for num in numbers:
sum += num
print("Sum of the array is: ", sum)

#outPut
Sum of the array is: 15

Page No.17

You might also like