1.
Control Structures of Python
Program 1:
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80 and avg<90):
print("Grade: B")
elif(avg>=70 and avg<80):
print("Grade: C")
elif(avg>=60 and avg<70):
print("Grade: D")
else:
print("Grade: F")
Output :
Enter marks of the first subject: 88
Enter marks of the second subject: 99
Enter marks of the third subject: 97
Enter marks of the fourth subject: 96
Enter marks of the fifth subject: 97
Grade: A
2. Different Types of Structures (List, Dictionary, Tuples)
Program 2
print("List Creation with Append Function\n")
numbers = [21, 34, 54, 12]
print("Before Append:", numbers)
[Link](32)
print("After Append:", numbers)
print("\n\n")
print("Different types of tuples")
print("Empty Tuple")
my_tuple = ()
print(my_tuple)
print("Tuple having Integer")
my_tuple = (1, 2, 3)
print(my_tuple)
print("Tuple with mixed datatype")
my_tuple = (1, "Hello", 3.4)
print(my_tuple)
print("Nested Tuple")
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
print("\n\n")
print("Dictionary Creation")
Dictionary = dict({1: 'Python', 2: 'Javatpoint', 3:'Dictionary'})
print("\nDictionary created by using dict() method: ")
print(Dictionary)
Dictionary = dict([(1, 'Javatpoint'), (2, 'Python'), (3, 'Dictionary')])
print("\nDictionary with key:value pair format: ")
print(Dictionary)
Output:2
List Creation with Append Function
Before Append: [21, 34, 54, 12]
After Append: [21, 34, 54, 12, 32]
Different types of tuples
Empty Tuple
()
Tuple having Integer
(1, 2, 3)
Tuple with mixed datatype
(1, 'Hello', 3.4)
Nested Tuple
('mouse', [8, 4, 6], (1, 2, 3))
Dictionary Creation
Dictionary created by using dict() method:
{1: 'Python', 2: 'Javatpoint', 3: 'Dictionary'}
Dictionary with key:value pair format:
{1: 'Javatpoint', 2: 'Python', 3: 'Dictionary'}
3. Working of Exception Handling and Assertions
Program 3
try:
div = 4 // 0
print( div )
except ZeroDivisionError:
print( "Attempting to divide by zero" )
finally:
print( 'This is code of finally clause' )
Output 3
Attempting to divide by zero
This is code of finally clause
[Link] Structure Algorithms Using Python Searching and Sorting
Program 4
def binary_search(item_list,item):
first=0
last=len(item_list)-1
found=False
while(first<=last and not found):
mid=(first+last)//2
if item_list[mid]==item:
found=True
else:
if item<item_list[mid]:
last=mid-1
else:
first=mid+1
return found
print(binary_search([1,2,3,5,8],6))
print(binary_search([1,2,3,5,8],5))
Output 4
False
True
5. Functions Scoping, Recursion and List Mutability
Program 5
def lcm(a,b):
[Link]=[Link]+b
if(([Link]%a==0) and ([Link]%b==0)):
return [Link];
else:
lcm(a,b)
return [Link]
[Link]=0
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
if(a>b):
LCM=lcm(b,a)
else:
LCM=lcm(a,b)
print(LCM)
Output 5
Enter first number:77
Enter second number:55
385
[Link] Expressions
Program 6
import re
txt = "The rain in Spain"
x = [Link]("Spain", txt)
print(x)
if (x):
print("Yes, there is at least one match!")
else:
print("No match")
Output 6
['Spain']
Yes, there is at least one match!
Graphs Using PyPlot
Program 7
import [Link] as plt
days = [1, 2, 3, 4, 5]
slices = [7,2,2,13]
cols = ['r','y','g','b']
my_labels = ["Sleeping ", "Eating", "Working", "Playing"]
[Link](slices,
labels=my_labels,
colors = cols,
startangle=45,
explode =(0,0.2,0,0),
shadow = True,
autopct = '%1.1f%%')
[Link]('equal')
[Link](loc=3)
[Link]()
Output 7
[Link] Ciphers
Program 8
def encypt_func(txt, s):
result = ""
for i in range(len(txt)):
char = txt[i]
if ([Link]()):
result += chr((ord(char) + s - 64) % 26 + 65)
else:
result += chr((ord(char) + s - 96) % 26 + 97)
return result
txt = "CEASER CIPHER EXAMPLE"
s=4
print("Plain txt : " + txt)
print("Shift pattern : " + str(s))
print("Cipher: " + encypt_func(txt, s))
Output 8
Plain txt : CEASER CIPHER EXAMPLE
Shift pattern : 4
Cipher: HJFXJWsHNUMJWsJCFRUQJ