Statements in Python

Source: Internet
Author: User

The previous section has already learned about the basic data types in Python and some basic operations, and this section outlines the issues related to the statements in Python.

The print () statement that we are familiar with may be the case. print(‘YJK923‘) or print(name)but we also want to know that the print function can contain multiple parameters, where the parameters are separated by commas, like this, print(name1,name2,sep = ‘_‘) this sentence means to print name1 and name2, and use ' _ ' connection.
We can use the following methods when importing other modules:

Import ModuleName  from Import someFunction  from Import Somefunction1,somefunction2,somefunction3

You can use as keywords for abbreviations when importing.

Here's a look at some different assignment statements

X, y,z = 1 2 3
用法在这呢!x,y = y,x # 轻轻松松就交换两个值有木有!

The above usage has a professional noun, called sequence unpacking if this situation changes what to do?

# error due to a 3 variable to give only 2 value is definitely not AH.  #  error because there are 3 variables to give 4 values do not fit Ah! 
# you can use the * number to collect extra values such as x, Y,*rest= 1,2,3,4= 1= 2 = [3,4]

So we can use * the number to collect extra values! The variable with an asterisk eventually contains a list.

Chained assignment

x = y = somefunction ()<=>== y<!=>== Somefunction ()

The syntax in Python is formatted by indentation, and the indentation is 4 spaces instead of a TAB key. A TAB key is 8 spaces. Use a colon (:) to indicate that a block of code is next and to indent the code in that code block to the same degree.

Conditional and conditional statements: The structure of the conditional expression is either true or FALSE, and these values mean that the False,None,0,"",(),[],{} rest of the false is true. The standard true and false values are 1 and 0. We can use the bool () method to convert other values to True and False.

conditional statements are mainly  

if  ... :   if Else  : ...   if elif Else

There is also a common conditional expression, similar to the three-mesh expression we encountered before

" friend " if name.endswith ('YJK923'else"stranger" 

Talk about the use of easily confusing comparators:

# Compare Equality =  #  assignment Operation is#  Whether it is the same object, no is used between numbers and strings inch # whether it contains, for example, whether a container or a sequence exists

The ASCII code to get the letters can be ord() decoded using a function chr() .

Assertion: If you know that a particular conditional program must be met to execute, you can add a statement to the program to act as a assert  checkpoint, similar to this.

Age = 1assert' Theage was more then10! "

The key point is that the condition of the assertion is True or False. The following string is followed to illustrate the assertion.

Cycle:

 while Loops  for Loops
# you can use a for loop instead of a while loop.

Jump out break of the loop and end this cycle usingcontinue

Simple derivation of use (very useful!) ):

    1. List derivation, using []
    2. Dictionary derivation, using {}
      Example:
 for inch if x% 3 = = 0][0,9,36,81]

Send 3 statements out of the way:

Pass  # nothing to do, placeholder use similar to  Java//TODOdel#  del deletes just the reference, and the specific value is still waiting for the GCexec#  The function exec executes the string as code, which is a statement with no return value. Ecex ("print (' hello,yjk923! ') "  #  similar to the Ecex function, but with a return value. 

Statements in Python

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.