0% found this document useful (0 votes)
16 views6 pages

pythonpraexam

The document contains a series of Python programs that demonstrate various programming concepts, including calculating areas and perimeters, checking even/odd numbers, finding the largest number, grading based on marks, and performing operations on lists, tuples, and sets. It also includes examples of handling exceptions, file operations, and using modules like math and numpy. Additionally, there are scripts for user interaction with hardware components like an LED using Raspberry Pi GPIO.
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)
16 views6 pages

pythonpraexam

The document contains a series of Python programs that demonstrate various programming concepts, including calculating areas and perimeters, checking even/odd numbers, finding the largest number, grading based on marks, and performing operations on lists, tuples, and sets. It also includes examples of handling exceptions, file operations, and using modules like math and numpy. Additionally, there are scripts for user interaction with hardware components like an LED using Raspberry Pi GPIO.
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/ 6

Python Program List

Write a python program to calculate area and perimeter of the


square.
side = float(input("Enter the side of the square: "))
area = side ** 2
perimeter = 4 * side
print("Area:", area)
print("Perimeter:", perimeter)

Write a program to find the area of Rectangle and Circle by


taking input from the user.
import math
length = float(input("Enter length of rectangle: "))
breadth = float(input("Enter breadth of rectangle: "))
area_rectangle = length * breadth
radius = float(input("Enter radius of circle: "))
area_circle = math.pi * radius ** 2
print("Area of Rectangle:", area_rectangle)
print("Area of Circle:", area_circle)

Write a program to check whether a number is even or odd


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

Write a program to check the largest number among the three


numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
largest = max(a, b, c)
print("Largest number is:", largest)
Write a program that takes the marks of 5 subjects and displays
the grade
marks = [int(input(f"Enter mark {i+1}: ")) for i in range(5)]
avg = sum(marks)/5
if avg >= 90:
grade = "A"
elif avg >= 75:
grade = "B"
elif avg >= 60:
grade = "C"
elif avg >= 40:
grade = "D"
else:
grade = "F"
print("Grade:", grade)

Print the following patterns using loop:


*
**
***
****
for i in range(1, 5):
print("* " * i)

Write a Python program to print all even numbers between 1 to


100 using while loop.
i = 1
while i <= 100:
if i % 2 == 0:
print(i, end=" ")
i += 1

Write a Python program to calculate factorial of a number


num = int(input("Enter a number: "))
fact = 1
for i in range(1, num+1):
fact *= i
print("Factorial:", fact)
Write a Python program to print Fibonacci series.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Write a Python program to sum all the items in a list, maximum


and minimum number in the list.
lst = [int(x) for x in input("Enter numbers separated by space:
").split()]
print("Sum:", sum(lst))
print("Max:", max(lst))
print("Min:", min(lst))

Create a tuple and find the minimum and maximum number


from it and find all the elements in the Tuple.
tpl = tuple(int(x) for x in input("Enter tuple elements separated
by space: ").split())
print("Tuple:", tpl)
print("Min:", min(tpl))
print("Max:", max(tpl))

Write a Python program to perform following operations on set:


intersection of sets, union of sets, set difference, symmetric
difference, clear a set.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print("Union:", A | B)
print("Intersection:", A & B)
print("Difference A-B:", A - B)
print("Symmetric Difference:", A ^ B)
A.clear()
print("Cleared Set A:", A)
Write a Python script to concatenate following dictionaries to
create a new one.
Sample Dictionary:
dic1 = {1:10, 2:20}
dic2 = {3:30, 4:40}
dic3 = {5:50,6:60}
dic1 = {1:10, 2:20}
dic2 = {3:30, 4:40}
dic3 = {5:50,6:60}
result = {**dic1, **dic2, **dic3}
print(result)

Write a Python function that accepts a string and calculate the


number of upper case letters and lower case letters.
def count_case(s):
upper = sum(1 for c in s if c.isupper())
lower = sum(1 for c in s if c.islower())
print("Uppercase letters:", upper)
print("Lowercase letters:", lower)
count_case(input("Enter a string: "))

Write a Python program to create a user defined module that


will ask your college name and will display the name of the
college.
# mymodule.py
def ask_college():
name = input("Enter your college name: ")
print("College Name:", name)
# In main file
# import mymodule
# mymodule.ask_college()
Write a Python program that will calculate area and
circumference of circle using inbuilt Math Module
import math
r = float(input("Enter radius: "))
area = math.pi * r ** 2
circumference = 2 * math.pi * r
print("Area:", area)
print("Circumference:", circumference)

Write a Python program to create two matrices and perform


addition, subtraction, multiplication and division operation on
matrix.
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
print("Addition:\n", A + B)
print("Subtraction:\n", A - B)
print("Multiplication:\n", A * B)
print("Division:\n", A / B)

Write a Python program to Check for ZeroDivisionError


Exception.
try:
a = int(input("Enter numerator: "))
b = int(input("Enter denominator: "))
print("Result:", a / b)
except ZeroDivisionError:
print("Division by zero is not allowed.")
Write a Python program to open a file using with …open. And
write the content to the file.
with open("output.txt", "w") as f:
f.write("This is a sample content.")
print("Content written to file.")

Write a Python script to allow a user to turn the LED on and off.
# Assuming Raspberry Pi with GPIO
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
cmd = input("Enter 'on' to turn on LED, 'off' to turn off,
'exit' to quit: ")
if cmd == 'on':
GPIO.output(18, True)
elif cmd == 'off':
GPIO.output(18, False)
elif cmd == 'exit':
break
GPIO.cleanup()

You might also like