0% found this document useful (0 votes)
29 views19 pages

Computer PROGRAMS A2Z XI

The document contains multiple Python code snippets demonstrating various programming concepts such as reversing strings, checking leap years, determining palindromes, generating Fibonacci series, sorting lists, and searching elements in lists. Each section includes user input prompts and example outputs to illustrate the functionality of the code. The document serves as a practical guide for beginners to learn basic programming techniques in Python.

Uploaded by

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

Computer PROGRAMS A2Z XI

The document contains multiple Python code snippets demonstrating various programming concepts such as reversing strings, checking leap years, determining palindromes, generating Fibonacci series, sorting lists, and searching elements in lists. Each section includes user input prompts and example outputs to illustrate the functionality of the code. The document serves as a practical guide for beginners to learn basic programming techniques in Python.

Uploaded by

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

REVERSE THE DIGITS OF A STRING USING FOR LOOP

a=input("Enter a word : ")


b=''
l=len(a)
for i in range(-1,-l-1,-1):
b=b+a[i]
print('orginal string= ',a)
print('reverse string= ',b)

'''
Enter a word : tamanna
orginal string= tamanna
reverse string= annamat
'''

REVERSE THE DIGITS OF A STRING USING WHILE LOOP


a=int(input("Enter a number : "))#1234
b=a
s=0
while a>0:#T
c=a%10#4,3,2,1
s=s*10+c#0*10+4=4,4*10+3=43,43*10+2=432,432*10+1=4321
a=a//10#123,12,1,0
print("REVERSE OF ",b,'=' ,s)

'''
Enter a number : 1234
REVERSE OF 1234 = 4321

'''

CHECK WHETHER AN YEAR IS A LEAP YEAR OR NOT


yy=int(input('Enter year :'))
if yy%100==0 and yy%400==0:
print(yy,' : is leap year')
elif yy%4==0:
print(yy, ': is leap year')
else:
print(yy,' : is not leap year')

'''
Enter year :1999
year is not leap year

'''

CHECK WHETHER A NUMBER IS A PALINDROM OR NOT


a=int(input("Enter a number : "))
b=a
s=0
while a>0:
c=a%10
s=s*10+c
a=a//10
print("REVERSE OF ",b,'=' ,s)
if b==s:
print(b,' is a palindrome ')
else:
print(b,' is not a palindrome ')

'''
Enter a number : 1221
REVERSE OF 1221 = 1221
1221 is a palindrome

'''

FIBONACCI SERIES AND SUM OF FIBONACCI SERIES


n=int(input('enter any no ?'))
a,b=0,1
s=1
print('Fibonacci series !')
print(a,b,end=',')
for i in range(3,n+1):
c=a+b
s+=c
print(c,end=',')
a=b
b=c
print('=',s)

'''
enter any no ?20
Fibonacci series !
0 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,= 10945
'''

ASENDING AND DESENDING USING FOR LOOP


print()
print('asending order of for loop')
for i in range(1,10,2):
print(i,end=',')
print()
print('desending order of for loop')
for i in range(9,-1,-2):
print(i,end=',')

'''
asending order of for loop
1,3,5,7,9,
desending order of for loop
9,7,5,3,1,
'''
CHECK CREDIT CARD NUMBER
a=int(input('enter a credit card no '))
b=a
s1=0
s2=0
l=[]
i=0
while a>0:
x=a%10
l.append(x)
i+=1
a=a//10
print()
print('a=',a)
print('orginal no=',b)
print('no of digits =',i)
print(l)#87654321
for j in range(0,i,2):
s1+=l[j]
print('sum of odd position no =',s1)

for j in range(0,i,2):
x=l[j+1]**2
print('sqr of no=',x)
while x>0:
p=x%10
s2+=p
#print('sum=',s2)
x=x//10
print('sum of odd position no =',s2)

'''
enter a credit card no 12345678

a= 0
orginal no= 12345678
no of digits = 8
[8, 7, 6, 5, 4, 3, 2, 1]
sum of odd position no = 20
sqr of no= 49
sqr of no= 25
sqr of no= 9
sqr of no= 1
sum of odd position no = 30
'''

SEARCH A WORD IN ANY STATEMENT


line = input("Enter any statement")
word = input("Enter any word")
if word in line:
#print(line)
print("Yes ", word, "is a part of ",line)
else:
#print(line)
print("No", word ," is not part of ",line)
'''

Enter any statement i am noida


Enter any word: noida
Yes noida is a part of i am noida

'''

Sum of all elements of list.


l=[]
#input the elements of the list
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
s=0
for i in l:
s+=i
print("Sum of the elements of the list= ",s)

'''
Enter the no. of elements in a list: 5
Enter a number:10
Enter a number:20
Enter a number:30
Enter a number:40
Enter a number:50
Sum of the elements of the list= 150
'''

Sum of the even numbers of the list


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)
se=0
for i in l:
if i%2==0:
se+=i
print("Sum of the even numbers of the list= ",se)

'''
Enter the no. of elements in a list: 6
Enter a number:1
Enter a number:2
Enter a number:3
Enter a number:4
Enter a number:5
Enter a number:6
Sum of the even numbers of the list= 12
'''
Sum of the odd numbers of the list
l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
so=0
for i in l:
if i%2!=0:
so+=i
print("Sum of the odd numbers of the list= ",so)
'''
Enter the no. of elements in a list: 5
Enter a number:12
Enter a number:8
Enter a number:55
Enter a number:23
Enter a number:78
Sum of the odd numbers of the list= 78
'''

Sum of even index number of the list


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
se1=0
for i in range(n):
if i%2==0:
se1+=l[i]
print ("Sum of even index numbers of the list= ",se1)
'''
Enter the no. of elements in a list: 6
Enter a number:10
Enter a number:20
Enter a number:30
Enter a number:40
Enter a number:50
Enter a number:60
Sum of even index numbers of the list= 90
'''

Sum of odd index number of the list


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
se2=0
for i in range(n):
if i%2!=0:
se2+=l[i]
print("Sum of odd index number of the list= ",se2)
'''
Enter the no. of elements in a list: 6
Enter a number:10
Enter a number:20
Enter a number:30
Enter a number:40
Enter a number:50
Enter a number:60
Sum of odd index number of the list= 120
'''

Modify a list
l=[]
#input the elements of the list
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)
le=[]
lo=[]

for i in l:
if i%2==0:
le.append(i)
else:
lo.append(i)
print("ORIGINAL LIST: ",l)
print(" EVEN NUMBERS OF THE LIST: ",le)
print(" ODD NUMBERS OF THE LIST: ",lo)

'''
Enter the no. of elements in a list: 5
Enter a number:1
Enter a number:2
Enter a number:3
Enter a number:4
Enter a number:5
ORIGINAL LIST: [1, 2, 3, 4, 5]
EVEN NUMBERS OF THE LIST: [2, 4]
ODD NUMBERS OF THE LIST: [1, 3, 5]
'''

Bubble sort a list.


#create a list, sort usinging bubble sort and display the elements in proper way using for loop
l=[32,10,2,7,24,100,120,89,56]
l1=list(l)
#bubble sort
for i in range(len(l)):
for j in range(len(l)-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]

#display both list


print('orginal list : ',l1)
print('sorted list : ',l)
'''
orginal list : [32, 10, 2, 7, 24, 100, 120, 89, 56]
sorted list : [2, 7, 10, 24, 32, 56, 89, 100, 120]

'''

LINEAR SEARCH AN ELEMENT IN THE LIST


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
l1=list(l)
l2=list(l)
y=int(input("Enter the no. to be search in a ist: "))
j=0
for i in l:
if y==i:
j+=1
if j>0:
print(y,' is in the list ',j,' times')
else:
print(y,' is not in the list ')
print(l)
'''
Enter the no. of elements in a list: 6
Enter a number:10
Enter a number:20
Enter a number:30
Enter a number:10
Enter a number:50
Enter a number:10
Enter the no. to be search in a list: 10
10 is in the list 3 times
[10, 20, 30, 10, 50, 10]
'''

Shift the present element in the last in a list


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
for i in range(len(l)):
if y==l[i]:
x=l.pop(i)
l.append(x)
print('new list ',l)
print('orginal list ',l1)
'''
new list [20, 30, 50, 10, 10, 10]
orginal list [10, 20, 30, 10, 50, 10]
'''

Shift the only duplicate element in the last


l=[]
n=int(input("Enter the no. of elements in a list: "))
for i in range(n):
a=int(input("Enter a number:"))
l.append(a)#l=l+[a]
j=0
for i in range(len(l2)):
if y==l2[i]:
j+=1
if j>1:
x=l2.pop(i)
l2.append(x)
print('new list ',l2)
print('orginal list ',l1)
'''
new list [20, 10, 30, 40, 50, 10, 10]
orginal list [20, 10, 30, 40, 10, 50, 10]

'''

LIST USING WHILE LOOP


l=[]
ch='y'
while ch=='y':
a=input("Enter a name:")
l.append(a)
ch=input('do u want to enter more names (y/n)? ')
print('printing direct list :',l)

'''
Enter a name:tamanna mohan
do u want to enter more names (y/n)? y
Enter a name:mohan singh
do u want to enter more names (y/n)? y
Enter a name:tushar mohan
do u want to enter more names (y/n)? h
printing direct list : ['tamanna mohan', 'mohan singh', 'tushar mohan']
'''

BINARY SEARCH
l=[10,20,30,45,46,75,80,89,100,200]
y=eval(input('enter the no to be searched :?'))#100
l1=len(l)
lb=0
ub=l1-1
j=0
while (lb<=ub and j==0):
mid=(lb+ub)//2#4,7,8
if y>l[mid]:#T
lb=mid+1#5,8
elif y<l[mid]:
ub=mid-1
else:
j+=1#1
pos=mid#8

print('List :',l)
if j>0:
print(y,'is present ',pos,' in the list')
else:
print(y,'is not present in the list')
'''
enter the no to be searched :?46
List : [10, 20, 30, 45, 46, 75, 80, 89, 100, 200]
46 is present 4 in the list

enter the no to be searched :?33


List : [10, 20, 30, 45, 46, 75, 80, 89, 100, 200]
33 is not present in the list
'''

ADD A NUMBER TO THE LIST


l=[1,2,3]
l2=[5,6,7]
l2.append(l)
print('l=',l)
print('orginal l2=',l2)
print('new l2=',l2)

'''
l= [1, 2, 3]
orginal l2= [5, 6, 7, [1, 2, 3]]
new l2= [5, 6, 7, [1, 2, 3]]
'''
PATTERN-1

for i in range(1,10):
for j in range(9-i,0,-1):
print(' ',end=' ')
for k in range(1,i+1):
print(k,end=' ')
print()

'''
1
12
123
1234
12345
123456
1234567
12345678
123456789
'''

PATTERN-2

n=int(input('Enter any value '))


for i in range(n,0,-1):
print('A'*i)
'''
Enter any value 9
AAAAAAAAA
AAAAAAAA
AAAAAAA
AAAAAA
AAAAA
AAAA
AAA
AA
A
'''

PATTERN-3

n=int(input('Enter any value '))


for i in range(n):
print('$'*i)

'''
Enter any value 10

$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$
$$$$$$$$$

''’
PATTERN-4

for i in range(1,10):
for j in range(9-i,0,-1):
print(j ,end=' ')
print()

'''
87654321
7654321
654321
54321
4321
321
21
1
'''
PATTERN-5
CONVERT THE NUMBER ENTERED BY THE USER INTO CORRESPONDING NUMBER IN WORDS
USING DICTIONARY

FORMATTED OUTPUT OF STATES WITH THEIR CAPITALS USING DICTIONARY

WAP TO STORE EMPLOYEE DETAILS IN A DICTIONARY


emp={}
n=int(input('Enter the no of Employee :? '))
i=1
while i<=n:
empno=input('Enter the Employee no :? ')
ename=input('Enter the Employee Name :? ')
job=input('Enter Post :? ')
sal=eval(input('Enter Salary : ? '))
emp[empno]=[ename,job,sal]
i+=1
print('='*80)
print('DICTIONARY IN NORMAL FORM')
print(emp)
print('='*80)
key=emp.keys()
for i in key:
print('='*80)
print("Employee No. : ",i, ":")
val=emp[i]
print("Emp Name\t\t","Post\t\t"," Salary")

for j in val:
print(j,end="\t\t")
print()
print('='*80)
x=input('Enter the empno to be searched :/ ')

WAP TO SEARCH IN A DICTIONARY


DICTIONARY FUNCTIONS =>
1. len()
2. clear()
3. get()
4. item()
TUPLE-1

TUPLE-2
TUPLE-3

n=int(input("n="))
efinal=()
dfinal=()
complete=()
for i in range(n):
email=input("Enter the email id: ")
complete=complete+(email,)
em,dm=email.split('@')
efinal+=(em,)
dfinal+=(dm,)
print("Email id= ", efinal)
print("Domain Name= ",dfinal)
print("Complete= ", complete)

'''
n=3
Enter the email id: [email protected]
Enter the email id: [email protected]
Enter the email id: [email protected]
Email id= ('stef', 'kiki', 'tush')
Domain Name= ('bonzo.com', 'tamix.com', 'kammo.com')
Complete= ('[email protected]', '[email protected]', '[email protected]')

'''

TUPLE-4
a=input("Enter a string: ")
b=tuple(a)
print("Tuple created from string: ",b)

'''
Enter a string: Tamanna
Tuple created from string: ('T', 'a', 'm', 'a', 'n', 'n', 'a')

'''

TUPLE-5
a=eval(input("Enter a tuple: "))
s=0
for i in a:
if i%2==0:
s+=i
print(s)

'''
Enter a tuple: (1,2,3,4,5,6,7,8)
20

'''
BASIC OPERATORS

WAP TO PRINT THE SUM OF 2 NOS.

WAP TO PRINT THE DIFFERENCE OF 2 NOS.

WAP TO PRINT THE PRODUCT OF 2 NOS.


WAP TO PRINT THE REMAINDER OF 2 NOS.

WAP TO FIND THE AREA AND PRIMETER OF A RECTANGLE.

WAP TO FIND THE AREA OF A CIRCLE


WAP TO READ THE PA, RATE N TIME THEN PRINT THE SI.

WAP to read the Marks of 5 subjects from the user and print the Total Marks and
Percentage .

You might also like