0% found this document useful (0 votes)
67 views24 pages

Python 27

This document contains 10 sections that demonstrate various Python programming concepts through code examples and output. The sections cover working with numbers, strings, tuples, sets, lists, dictionaries, functions, modules, file handling, object-oriented programming, exception handling, and regular expressions. Each section includes sample Python code to demonstrate the concept and the corresponding output from running the code.

Uploaded by

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

Python 27

This document contains 10 sections that demonstrate various Python programming concepts through code examples and output. The sections cover working with numbers, strings, tuples, sets, lists, dictionaries, functions, modules, file handling, object-oriented programming, exception handling, and regular expressions. Each section includes sample Python code to demonstrate the concept and the corresponding output from running the code.

Uploaded by

Naveenesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

20921P08027 PERARASU.

1. WORKING WITH NUMBER

PROGRAM

a=10

b=5

c=a+b

print("addition:",c)

d=5

e=3

f=d-e

print("subtraction:",f)

g=10

h=2

i=g//h

print("division:",i)

j=8

k=5

l=j*k

print("multiplication:",l)

m=20

n=5

o=m%n

print("modulus:",o)

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

2. IMPLEMENTING STRING OPERATIONS

PROGRAM

my_str='hello'

print(my_str)

my_str="hello\ to the world"

print(my_str)

my_str="'hello to the python'"

print(my_str) my_str="""hello,Welcome to the

world of python"""

print(my_str)

print(my_str.upper())

print(my_str.lower())

print(my_str.find('llo'))

print(my_str.replace('hello','happy'))

print(my_str.split())

my_str1='this is my world'

my_str="harsha"

print(my_str+my_str1)

print(my_str*10)

my_str2=input("enter the string")

rev_str="".join(reversed(my_str2))

if

my_str2==rev_str:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

print("the string is palindrome")

else:

print("the string is not palindrome")

my_str="my first program in python"

count=0

for letter in my_str:

if(letter=='i'):

count+=1

print(count,'letters found')

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

3. WORKING WITH TUPLES AND SET

A). TUPLE

tuple1 = 1, 2, "Hello"

print(tuple1)

print(type(tuple1)) i, j, k = tuple1

print(i, j, k)

tuple1 = ('P', 'Y', 'T', 'H', 'O', 'N')

print(len(tuple1))

sample_tuple = tuple((1, 2, 3, "Hello", [4, 8,

16]))

for item in sample_tuple:

print(item)

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

B) SET

color_set={'red', 'orange', 'yellow', 'white', 'black'}

color_set.remove('yellow')

print(color_set)

color_set.discard('white')

print(color_set)

deleted_item = color_set.pop()

print(deleted_item) color_set.clear()

print(color_set)

del color_set

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

5. DEMONSTRATING LIST OPERATIONS

PROGRAM
iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print('iList: ',iList)
print('first element: ',iList[0])
print('fourth element: ',iList[3])
print('iList elements from 0 to 4 index:',iList[0: 5])
print('3rd or -7th element:',iList[-7])
iList.append(111)
print('iList after append():',iList)
print('index of \'80\': ',iList.index(80))
iList.sort()
print('after sorting: ', iList);

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

4.IMPLEMENTATION OF DICTIONARY

PROGRAM

y={'arasu':23,'nivi':20,ammu:21,'tamil':22}

l=list(y.items())

l.sort()

print('Ascending order is',l)

l=list(y.items())

l.sort(reverse=True)

print('Descending order is',l)

dict=dict(l)

print("Dictionary",dict)

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

6. FLOW CONTROL AND FUNCTIONS


PROGRAM

for i in range(1, 11):

print(i)

number = 6 if number >

5: # Calculate square

print(number * number)

print('Next lines of code')

def user_check(choice):

if choice == 1:

print("Admin")

elif choice == 2:

print("Editor")

elif choice == 3:

print("Guest") else:

print("Wrong entry")

user_check(1)

user_check(2)

user_check(3)

user_check(4)

password = input('Enter password ')

if password == "PYnative@#29":

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

print("Correct password")

else:

print("Incorrect Password") num1 =

int(input('Enter first number ')) num2 =

int(input('Enter second number ')) if

num1 >= num2: if num1 == num2:

print(num1, 'and', num2, 'are equal')

else:

print(num1, 'is greater than', num2)

else:

print(num1, 'is smaller than', num2)

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

7. MODULES AND PACKAGE

PROGRAM

import math

print(math.sqrt(25))

print(math.pi)

print(math.degrees(2))

print(math.radians(60))

print(math.sin(2))

print(math.cos(0.5))

print(math.tan(0.23))

print(math.factorial(4)) import

random

print(random.randint(0, 5))

print(random.random())

print(random.random() * 100)

List = [1, 4, True, 800, "python", 27, "hello"]

import datetime from datetime import date

print(date.fromtimestamp(454554))

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

8. FILE HANDLING

PROGRAM

F=open("drinks.dat","r")

count=0

while(True):

b=F.read(1)

if(b=='\n'):

count+=1

if(b==""):

break

print(b,end="")

print("Line Count " , count)

F.close()

ADDITIONAL FILE FOR IMPLEMENTATION

FILE: drinks.dat

Red Bull
Cafe Mocha
Americano
Coca Cola
Tea

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

9. OBJECT ORIENTED PROGRAMMING

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

PROGRAM
class Message(object):
def __init__(self):
self.msg=None
def assignValue(self):
self.msg="Hello welcome to python programming"
def getValue(self,str):
self.msg=str def
printValue(self):
print("msg=",self.msg)
M=Message()
print("value after init.the object...")
M.printValue(); M.assignValue();
print("value after assignValue()..")
M.printValue();
M.getValue("How are you?") print("value
after getValue()...")
M.printValue();

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

10. EXCEPTION HANDLING AND REGULAR EXPRESSIONS

A) EXCEPTION HANDLING
import sys
randomList=['a',0,2]
for entry in randomList:
try:
print("The entry is",entry)
r=1/int(entry)
break except:
print("Oops!",sys.exc_info()[0],"occured.")
print("Next entry.") print()
print("The reciprocal of",entry,"is",r)

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

OUTPUT:

B)REGULAR EXPRESSIONS

DCA42 MCA, SIASC PYTHON PROGRAMMING


20921P08027 PERARASU.M

import re txt="No rain in Spain"


x=re.search("^The.*Spain$",txt)
if x:
print("No match")
else:
print("YES! we hava a match!")

OUTPUT:

DCA42 MCA, SIASC PYTHON PROGRAMMING

You might also like