Python 2
Python 2
Input:
s=input("enter string: ")
x=s.split()
for i in x:
print(i[::-1],end=" ")
Output:
enter string: this is a string
siht si a gnirts
x=s.split()
for i in x:
if i[0] in "AEIOUaeiou":
print(i[::-1],end=" ")
else:
print(i,end=" ")
Output:
enter string: africa is an enormous continent
for i in L:
print(i)
Output:
enter list:[1,2,2,3]
min,max=0,0
for i in l:
if i>max:
max=i
elif i<min:
min=i
Output:
enter list: [1,2,3,4,100,0]
sum=0
n=len(l)
for i in l:
sum+=i
avg=sum/n
Output:
enter list: [1,2,3]
Aim: To enter list of integers and create another 2 lists unique and duplicate
having elements which are not repeated in unique list and repeated in duplicate
Input:
l=eval(input("enter list: "))
l1=[]
l2=[]
for i in l:
if i not in l1:
if l.count(i)>1:
l1.append(i)
if l.count(i)==1:
l2.append(i)
print(l2,l1)
Output:
enter list: [1,2,2,2,2,3,3,3,4,5,6]
[1, 4, 5, 6] [2, 3]
Aim: To enter list of strings and copy all words starting with a vowel and ending
with a vowel into a new list called vowel
Input:
l=eval(input("enter list: "))
vowel=[]
for i in l:
vowel.append(i)
print(vowel)
Ouput:
enter list: ["africa","is","big"]
['africa']
Aim: To count no. of strings present in a list of strings where string length is 3 or
more and first and last characters are same
Input:
l=eval(input("enter list: "))
c=0
for i in l:
c+=1
print(c)
Output:
enter list: ["hello","africa","yes","argo"]
1
Aim: To shift every element of a list to the right
Input:
l=eval(input("enter list: "))
x=l[-1]
for i in range(len(l)-1,0,-1):
l[i]=l[i-1]
l[0]=x
print(l)
Output:
enter list: [1,2,3,4,5,6]
[6, 1, 2, 3, 4, 5]
Aim: To store a list of n numbers where n is an even number, create two new
lists, namely l1first and l2sec storing elements of first half and second half
elements of the list. find the sum of elements in the first half and avg of
elements in second half and display both the halves and the sum and average
Input:
n=int(input("enter even number: "))
sum1,sum2,avg=0,0,0
l1first=L[:n//2]
l1sec=L[n//2:]
for i in range(n//2):
sum1+=L[i]
for j in range(n//2,n):
sum2+=L[j]
avg=sum2/len(l1sec)
Aim: To accept a list of n numbers where n is even no. Exchange the first half
elements with the second half without using any separate list and display the
list
Input:
n=int(input("enter number: "))
last=-1
for i in range(n//2):
L[i],L[last]=L[last],L[i]
last-=1
print(L)
Output:
enter number: 6
[5, 6, 7, 8, 9, 10]
L1=[]
for i in L:
if i not in L1:
L1.append(i)
print(L1)
Output:
Enter list: [1,22,22,22,33,44,33]
Aim: To input list L of integers, and create another list indexlist that stores the
index of all nonzero elements of list L
Input:
L=eval(input("Enter list: "))
indexlist=[]
for i in range(len(L)):
if L[i] !=0:
indexlist.append(i)
print(indexlist)
Output:
Enter list: [1,0,0,0,0,2,3,4,7,0,1,2,45,0]
Aim: To replace elements having even values with its half and odd values to its
double
Input:
L=eval(input("Enter list: "))
L1=[]
for i in L:
if i%2==0:
L1.append(i//2)
else:
L1.append(i*2)
print(L1)
Output:
Enter list: [1,2,3,4,5,6]
[2, 1, 6, 2, 10, 3]
Aim: To enter a list containing numbers 1 to 12, then replace all no. greater than
10 to 10
Input:
L=eval(input("Enter list: "))
for i in range(len(L)):
if L[i]>10:
L[i]=10
print(L)
Output:
Enter list: [1,10,10,11,13]
L1=[]
for i in L:
if i==0:
continue
else:
L1.append(i)
for j in range(1,L.count(0)+1):
L1.append(0)
print(L1)
Output:
Enter list:[1,2,3,0,0,2,2,30,0,1]
[1, 2, 3, 2, 2, 30, 1, 0, 0, 0]
Aim: To count the number of strings where the string length is 2 or more and
the first and last character are same from a given list of strings
Input:
L=eval(input("Enter list: "))
count=0
for i in L:
count+=1
Output:
Enter list: ['africa','cat','alpha','boat']
L.sort()
smax=L[-2]
Output:
Enter list: [5,23,3521,1,7,2]
for i in L:
if i%2==0:
print(i,end=" ")
Output:
Enter list: [2,4,6,1,3,5]
246
L1=[]
for i in L:
if i<10:
L1.append(i)
print(L1)
Output:
Enter list: [1,2,3,4,5,6,11,12,10,11]
[1, 2, 3, 4, 5, 6]
L1=[]
for i in L:
if i not in L1:
L1.append(i)
for j in L1:
print("Frequency of",j,"is",L.count(j))
Output:
Enter list: [1,2,2,2,2,3,3,4]
Frequency of 1 is 1
Frequency of 2 is 4
Frequency of 3 is 2
Frequency of 4 is 1
Aim: Input a list of numbers and swap elements at the even location with the
elements at the odd location
Input:
L=eval(input("Enter list: "))
x=i=0
y=1
while i<len(L):
if y>len(L) or x>len(L):
break
L[x],L[y]=L[y],L[x]
x+=2
y+=2
i+=1
print(L)
Output:
Enter list: [1,2,3,4,5,6]
[2, 1, 4, 3, 6, 5]
Aim: To print the sequence A
AD
ADI
ADIS
Input:
s="ADIS"
for i in range(1,5):
for j in range(i):
print(s[j],end='')
print()
Output:
A
AD
ADI
ADIS