Python Lists
Subject: Python Programming
Sometimes it is more convenient to store multiple values or items in one variable. For example to store 50 students name, instead of creating student1, student2, student3 .... student50, then using List may be more convenient.
Lists (known as arrays in other languages) are one of the compound data types that Python understands. Lists can be indexed, sliced and manipulated with other built-in functions. Lists help us to easily handles ordered set of items in Python.
In Python lists is created by entering all the expected values between a square bracket.
Example 1
#creating a lists of students
students=["Onuorah", "Abiodun", "Abu", "Chioma"]
#creating a lists of grades
grades=[50, 48, 20, 37]
The first list containing names i.e strings (that is why the items are in doubles quotes).
The second list grades is an interger type list i.e whole numbers with no decimal point (number with decimal point are refer to as floats)
Example 2
You can print out all the values in the created lists in example 1 by simply doing the following:
#continue from example 1
print (grades)
print (students)
This approach print the list with the square bracket, but if you want to get only the values (which is a better approach) then it will be better to loop through the list and print out each items of the list.
Example 3
#continue from example 1
#loop through the lists and print each items
for name in students:
print (name)
for grade in grades:
print (grade)
To access just one item in a list, you simply reference the index of the item in the list.
Example 4
students=["Onuorah", "Abiodun", "Abu", "Chioma"]
print (students[0]) #will output Onuorah
print (students[1]) #Abiodun
print (students[2]) #Abu....
This approach of use the index to reference an item, can also be to access the characters of a string as do for a list.
Example 5
msg="Welcome to TEA Learn"
#accessing single character from the string
print (msg[0]) #will output 'W'
print (msg[0:7]) #will output 'Welcome'
print (msg[8:11]) #will output 'to'
Empty List
This is a list that has no values stored to it when created, the idea is to store (append) a value to it later in the program.
Example 6
itemcost = [] #empty list
num=int(input("How many products was ordered "))
#use loop to loop through number of products ordered.
for n in range(num):
#ask user to enter the cost for each product
each_cost = float(input("Enter a product cost "))
#store the inputted cost to the empty list
itemcost.append(each_cost)
#Display each product cost and sum total
total_cost=0
count=0
for y in range(num):
product_cost=itemcost[y]
count=count+1
print ("Product ",count," = ",product_cost)
#sum as we loop through....
total_cost=total_cost+product_cost
print ("The total cost is ",total_cost)
Example 7
#Program to search if user name exist in a list
usernames=["bola", "hammed", "chido", "ada"]
search=input("Enter the name you searching for ")
flag=0
for item in usernames:
if search.lower()==item:
flag = 1
break #item found exit loop
if flag==1:
print (search, " exist in our record")
else:
print (search, " NOT found...")
By:
Benjamin Onuorah
Comments
Joshua Fasinu (learner)
Date commented: 13-Aug-2024 08:17:38am
nice!
Login to comment or ask question on this topic
Previous Topic Next Topic