0% found this document useful (0 votes)
6 views4 pages

Python Programming Examples and Exercises

The document contains various Python programming tasks including printing patterns, creating classes, performing arithmetic operations using modules, manipulating dictionaries, and file handling. It also includes examples of control flow statements, set operations, and class inheritance. Each task is illustrated with code snippets and expected outputs.

Uploaded by

sanskaruravane9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Python Programming Examples and Exercises

The document contains various Python programming tasks including printing patterns, creating classes, performing arithmetic operations using modules, manipulating dictionaries, and file handling. It also includes examples of control flow statements, set operations, and class inheritance. Each task is illustrated with code snippets and expected outputs.

Uploaded by

sanskaruravane9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Write a program to print following

1
1 2
1 2 3
1 2 3 4

for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()

Write a program to create class EMPLOYEE with ID and NAME and display its contents.
class employee :
id=0
name=""
def getdata(self,id,name):
[Link]=id
[Link]=name
def showdata(self):
print("ID :", [Link])
print("Name :", [Link])
e = employee()
[Link](11,"Vijay")
[Link]()

Write a program for importing module for addition and subtraction of two numbers
[Link]:
def add(x,y):
return (x+y)
def sub(x,y):
return (x-y)
[Link]:
import calculation
print([Link](1,2))
print([Link](4,2))

Write a program to create dictionary of student the includes their ROLL NO and NAME
i) Add three students in above dictionary
>>> dict1={1:"Vijay",2:"Santosh",3:"Yogita"}
>>>print(dict1)
{1: 'Vijay', 2: 'Santosh', 3: 'Yogita'}

ii) Update name=’Shreyas’ of ROLL NO=2


>>>dict1[2]="Shreyas"
>>>print(dict1)
{1: 'Vijay', 2: 'Shreyas', 3: 'Yogita'}

iii) Delete information of ROLL NO=1


>>>[Link](1)
‘Vijay'
>>>print(dict1)
{2: 'Shreyas', 3: 'Yogita'}

Write the output of the following


i) >>> a=[2,5,1,3,6,9,7]
>>> a[2:6]=[2,4,9,0]
>>> print(a)
Output: [2, 5, 2, 4, 9, 0, 7]
ii) >>> b=[“Hello”,”Good”]
>>> [Link](“python”)
>>>print(b)
Output: ['Hello', 'Good', 'python']
iii) >>>t1=[3,5,6,7] output:
>>>print(t1[2]) >>>6
>>>print(t1[-1]) >>>7
>>>print(t1[2:]) >>>[6, 7]
>>>print(t1[:]) >>>[3, 5, 6, 7]

Write a program to open a file in write mode and append some content at the end of
file
file1 = open("[Link]", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London"]
[Link](L)
[Link]()
# Append-adds at last
# append mode
file1 = open("[Link]", "a")
# writing newline character
[Link]("\n")
[Link]("Today")
# without newline character
[Link]("Tomorrow")
file1 = open("[Link]", "r")
print("Output of Readlines after appending")
print([Link]())
print()
[Link]()

Write python program to illustrate if else ladder.


i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")

Write Python code for finding greatest among four numbers.


list1 = [ ]
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
element = int(input("Enter elements: "))
[Link](element)
print("Largest element is:", max(list1))
Output:
Enter number of elements in list: 4
Enter elements: 10
Enter elements: 20
Enter elements: 45
Enter elements: 20
Largest element is: 45
Write python code to count frequency of each characters in a given file.
import collections
import pprint
file_input = input('File Name: ')
with open(file_input, 'r') as info:
count = [Link]([Link]().upper())
value = [Link](count)
print(value)

d) Write python program to read contents of [Link] and write same content to
[Link].
with open('[Link]','r') as firstfile, open('[Link]','w') as secondfile:
# read content from first file
for line in firstfile:
# write content to second file
[Link](line)

Write python program to perform following operations on Set (Instead of Tuple)


i) Create set
ii) Access set Element
iii) Update set
iv) Delete set

# To Create set
S={10,20,30,40,50}
# To Access Elements from set
print (S)
#To add element into set using add method
[Link](60)
print(S)
#To update set using update method
[Link](['A','B'])
print(S)
#To Delete element from Set using discard() method
[Link](30)
print(S)
#To delete element from set using remove() method
[Link]('A')
print(S)
#To delete element from set using pop() method
[Link]()
print(S)
output:
{50, 20, 40, 10, 30}
{50, 20, 40, 10, 60, 30}
{'B', 50, 20, 'A', 40, 10, 60, 30}
{'B', 50, 20, 'A', 40, 10, 60}
{'B', 50, 20, 40, 10, 60}
{50, 20, 40, 10, 60}
(Any other suitable example can consider)

Create suitable method for reading and printing students details.


class Student: def getStudentDetails(self): [Link]=input("Enter Roll Number :
") [Link] = input("Enter Name : ") [Link] =input("Enter Address : ") def
printStudentDetails(self): print([Link],[Link], [Link]) S1=Student()
[Link]() print("Student Details ") [Link] () Output:
Enter Roll Number : 001 Enter Name : ABC Enter Address : New York Student Details :
001 ABC New York

Create a parent class named Animals and a child class Herbivorous which will extend
the class Animal. In the child class Herbivorous over side the method feed ( ).
Create a object
# parent class class Animal: # properties multicellular = True # Eukaryotic means
Cells with Nucleus eukaryotic = True # function breath def breathe(self): print("I
breathe oxygen.")
# function feed def feed(self): print("I eat food.") # child class class
Herbivorous(Animal):
# function feed def feed(self): print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
[Link]() # calling some other function [Link]()

You might also like