Python Learning (v)

Source: Internet
Author: User

                  Chapter fifth conditions, loops and other statements

5.1 Print and Import for more information

5.1.1 using a comma output

>>> print (' age ', 43,45)//You can separate multiple expressions with commas, with spaces in the middle
Age 43 45

5.1.2 an event as an import of another event

Import Somemodule

From Somemodule Improt AAA, BBB, CCC

From somemodule Import *

What if two modules have a function with the same name?

The first method can be referenced with a module:

Module1.open ()

Module2.open ()

Another way to do this is to alias the module or alias the function.

>>> Import Math as Foobar
>>> foobar.sqrt (3)
1.7320508075688772

5.2 Value Magic

5.2.1 Sequence Unpacking

>>> x, y, z = +/--Multiple values can be assigned simultaneously
>>> print (x, y, z)
1 2 3

>>> x, y //Exchange two values
(1, 2)
>>> x, y = y, ×
>>> x, y
(2, 1)

Sequence unpacking (or recursive unpacking): The sequence of multiple values is untied and placed in a sequence of variables.

This feature is especially important when a function or method returns a tuple (or other sequence or optional iteration object).

>>> Source = {' name ': ' Robin ', ' Girlfriend ': ' Marion '}
>>> Key, value = Source.popitem ()//function returns the values that are packaged in the Narimoto group.
>>> Key, value
(' Girlfriend ', ' Marion ')

5.2.2 Chain-value assignment

>>> x = y = "FSDFSD"
>>> x, y
(' fsdfsd ', ' FSDFSD ')

5.2.3 Increment Assignment

+=  /  *=

5.3 Statement block: The fun of indentation

Statement BLOCK: A set of statements that executes or executes multiple times when the condition is true. To create a statement block by placing a space in front of the code to indent the statement

In Python, a colon is used to identify the beginning of a statement block, the statements in a block are indented, and the indentation is the same. When fallback to and closed

The same amount of indentation as the block, it means that the current block has ended.

5.4 Items and conditional statements

5.4.1 This is the function of the Boolean variable

True Value: The following values are interpreted as false (false) when they are used as Boolean expressions

False None 0 (including floating point, long Integer, and other types) "" () [] {}

5.4.2 conditional execution and if statements

5.4.3 ELSE clause

5.4.4 Elif

5.4.5 Nested code blocks

Name= input ("What is your name?")ifName.endswith ('Gumby'):    ifName.startswith ('Mr.'):        Print("Hello MR gumyby")    elifName.startswith ('Mrs.'):        Print('Hello Mrs. Gumy')    Else:        Print('Hello Gumby')Else:    Print('Hello, stranger.') name= Input ('JFSDLJFS')

5.4.6 more complex conditions

(1) Comparison operators

x = = y/x<y/x > y/x >=y/x<=y/x! = y/x is y/x was not y/x in y/x not in Y

0 < Age < 100//operator can connect

(2) Equality operator

==

(3) is: same operator

>>> x = y = [[+]
>>> z = [[+]
>>> x = = y
True
>>> x = = z//equality to determine whether the values are equal
True
>>> x is y
True
>>> x is z//identity determines whether the object is the same
False

(4) In: Membership operator

(5) string and sequence comparison:

Python Learning (v)

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.