Learn Python The Hard Way learning (32)-loop and list

Source: Internet
Author: User
Tags pears

Next we will do some interesting things. if you follow the progress, you will find that you can use the if statement and Boolean expression to do a lot of things.

In any case, the program will do some repetitive tasks. Next we will print a list variable with a for loop. During this exercise, you must understand their meanings and functions.

Before using the for loop, we need to save the value of the loop. The best way is to use a list. The list is the container that stores data in order, which is not very complex, it is just a new syntax. The structure is as follows:
Hairs = ['brown ', 'blond', 'red']
Eyes = ['brown ', 'Blue', 'green']
Weights = [1, 2, 3, 4]

A list starts with a [sign. The elements in the list are separated by a comma, which is like a function parameter and ends with a]. python includes all the elements in a variable.

Note: In the previous exercise, we used the if statement to include the if statement. Many people do not understand why one thing can contain another thing. This is very common in programs, you will find that a function can contain a function, and a list can contain a list. If you find a structure that you do not understand, you can use the notes to write it down and understand it later.

Let's take a look at some lists and print them cyclically:
[Python]
The_count = [1, 2, 3, 4, 5]
Fruits = ['append', 'oranges', 'pears', 'audio cots']
Change = [1, 'pennies', 2, 'domes ', 3, 'quarters']
 
 
# This first kind of for-loop goes through a list
For number in the_count:
Print "This is count % d" % number
 
 
# Same as abve
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 on
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 "Elements was: % d" % I


Running result
Root @ he-desktop :~ /Mystuff # python ex32.py
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: oranges
A fruit of type: pears
A fruit of type: Required Cots
I got 1
I got 'pennies'
I got 2
I got 'domes'
I got 3
I got 'quarters'
Adding 0 to the list.
Adding 1 to the list.
Adding 2 to the list.
Adding 3 to the list.
Adding 4 to the list.
Adding 5 to the list.
Elements was: 0
Elements was: 1
Elements was: 2
Elements was: 3
Elements was: 4
Elements was: 5

Extra score exercise
1. Understand the range function.
Range ([start], stop [, step])
Generates an integer list. The default value of step is 1. If start is not assigned a value, the default value starts from 0.

2. Can you assign values to elemens directly by using range instead of 22 rows of for loop?
Elements = range (0, 6)

3. view the help document of list to see what functions are available near append.


Author: lixiang0522

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.