Summary of Essential Sentence Usage in python Language

  • 2021-06-28 12:58:02
  • OfStack

python statement and syntax

1. Basic introduction of python simple statements


>>> while True: # Ordinary while loop 

... reply = input('Enter text:') # Called Input , pass the input to reply

... if reply == 'stop': break  # If the input is stop Exit the loop 

... print(reply.upper())    # If not entered stop on 1 Convert input directly to uppercase 

...

Enter text:abc   # This is the first 1 Input abc And see the following converted to uppercase ABC Yes 

ABC

Enter text:nihao123da

NIHAO123DA

Enter text:stop  # Here you enter 1 individual stop And the loop exits 

>>>

The code above takes advantage of the while loop of Python, which is the most common loop statement for Python.Simply put, it consists of the word while followed by an expression that turns out to be true or false, followed by a nested block of code that iterates over and over when the top test is true (True is considered to be true forever).

This Input built-in function, which is used here for console output, prints an optional parameter string as a hint and returns the reply string entered by the user.

A single-line if statement utilizing nested block-of-code special rules also appears here: the body of the if statement appears in the first line after the colon, not indented in the next line of the first line.

Finally, the break statement of Python is used to exit the loop immediately.This is the part after the loop statement is completely jumped out and the program continues to loop.Without this exit statement, the while loop will loop forever because tests are always true.


>>> while True:

...   reply = input('Enter text:')

...   if reply == 'stop': # If it is stop Exit 

...    break

...   elif not reply.isdigit(): # Print if input is not numeric Bad1 8 second 

...     print('Bad!' * 8)

...   else: # Otherwise, print the input number's 2 Power 

...     print(int(reply) ** 2)

...   # Press Enter below the test results 

Enter text:abc

Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!

Enter text:a

Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad!

Enter text:2

4

Enter text:stop

>>>

Python executes blocks of code that are what it was meant to be in the first test, executing the else section in top-down order if all tests are false.

2. Assignment, expression and printing

Assignment statements have some special features to remember, as shown below

An assignment statement establishes an object reference value, and an Python assignment statement stores the object reference value in an element of a variable name or data structure.Assignment statements always establish a reference value to an object, not an assignment object.Therefore, the Python variable is more like a pointer than a data storage area.

The variable name is created when it is first assigned.Python creates its variable name the first time an object reference value is assigned to a variable.Some (but not all) data structure elements are also created when assigning values (for example, elements in dictionaries, some object attributes).Once assigned, whenever the variable name strikes a line in the expression, it is replaced by the value it references.

The variable name must be assigned before it can be referenced.It is an error to use variable names that have not been assigned. If your view does this, Python will throw an exception instead of returning a fuzzy default value.If the default value is returned, it is difficult to find out where the input errors are in the program.

Perform some operations of implicit assignment, and in Python, assignment statements are used in many cases.For example, module imports, definitions of functions and classes, for loop variables, and function parameters are all implicit assignments.


>>> seq = [1,2,3,4]

>>> a,b,c,*d = seq

>>> print(a,b,c,d)

1 2 3 [4]

>>> L = [1,2,3,4]

>>> while L:

...  front, *L = L

...  print(front,L)

...

1 [2, 3, 4]

2 [3, 4]

3 [4]

4 []

When using an asterisked name, the number of items in the target on the left does not need to match the length of the topic sequence.Actually, asterisked names can appear anywhere in the target

Print operation

In python, print statements can be printed -- just an interface to a programmer-friendly standard output stream.Technically, this is a stream that converts one or more objects into their text representation and sends them to standard output or another similar file.

File object methods such as file.write (str). Printing operations are similar, but more focused -- the file writing method is to write strings to any file, print prints objects to the stdout stream by default, adding some automatic formatting.Unlike file methods, you do not need to convert objects to strings when using print operations.

Standard Output Stream: A standard output stream (often called stdout) is simply the default place to send text output from a program.With standard input and error streams, it is only one of the three data connections created when the script is started.Standard output is usually mapped to a window that starts the Python program unless it has been redirected to a file or pipe in the shell of the operating system.

if Test and Grammar Rules

Python Grammar Rules

There are some features of the Python syntax that we need to know: Statements run one by one: python1 generally executes statements in nested blocks in a file from beginning to end in order, but statements like if (and loops) cause the interpreter to jump within the program.Because the path through which Python passes through a program is called a control process, statements that affect it, such as if, are often called control process statements.The boundaries of blocks and statements are automatically detected.There are no braces or "begin/end" separators in the block of Python;Conversely, Python combines statements within a nested block using indentation of the statement under the first line.Similarly, Python statement 1 usually does not terminate with a semicolon, and the end of a line is usually the end of the statement it writes.

Compound statement = first line +':'+ indent statement.All compound statements in Python follow the same format: the first line terminates with a colon, followed by one or more nested statements, and is usually indented under the first line.Indentation statements are called blocks (sometimes groups).In the If statement, the elif and else clauses are part of if and the first line of its own nested block.Blank lines, spaces, and comments are often ignored.Blank lines in the file are ignored (but not at the interactive mode prompt).Spaces in statements and expressions are almost ignored (except within string constants and when used for indentation).Comments are always ignored: they start with the #character (not within a string constant) and extend to the end of the line.The document string (docstring) is ignored, but saved and displayed by the tool.Another comment supported by Python is called a document string (docsting for short).Unlike the # comment, the document string is retained at run time for viewing.Document strings appear only in program files and strings at the top of some statements.Python ignores these, but they are automatically attached to objects at run time and can be displayed by documentation tools.

while and for loops

The while statement is the most common iteration structure in the Python language.


>>> x = 'spam'

>>> while x:

...   print(x,end='')

...   x = x[1:]

...

spampamamm>>>

Note that the end=''keyword parameter is used here so that all outputs appear on the same line, separated by spaces;

In python:

break: Jump out of the last loop (skip the entire loop statement)

continue: Jump to the beginning of the nearest loop (to the first line of the loop)

pass: Do nothing, just empty placeholder statements

Loop else block: Executes only when the loop leaves normally (that is, no break statement is encountered)


Related articles: