Linux Next Python learning Note 2: conditional judgment, looping

Source: Internet
Author: User

First, condition judgment

If statement

For example, to enter user age, print different content according to age, in a Python program, you can use the IF statement:

 
  

Note: The indentation rules for Python code. Code with the same indentation is treated as a block of code, and the 3-line print statement above forms a block of code (but excludes print on line 4th). If the IF statement evaluates to True, the code block is executed.

Indent please strictly follow Python's customary notation: 4 spaces, do not use tab, do not Mix tab and space, otherwise it is easy to cause syntax errors due to indentation.

In Vim under Ubuntu need to set tab default to 4 spaces, you can find the ~/.VIMRC file and set tabstop=4

#使用vim打开 ~/.VIMRC file, if not, create vim ~/.vimrc# open and enter set tabstop=4

Note : The IF statement is followed by an expression, and then begins with the : representation code block.

If you hit the code in a python interactive environment, pay special attention to the indentation, and the exit indentation requires more than one line of return.

If-else statements

Note:The Else statement is also followed by: used to represent the beginning of a block of code.

If-elif-else statements

The equivalent of multi-layered judgment, in order to facilitate the visual effect, we use raw_input to receive our input, in order to see the effect in time.

In a different way.

Run after Save

Second, the circulation

The previous article mentions the collection type list and tuple, and we need to loop when we need to access the contents of the collection in turn.

For loop

While loop

Break Exit Loop

Continue continue circulation

Loop nesting

Iterative loops

In Python, iterations are always taken out of the element itself, not the index of the element.

For an ordered set, the element is indeed indexed. Sometimes we really want to get the index in the for loop, what do we do?

The method is to use the enumerate () function :

Using the enumerate () function, we can bind both index and element name in the For loop. However, this is not a special syntax for enumerate (). In fact, the enumerate () function puts:

[' John ', ' json ', ' Jay ']

Into something like this:

[(0, ' John '), (1, ' json '), (2, ' Jay ')]

Therefore, each element of an iteration is actually a tuple.

Iterative Dict

We have learned that the Dict object itself is an iterative object , iterating the dict directly with a for loop, and can get a key for dict each time.

What should we do if we want to iterate over the value of the Dict object?

The Dict object has a values () method , which converts the dict into a list that contains all value, so that we iterate over each value of Dict:

The Dict object also has a itervalues () method , and the effect of the iteration is the same as values ().

What is the difference between the two methods?

1. The values () method actually converts a dict to a list containing value.

2. However, the itervalues () method does not convert, and it takes value from dict in sequence during the iteration, so the Itervalues () method saves the memory needed to generate the list, compared to the values () method.

3. Print itervalues () find it returns a <dictionary-valueiterator> object, which shows that in Python, thefor loop can be used to iterate beyond list,tuple,str. Unicode,dict, and so on , any iterator object can be used for a for loop, and the inner iteration of how we usually don't care.

If an object says that it can iterate, then we iterate it directly with a for loop, which is an abstract data operation that does not require any data inside the iteration object.

The Dict object also has an items () method ,

You can see that the items () method contains key and value, actually converting the Dict object to a list that contains a tuple.

The items () also have a corresponding iteritems (), Iteritems () does not convert the dict to a list, but is continuously given a tuple during the iteration, so Iteritems () does not occupy additional memory.

Linux Next Python learning Note 2: conditional judgment, looping

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.