Python Record 1
Python Record 1
Program:
# Constants
PI = 3.14159
# Variables
name = input("Enter your name: ")
radius = float(input("Enter the radius of a circle: "))
# Calculations
circumference = 2 * PI * radius
area = PI * radius ** 2
# Output
print("Name:", name)
print("Radius:", radius)
print("Circumference:", circumference)
print("Area:", area)
Output
Enter your name: Chandresh
Enter the radius of a circle: 3.23
Name: Chandresh
Radius: 3.23
Circumference: 20.2946714
Area: 32.775894311
Ex no:2 Program using Operators in Python
Program:
# Arithmetic operators
num1 = 10
num2 = 5
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Modulus:", modulus)
print("Exponentiation:", exponentiation)
print("Floor Division:", floor_division)
# Comparison operators
num1 = 10
num2 = 5
# Logical operators
is_raining = True
is_sunny = False
# Assignment operators
x = 10
y=5
x += y
print("x after x += y:", x)
x -= y
print("x after x -= y:", x)
x *= y
print("x after x *= y:", x)
x /= y
print("x after x /= y:", x)
x %= y
print("x after x %= y:", x)
x **= y
print("x after x **= y:", x)
x //= y
print("x after x //= y:", x)
# Bitwise operators
a = 60 # 60 in binary is 00111100
b = 13 # 13 in binary is 00001101
Output :
Enter Your Choice (F-Fahrenheit to Celsius ,C-Celsius to Fahrenheit)C
Enter the Celsius Value : 56
56.0 Celsius equal to 132.8 Fahrenheit
Ex no:3 .2-Program using Conditional Statements
# Program used to find the grade of a student
print("Please enter the marks of")
m1=int(input("Subject 1 :"))
m2=int(input("Subject 2 :"))
m3=int(input("Subject 3 :"))
m4=int(input("Subject 4 :"))
m5=int(input("Subject 5 :"))
tot_marks=m1+m2+m3+m4+m5
percent=tot_marks/5
if percent>=80:
grade="A"
elif percent>=70 and percent<80:
grade="B"
elif percent>=60 and percent<70:
grade="B"
elif percent>=40 and percent<60:
grade="B"
else:
grade="E"
print("Total Marks : ",tot_marks)
print("Percentage : ",percent)
print("Grade : ",grade)
Output:
Please enter the marks of
Subject 1 :53
Subject 2 :45
Subject 3 :89
Subject 4 :67
Subject 5 :76
Total Marks : 330
Percentage : 66.0
Grade : B
Ex no:4 Program using Loops
(a). While Loop – Factorial Calculation
Program :
# Program to calculate factorial
n=int(input("Enter a positive integer : "))
if(n<0):
print("No factorial for negative numbers")
elif(n==0):
print("The factorial for 0 is 1")
else:
f=1
i=1
while(i<=n):
f=f*i
i=i+1
print("The factorial of ",n, "is ",f)
Output 1:
Enter a positive integer : 0
The factorial for 0 is 1
Output 2:
Enter a positive integer : 8
The factorial of 8 is 40320
Output 3:
Enter a positive integer : -9
No factorial for negative numbers
(b). For Loop – Factorial Calculation
Program :
# Program to calculate factorial using 'for' loop
n=int(input("Enter a positive integer : "))
if(n<0):
print("No factorial for negative numbers")
elif(n==0):
print("The factorial for 0 is 1")
else:
f=1
for i in range(1,n+1):
f=f*i
print("The factorial of ",n, "is ",f)
Output 1:
Enter a positive integer : 0
The factorial for 0 is 1
Output 2:
Enter a positive integer : 7
The factorial of 7 is 5040
Output 3:
Enter a positive integer : -11
No factorial for negative numbers
Ex no:5 Program using Jump Statements
(a). Break Statement
Program :
# Use of break statement inside the loop
for val in "University":
if val == "r":
break
print(val)
print("The end")
Output :
U
n
i
v
e
The end
(b). Continue Statement
Program :
# Use of continue statement inside the loop
for val in "University":
if val == "r":
continue
print(val)
print("The end")
Output :
U
n
i
v
e
s
i
t
y
The end
(c). Pass Statement
Program :
# Use of pass statement inside the loop
for val in "University":
pass
print("The end")
Output :
The end
Ex no:6 Program using Functions
(a). Function returning single value
Program :
# Program to illustrate find and return factorial value
def fact(n1):
f=1
for i in range(1,n1+1):
f=f*i
return(f)
Output :
Enter a positive integer value : 6
The factorial value of 6 is 720
(b). Function returning multiple values
Program :
# Program to illustrate multiple return values
def minmax(n):
val=float(input("Enter a value : "))
ma=mi=val
for i in range(1,n):
val=float(input("Enter a value : "))
if ma<val:
ma=val
elif mi>val:
mi=val
return(mi,ma)
Output :
Enter 'n' value: 6
Enter a value : 12
Enter a value : 32
Enter a value : 43
Enter a value : 54
Enter a value : 654
Enter a value : -34
Minimum is -34.0 Maximum is 654.0
Ex no:7 Program using Recursion
Program :
# Recursive Function - Factorial
def Factorial(n):
if n==0:
return 1
else:
return n * Factorial(n-1) # recursive call
Output :
FACTORIAL - RECURSIVE
Enter number to find factorial : 8
Factorial of 8 is : 40320
Ex no:8 Program using Arrays
Program:
#Program to sort the list of elements using array
import array
def array_sort(arr):
# Convert the array to a list for sorting
arr_list = arr.tolist()
arr_list.sort()
return sorted_arr
# Example usage
n=int(input("Enter the number of elements to be sorted : "))
x=float(input("Enter the 0 element : "))
arr = array.array('f', [x])
for i in range(1,n):
str1="Enter the "+str(i)+"th element : "
x=float(input(str1))
arr.append(x)
sorted_arr = array_sort(arr)
Output :
Enter the number of elements to be sorted : 5
Enter the 0 element : 12
Enter the 1th element : 4
Enter the 2th element : -9
Enter the 3th element : 18
Enter the 4th element : 11
-9.0 4.0 11.0 12.0 18.0
Ex no:9 Program using Strings – Palindrome checking
Program:
def is_palindrome(string):
# Remove spaces and convert to lowercase for case-insensitive comparison
string = string.replace(" ", "").lower()
length = len(string)
# Example usage
input_string = input("Enter a string: ")
if is_palindrome(input_string):
print("The given string '",input_string,"' is a palindrome.")
else:
print("The given string '",input_string,"' is not a palindrome.")
Output 1 :
Enter a string: amma
The given string ' amma ' is a palindrome.
Output 2 :
Enter a string: god
The given string ' god ' is not a palindrome.
Ex no:10 Program using Modules
(a). Built in modules
Program:
import datetime
import random
import math
Output :
Current Date and Time: 2023-07-15 10:00:49.671686
Random Number: 3
Square Root of 16 : 4.0
(b). User-defined modules
Program:
• User defined Module
# Fibonacci and Factorial module 'myMod.py'
def fact(n):
f=1
if(n==0):
return(1)
else:
for i in range(1,n+1):
f=f*i
return(f)
• Main Program
import myMod
# Accessing elements
print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])
print("Slice of fruits:", fruits[1:3])
# Modifying elements
fruits[1] = "blueberry"
print("Modified fruits list:", fruits)
# Adding elements
fruits.append("grape")
print("Fruits list after appending:", fruits)
# Removing elements
removed_fruit = fruits.pop(2)
print("Removed fruit:", removed_fruit)
print("Fruits list after removing:", fruits)
# Concatenating tuples
more_colors = ("orange", "purple")
all_colors = colors + more_colors
print("All colors:", all_colors)
try:
with open(file_name, 'w') as file:
file.write(content)
print("File write operation completed successfully!")
except IOError as e:
print(f"Unable to write to the file. {e}")
Output :
File write operation completed successfully!
File read operation completed successfully!
File content:
This is some sample text that will be written to the file.