Python Loops and Lists

You should now be able to do some programs that are much more interesting. If you have been keeping up, you should realize that now you can combine all the other things you have learned with if-else statement or with string functions to make your programs do smart things. But some time we need our program to do repetitive tings very quickly, or to use a list of data like list of items in stock or list of books in library. We can do all these with help of Python Loops and Lists.

We are going to use a for-loop in this exercise to build and print various lists. When you do the exercise, you will start to figure out what they are. I won’t tell you right now. You have to figure it out. But before we create a program with loop lets understand lists.

Lists

list1 = ["1", "2", "3", "4", "5", "6"]
list2 = ["bus", "car", "train", "bike"]
print (list1[3])
print (list2[2])

Copy past this code and run it. I think you can understand by running this code what it do? But what if we want to print complete list or if you want to run some function on every value and then print? you need to use a loop for it.

For Loop

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# this first kind of for-loop goes through a list
# we use %d when we print a integer not string
for number in the_count:
 print ("This is count %d" % number)
# same as above
for fruit in fruits:
 print ("A fruit of type: %s" % fruit)
# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
 print ("I got %r" % i)
# we can also build lists, first start with an empty one
elements = []
# then use the range function to do 0 to 5 counts
for i in range(0, 6):
 print ("Adding %d to the list." % i)
 # append is a function that lists understand
 elements.append(i)
# now we can print them out too
for i in elements:
 print ("printing from Element : %d" % i)

Now check the output

python loop and list

Now lets understand this program, I have used both python loops and lists in this code. We have create 3 lists one with number, one with strings and last with both. We can pass number in lists directly just like assigning it to a variable.

append function

Lists have append function which you can use to insert some values in lists.

Lets see another example:

names = []
age = []
entries_count = input("How many entries you want: ")
for i in range(int(entries_count)):
 names.append(input("Please enter your name: "))
 age.append(input("Please enter your age: "))
print ("Thansk for your entires")
# Printing current values
for i in range(int(entries_count)):
 print("Name is %s Ages is %s" % (names[i], age[i]))
# To search a value in string
search_age = input("Enter age to search: ")
for i in range(int(entries_count)):
 if age[i] == search_age:
 print ("Person with given age is %s" % (names[i]))

Now run this code and see the output.

 

Exercise of the day:
  • Create a programe which:
    • Take values from user
    • Do error handling
  • Change the last example I gave you
    • It should save data in file in formate Name:Age

 

 

comments (2)

Leave me comment