Stupid way to learn Python (32)

Source: Internet
Author: User

Exercise 32: Loops and Lists

Now you should be able to write more interesting programs out. If you can keep up with it, you should have seen that combining "if statement" and "Boolean expression" can make the program do something intelligent.

However, our program also needs to be able to do the repetitive things quickly. In this exercise we will use For-loop (for loop) to create and print a wide variety of lists. In the process of doing this, you will gradually understand how they are going. Now I won't tell you, you need to find the answer yourself.

Before you start using the For loop, you need to store the results of the loop in a location. The best way to do this is to use a list, as its name implies, as a container for storing things in order. The list is not complicated, you just have to learn a little new syntax. Let's start by looking at how to create a list:

 hairs = [ " brown  , "  blond  , "  red   " ]eyes  = [ "  Brown   ", "   Blue   ", "   Green   " ]weights  = [1, 2, 3, 4] 

All you have to do is open the list with [ (left parenthesis), and then write down what you want to put in the list, separated by commas, just like the parameters of the function, and finally you need to end the definition of the right bracket with the square brackets. Python then receives the list and all the content inside it, assigning it to a variable.

Warning

This is a difficult point for people who do not program. Habitual thinking tells your brain that the earth is flat. Remember that the IF statement in the previous exercise was nested, and you might find it difficult to understand it because the average person in life would not go to a problem like this, but the problem is almost everywhere in programming. You'll see a function call another function that contains an if statement, with a list of nested lists. If you see something like this that can't be understood, write it down with a note, and divide it manually until you understand it.

Now we'll use loops to create some lists and then print them out.

1The_count = [1, 2, 3, 4, 5]2Fruits = ['Apples','oranges','Pears','apricots']3Change = [1,'Pennies', 2,'Dimes', 3,'Quarters']4 5 #This first kind of for-loop goes through a list6  forNumberinchThe_count:7     Print "This is count%d"% Number8 9 #Same as aboveTen  forFruitinchFruits: One     Print "A Fruit of type:%s"%Fruit A  - #also we can go through mixed lists too - #Notice we have the use of %r since we don ' t know what's in it the  forIinchChange : -     Print "I got%r"%I -  - #We can also build lists, first start with an empty one +elements = [] -  + #Then use the range function to does 0 to 5 counts A  forIinchRange (0, 6): at     Print "Adding%d to the list."%I -     #Append is a function that lists understand - elements.append (i) -  - #Now we can print them out too -  forIinchelements: in     Print "Element was:%d"% i
View CodeThe results you should see

Bonus points Exercise
    1. Take a look at the use of range . Check the range function and understand it.
    2. In line 22nd, you can assign elements directly to range (0,6) without using a for loop?
    3. Find the contents of the list in the Python document and read the following carefully, which actions are supported in addition to the append list?

Exercise Exercises

Stupid way to learn Python (32)

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.