Python 3 List and Programs

This blog is Explains about Python 3 List and its Example Programs:

List :-

In Python programming, a list is created by placing all the items (elements) inside square brackets [] , separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.). A list can also have another list as an item.

Split()


split() method returns a list of strings after breaking the given string by the specified separator.

Syntax :
str.split(separator, maxsplit)

Program 1:-

list=[‘a’,’b’,’c’,’d’]
for item in list:
print(item,end=”)

Output :-

abcd

Program 2:-

alpha=[‘a’,’b’,’c’,’c’]
numeric=[1,2,3,4]
both=[alpha,numeric]
print(both)
li=0
while li<len(alpha):
item=0
while item<len(alpha[li]):
print(alpha[li][item],end=”)
item+=1
print()
li+=1

Output :-

[[‘a’, ‘b’, ‘c’, ‘c’], [1, 2, 3, 4]]
a
b
c
c

Program 3 :-

alpha=[‘a’,’b’,’c’,’c’]
numeric=[1,2,3,’c’]
both=[alpha,numeric]
print(both)
li=0
while li<len(both):
item=0
while item<len(both[li]):
if(type(both[li][item])==str):
print(both[li][item],end=”)
item+=1
print()
li+=1

Output :-
[[‘a’, ‘b’, ‘c’, ‘c’], [1, 2, 3, ‘c’]]
abcc
c

Program 4 :-

alpha=[‘a’,’b’,’c’,’c’]
numeric=[1,2,3,’c’]
both=[alpha,numeric]
print(both)
li=0
while li<len(both):
item=0
while item<len(both[li]):
if li==1 and item==1:
both[li][item]=’check’
item+=1
print()
li+=1
print(both)

Output: –
[[‘a’, ‘b’, ‘c’, ‘c’], [1, 2, 3, ‘c’]]
[[‘a’, ‘b’, ‘c’, ‘c’], [1, ‘check’, 3, ‘c’]]

Program 5:-

number=int(input(“Tell us the number”))
squares=[]
for i in range(1, number+1):
squares.append(i**i)
print(squares)

Output :-
Tell us the number 5
[1, 4, 27, 256, 3125]

Program 6:-

studentDetails=[]
studentCount=int(input(“good morning sir,””please tell me student count in your class”))
for student in range(studentCount):
name,age,percentage=input(“Enter your name, Age, percentage separated by space:”).split()
studentDetails.append(name)
studentDetails.append(age)
studentDetails.append(percentage)
print(studentDetails)
count=1
print(“student name”, “student age”, “student mark”, end=”)
print()
print(“————————————————–“,end=”)
print()
for percentage in studentDetails:
if count%3==0:
if int(percentage)>75:
print(studentDetails[count-3],”:”,studentDetails[count-2])
count+=1

Output :-

good morning sir,please tell me student count in your class5
Enter your name, Age, percentage separated by space:samruddhi 17 95
Enter your name, Age, percentage separated by space:swathi 19 80
Enter your name, Age, percentage separated by space:sai 16 70
Enter your name, Age, percentage separated by space:surya 19 60
Enter your name, Age, percentage separated by space:ajith 20 65
[‘samruddhi’, ’17’, ’95’, ‘swathi’, ’19’, ’80’, ‘sai’, ’16’, ’70’, ‘surya’, ’19’, ’60’, ‘ajith’, ’20’, ’65’]

student name student age student mark

samruddhi : 17
swathi : 19

Reference

www.programiz.com

www.geeksforgeeks.com

Leave a Reply

Your email address will not be published. Required fields are marked *