Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
256 views

Python Project

The document contains 20 code snippets demonstrating various Python programming concepts like functions, loops, conditionals, strings, lists, dictionaries etc. The snippets include programs to check if a string is a palindrome, capitalize alternating characters of a string, Fibonacci series, power of numbers, reversing a string, validating email, finding second largest number in a tuple, student data in dictionary, uppercase to lowercase conversion and more.

Uploaded by

ShishirRanjan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
256 views

Python Project

The document contains 20 code snippets demonstrating various Python programming concepts like functions, loops, conditionals, strings, lists, dictionaries etc. The snippets include programs to check if a string is a palindrome, capitalize alternating characters of a string, Fibonacci series, power of numbers, reversing a string, validating email, finding second largest number in a tuple, student data in dictionary, uppercase to lowercase conversion and more.

Uploaded by

ShishirRanjan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

string=raw_input('enter a string')
length=len(string)
mid=length/2
rev=-1
for a in range(mid):
if string[a]==string[rev]:
a+=1
rev-=1
else:
print string, 'it is not a pallindrome'
break
else:
print string, 'it is a pallindrome'
outputenter a stringnitin
nitin it is a pallindrome
>>>
2.
st=raw_input('enter a string')
l=len(st)
print "original string", st
st2=' '
for a in range(0,l,2):
st2+=st[a]
if a<(l-1):

st2+=st[a+1].upper()
print "alternatively capitallised string",st2
outputenter a stringpassion
original string passion
alternatively capitallised string pAsSiOn
3.
f=0
s=1
print f
print s
for a in range(1,19):
t=f+s
print t,
f,s=s,t
output0
1
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
4.
n=1
for a in range(5):
print n
n=n*10+1
output1

11
111
1111
11111
5.
st=raw_input('enter a string')
l=len(st)
i=0
for a in range(-1,(-l-1),-1):
print st[i],"\t",st[a]
i+=1
outputenter a stringshishir
s

6.
email=raw_input("enter yr email id:")
domain='@gmail.com'
ledo=len(domain)
lema=len(email)
sub=email[lema-ledo:]

if sub==domain:
if ledo!=lema:
print"it is valid email id"
else:
print"this is invalid email id ,contains just the domain name"
else:
print " this email id is either not valid or belongs to other domain."
Outputenter yr email id:shishir3698@gmail.com
it is valid email id
7.
l=[]
a=1
for y in range(8):
b=3*a
l.append(b)
a=a+1
c=0
for y in range(8):
if l[y]%5==0:
c=c+l[y]
print c
for a in range(0,8,2):
l[a],l[a+1]=l[a+1],l[a]
for y in l:
print y

output15
6
3
12
9
18
15
24
21

8.
l=[0]*8
for y in range(8):
l[y]=raw_input('enter a string')
s=len(l[0])
for a in range(8):
if len(l[a])<s:
s=len(l[a])
for c in range(8):
if len(l[c])==s:
print l[c]
for ost in l:
f=0
for ch in ost:

if ch=='a':
f=f+1
print ost,f
outputenter a stringshishir
enter a stringsgy
enter a stringajh
enter a stringaaaaaa
enter a stringaad
enter a string
enter a stringsdfsa
enter a string

ajh 1
aaaaaa 1
aaaaaa 2
aaaaaa 3
aaaaaa 4
aaaaaa 5
aaaaaa 6
aad 1
aad 2
sdfsa 1
9.
s='student'

k=len(s)
for a in s:
print s[0:k]
k=k-1
outputstudent
studen
stude
stud
stu
st
s
10.
x=raw_input('enter a number')
x=len(x)
if x==1:
y=raw_input('enter length')
y=int(y)
z=raw_input('enter breadth')
z=int(z)
print y*z
elif x==2:
y=raw_input('enter radius')
y=int(y)
print 3.14*y*y
else:

print 'wrong choice'


outputenter a number1
enter length12
enter breadth12
144
11.
Employees={'john':{'age':25,'salary':20000}, 'divya':{'age':23,'salary':300000}}
for key in Employees:
print"Employees",key+':'
print"age:"+str(Employees[key]['age'])
print'salary:'+str(Employees[key]['salary'])
outputEmployees divya:
age:23
salary:300000
Employees john:
age:25
salary:20000
>>>
12.
n=int(raw_input("how many students?"))
winners={}
for a in range(n):
key=raw_input("name of the student:")
value=int(raw_input("number of comp wons"))

winners[key]=value
print"the dictionary is"
print winners
output-

how many students?2


name of the student:\shishir
number of comp wons2
name of the student:sgishir4
number of comp wons123
the dictionary is
{'shishir': 2, 'sgishir4': 123}
>>>
13.
def secondlargest(t):
maxvalue=max(t)
length=len(t)
secmax=0
for a in range(length):
if secmax<t[a]<maxvalue:
secmax=t[a]
return secmax
tuple=(23,23,4,4,543,51543,35,345)
print secondlargest(tuple)
output543

14.
#student details
n=input('enter no of students')
dic={}
for a in range(n):
nm=raw_input('enter name')
r=input('enter roll no')
m=input('enter marks')
dic[nm]={'rno':r,'marks':m}
print 'dic is', dic
for a in dic:
print 'name is',a
k=dic[a]
print'dic[a] is', dic[a]
for b in k:
print b,'is', k[b]
outputenter no of students2
enter nameshishir
enter roll no377
enter marks100
enter nameshishir2
enter roll no38
enter marks00
dic is {'shishir2': {'rno': 38, 'marks': 0}, 'shishir': {'rno': 377, 'marks': 100}}
name is shishir2

dic[a] is {'rno': 38, 'marks': 0}


rno is 38
marks is 0
name is shishir
dic[a] is {'rno': 377, 'marks': 100}
rno is 377
marks is 100
15.
l=[]
for y in range(7):
s=int(raw_input('enter data'))
l.append(s)
a=raw_input('enter no')
a=int(a)
f=0
for y in range(7):
if a==l[y]:
print y
f=1
break
if f==0:
print'not found'
outputenter data1
enter data12
enter data34

enter data2
enter data23
enter data5
enter data346547635
enter no5
not found
not found
not found
not found
not found
5
16.
x=raw_input('enter a string')
for b in x:
if ord(b)>=97 and ord(b)<=122:
b=chr(ord(b)-32)
else:
b=chr(ord(b)+32)
print x
outputenter a stringshishir
SHISHIR
17.
s='student'
k=len(s)
j=0

for a in s:
print' '*j,s[j:k]
j=j+1
k=k-1
output=
student
tuden
ude
d
18.
l=[]
x=raw_input('enter no of elements')
x=int(x)
for y in range(x):
s=int(raw_input('enter data'))
l.append(s)
t=l[x-1]
for w in range(x-2,-1,-1):
l[w+1]=l[w]
l[0]=t
for b in range(x):
print l[b]
outputenter no of elements2
enter data3
enter data2

2
3
19.
def fun(x):
if type(x)==type(1):
print' integer'
else:
print ' different'

k=fun(2)
print k
outputinteger
None
20.
def fun(a):
b=len(a)
if b%2==0:
x=b/2
y=(b/2)-1
print a[x],a[y]
else:
x=(b-1)/2
print a[x]
c=0
for w in a:

if 0<=w<=9:
c=c+1
return c
print fun('abcdefg')
outputd
0

You might also like