Skip to main content

LIST IN PYTHON - 14TH AUG

 LIST IN PYTHON

BEGINNERS PRACTICE QUESTIONS


1. Creating List by accepting data from user

List1=[] #Empty list

n=int(input("Enter number of elements"))   

i=0

while i < n:

    num= int(input("Enter element " + str(i) + ": "))

    List1+=[num]  

    i+=1

print(List1)

OR

#Creating List by accepting data from user

List1=[ ]

n=int(input("Enter number of elements"))  

i=0

while i < n:

    List1+=[int(input("Enter the element"))]  

    i+=1

    print(List1)


2.Write a program to input the size and the list of elements from user. Count the number of elements which are divisible by 5

list1= [ ]

s =int (input("Enter no of elements"))

for i in range(s):

    list1 += [int (input("Enter the Elements"))]

count=0

for num in list1:

        if num % 5 ==0:

            count = count+1

print("No. of elements divisible by 5--",count)


3. Write a program to input the size and the list of elements from user. Count the number of odd, even and zero .
list1 = []
s = int (input("Enter no of Elements:"))
ectr = octr = zctr = 0
for i in range(s):
list1 += [int (input("Enter the Element:"))]
for num in list1:
    if num==0:
       zctr+= 1
    elif num % 2 ==0:
        ectr += 1
    else:
        octr +=1
print("No. of odd, even and zero are:",octr,ectr,zctr)

4. Write a program to input the size and the list of elements from user Count the number of positive, negative and zero

5. Write a program to input a number and a list from user. Count the occurrence of that number in the list.
Sol:
list1=[]
s = int(input("Enter no of elements"))
for i in range(s):
    num= int(input("Enter element " + str(i) + ": "))
    list1 += [num]
n = eval(input("enter number"))
count=0
for i in list1:
    if n==i:
        count=count+1
    if count==0:
        print("element not present")
    else:
print( n ," is present" , count," times")

6.#Find Max number in a given list

list=[1,20,30,87,34]
max = list[0]
for a in list:
    if a > max:    
        max = a
    print(max) 

Traversing using for loop [ Examples]


1.Traversing using for loop Using in operator When 'in' operator is used with for loop, Python access each element of a list in sequence:
list = [12,10,”Python]
for i in list:
    print(i)

2. Traversing using for loop
Using range( )
The sequence of numbers generated by range() can be used as the index to access each element.
Example
list = [12,10,"Python"]
for i in range(3):
        print(list[i])

3.Traversing using while loop
Example
list = [12,10,"Python"]
Index = 0
while Index < 3:
    print(list[Index])
    Index +=1

7.#Program to input a list and to count number of  non-numeric data in a list
list=[]
s = int(input("Enter no of elements - "))  
for i in range(s):
    list+=[eval(input("Enter the element - "))]  
print(list)
ctr=0
for i in range(s):
    if type(list[i])==str:
           ctr+=1
print("no. of non-numeric data - ",ctr)

8.Write a program to input a list. Replace all positive numbers with twice it’s value and all negative numbers with half of it’s value.
list1=[]
s =int(input("Enter no of Elements:"))# Get the number of elements
for i in range(s):
    list1+=[int(input("Enter the elements:"))] # Add each element to the list
for i in range(s): #traversing the list
    if list1[i] < 0:
        list1[i] /=2# Halve the negative numbers
    elif list1[i] > 0:
        list1[i] *= 2# Double the positive numbers
print(list1)

9.#swap the first and last element in the list
sl = [10, 7, 12,66 ,88,77,56, 3, 14]
sl[0], sl[-1] = sl[-1], sl[0]
print(sl)

#or

sl = [10, 7, 12,66 ,88,77,56, 3, 14]
n = len(sl)
# Swapping 
temp = sl[0]
sl[0] = sl[n - 1]
sl[n - 1] = temp
print(sl)

10.#Largest element in list
list = [10, 7, 12,66 ,88,77,56, 3, 14]
large=list[0]
for i in list:
        if i>large:
            large=i
print("Largest number is :",large)

11.#PROGRAM TO CALCULATE MEAN OF A GIVEN LIST NUMBERS

list1=eval(input("Enter the List elements"))
length=len(list1)
mean=sum=0
for i in range(0,length):
    sum+=list1[i]
mean=sum/length
print("Given List is:",list1)
print("Mean of Given List is:",mean)

12.#ADDITION OF ELEMENTS
list1=[4,5,6,6]
list2=[1,2,3,4]
print("ORIGINAL LIST1 IS:"+str(list1))
print("ORIGINAL LIST2 IS:",list2)
res_list=[]
for i in range(0, len(list1)):
    res_list.append(list1[i]+list2[i])
    print(list1[i],list2[i])
print("The resultant List is :"+str(res_list))

13.#LINEAR SEARCH/search for an element in a given list
list1=eval(input("Enter the List elements"))
length=len(list1)
element=int(input("Enter the element to be searched for")) 

for i in range(0,length):
    if element==list1[i]:
        print("Element found At Index:",i)
        break
else:
    print(element,"Not present in the list")

14.#swap the first 3 and last 3 elements 
l1 = []
s = int(input("Enter no. of elements:"))
for i in range(s):
l1.append(int(input("Enter element"+str(i+1)+":")))
print(l1)
l1[:3], l1[-3:] = l1[-3:],l1[:3]
print(l1)

15. #swap the first and last element in the list
sl = [10, 7, 12,66 ,88,77,56, 3, 14]
sl[0], sl[-1] = sl[-1], sl[0]
print(sl)




Comments

Popular posts from this blog

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...