This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

In the previous section, you looked at the basic data types and some basic operations in Python. This section gives you a general overview of statements in Python.

The familiar print() statement might look something like this. Print (‘ non-serious programmer ‘) or print(name) but we also know that print can have multiple arguments separated by commas, like this: print(name1,name2, Sep = ‘_’) means to print out name1 and name2 and connect them using ‘_’. When importing other modules, we can use the following methods:

The import moduleName from moduleName import someFunction from moduleName import someFunction1, someFunction2, someFunction3Copy the code

You can use the AS keyword to abbreviate the import.

Let’s look at some different assignment statements

X,y,z = 1,2,3 print(x,y,z) X,y = y,x #Copy the code

There is a technical term for sequence unpacking. What if this happens?

X,y,z = 1,2 X,y,z = 1,2,3,4 You can use * signs to collect extra values like x,y,*rest = 1,2,3,4 x = 1, y = 2 rest = [3,4]Copy the code

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

Chain assignment

x = y = somefunction() <=> y = somefunction() x = y <! X = somefunction() y = somefunction()Copy the code

Syntax in Python is formatted by indenting four Spaces instead of a TAB key. A TAB key is eight Spaces. Use a colon (:) to indicate that a code block is next, and indent the code in that block to the same extent.

Conditional and conditional statements: The structure of a conditional expression is either true or False, and these values represent False False False False, None,0,””,(),[],{} the rest is true. And the standard true and false values are 1 and 0. We can convert other values to True and False using the bool () method. Conditional statements are mainly

if ... :

if ... : else : ...

if ... : elif ... : else : ...

There is also a common conditional expression, similar to the ternary expression we encountered earlier

status = "friend" if name.endswith('YJK923') else "stranger"

A word about the confusing use of comparators:

== # comparison is equal = # Assignment is # is the same object, numbers and strings are not used is in # contains, for example, is present in a container or sequenceCopy the code

To get the ASCII code of the letter, use the ord() function and decode using CHR ().

Assertions: If you know that certain conditions must be met for your program to execute, you can add assert statements to your program to act as checkpoints, like this.

age = 1
assert age > 10, "the age is more then 10!"
Copy the code

The key point is whether the condition of the assertion is True or False. The string that follows is intended to illustrate the assertion.

Cycle:

# Never use a while loop if you can use a for loopCopy the code

Break out of the loop with break and end the loop with continue

Use of simple derivations (useful!) :

  1. List derivation, using []
  2. Dictionary derivation, using {} for example:
[x*x for in range(10) if x % 3 == 0] [0,9,36,81]Copy the code

Send three statements:

The pass # function exec executes the string as code, which is a statement with no return value. ecex("print('Hello,YJK923! ')") Hello,YJK923! Eval # is similar to the ECEX function, but returns a value.Copy the code