0% found this document useful (0 votes)
28 views

Python 2

The document contains code snippets with different aims related to lists in Python. Some of the aims are to reverse words in a string, find the smallest and largest element in a list, calculate the mean of a list, separate a list into unique and duplicate elements, and shift all the elements of a list to the right. The code snippets take a user input, manipulate the input using for loops and built-in functions, and output the results.

Uploaded by

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

Python 2

The document contains code snippets with different aims related to lists in Python. Some of the aims are to reverse words in a string, find the smallest and largest element in a list, calculate the mean of a list, separate a list into unique and duplicate elements, and shift all the elements of a list to the right. The code snippets take a user input, manipulate the input using for loops and built-in functions, and output the results.

Uploaded by

monishchess2468
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Aim: To enter a string and reverse each word in a given string

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

Aim: To enter string and reverse words starting with a vowel


Input:
s=input("enter string: ")

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

acirfa si na suomrone continent


Aim: To print elements of a list in separate lines
Input:
L=eval(input("enter list:"))

for i in L:

print(i)

Output:
enter list:[1,2,2,3]

Aim: To find smallest and largest element in a list


Input:
l=eval(input("enter list: "))

min,max=0,0

for i in l:

if i>max:

max=i

elif i<min:

min=i

print("Largest element is",max,",smallest element is",min)

Output:
enter list: [1,2,3,4,100,0]

Largest element is 100 ,smallest element is 0


Aim: To find mean of a list
Input:
l=eval(input("enter list: "))

sum=0

n=len(l)

for i in l:

sum+=i

avg=sum/n

print("Mean of list is",avg)

Output:
enter list: [1,2,3]

Mean of list is 2.0

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:

if i[0] in "AEIOUaeiou" and i[-1] in "AEIOUaeiou":

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:

if len(i)>=3 and i[0]==i[-1]:

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: "))

L=eval(input("enter list: "))

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)

print(l1first, l1sec, "sum=",sum1,"avg=",avg)


Output:
enter even number: 6

enter list: [1,2,3,4,5,6]

[1, 2, 3] [4, 5, 6] sum= 6 avg= 5.0

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: "))

L=eval(input("enter list: "))

last=-1

for i in range(n//2):

L[i],L[last]=L[last],L[i]

last-=1

print(L)

Output:
enter number: 6

enter list: [10,9,8,7,6,5]

[5, 6, 7, 8, 9, 10]

Aim: To remove duplicate elements from a list


Input:
L=eval(input("Enter list: "))

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]

[1, 22, 33, 44]

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]

[0, 5, 6, 7, 8, 10, 11, 12]

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]

[1, 10, 10, 10, 10]

Aim: To insert a list of integers and push all 0 elements to RHS


Input:
L=eval(input("Enter list:"))

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:

if len(i)>=2 and i[0]==i[-1]:

count+=1

print("No. of such strings is",count)

Output:
Enter list: ['africa','cat','alpha','boat']

No. of such strings is 2

Aim: To find second largest no. of a list


Input:
L=eval(input("Enter list: "))

L.sort()

smax=L[-2]

print("Second greatest is",smax)

Output:
Enter list: [5,23,3521,1,7,2]

Greatest is 3521 and second greatest is 23


Aim: To find even elements in a list
Input:
L=eval(input("Enter list: "))

for i in L:

if i%2==0:

print(i,end=" ")

Output:
Enter list: [2,4,6,1,3,5]

246

Aim: To print list of numbers less than 10 in a given list


Input:
L=eval(input("Enter list: "))

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]

Aim: To display frequency of each item in a list


Input:
L=eval(input("Enter list: "))

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

You might also like