Python reserved words

Reserved words are keywords, and we cannot use them as any identifier name. Python’s standard library provides a keyword module that prints all the keywords in the current version:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>> 
Copy the code

Python annotation

Single-line comments

# This is a one-line comment.....Copy the code

Multiline comment

Python is an interpreted language: this means that there is no compilation part of the development process. Similar to PHP and Perl. Python is an interactive language: that means you can execute code directly after a Python prompt >>>. Python is an object-oriented language: this means That Python supports an object-oriented style or programming technique in which code is encapsulated in objects. ' ' 'Copy the code

Python indentation

Python’s most distinctive feature is the use of indentation to represent blocks of code, without the need for curly braces {}.

if True:
    print ("True")
else:
    print ("False")
Copy the code

Inconsistent indenting will result in a run error

if True:
    print("True")
else:
    print("False")
  print("False")
​
# File "C:/**/**/***/***.py", line **
#  print("False")
                ^
# IndentationError: unindent does not match any outer indentation level
Copy the code

Python multiline statements

If an expression/statement is very long, we can use ‘ ‘to implement a multi-line statement with newlines

total = val_1 + \
        val_2 + \
        val_3 - \
        val_4
Copy the code

In multi-line statements in [], {}, or (), backslashes are not required

In the dictionary {}

num_dict = {
    'num_1': 1,
    'num_2': 2,
    'num_3': 3
}
# print ---- {'num_1': 1, 'num_2': 2, 'num_3': 3}
Copy the code

In the list []

num_list = [
    1, 2, 3, 4,
    5, 6, 7, 8,
    9, 10, 11
]
# print ---- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Copy the code

In a tuple ()

num_tuple = (
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
)
# print ---- (1, 2, 3, 4, 5, 6, 7, 8, 9)
Copy the code

Python displays multiple statements on the same line

Use semicolons between statements; segmentation

print(num_dict); print(num_tuple); print(num_list)
Copy the code

Python print output and output do not wrap lines

Print ('python print output ')Copy the code

Print uses a newline by default. To do this, add end=”” to the end of the variable:

Print (' hello ', end = "") print (' python) # output -- -- -- -- > > hello python print (' hello ', End =" ") print('python') # output ---->> Hello *ythonCopy the code

In Python, multiple statements form groups of code

A set of statements with the same indent form a block of code called a code group.

For complex statements such as if, while, def, and class, the first line begins with a keyword and ends with a colon (:), and the next line or lines form a code group.

We call the first line and the group of code that follows a clause.

if val : 
   pass
elif val_2 : 
   pass 
else : 
   pass
Copy the code

Python import with the from… import

In Python, use import or from… Import to import the corresponding module.

Import the entire module (somemodule) in the format: import somemodule

Import someFunction from somemodule import someFunction

From somemodule import firstFunc, secondFunc, thirdFunc from somemodule import firstFunc, secondFunc, thirdFunc

Import all functions from a module in the format: from somemodule import *

Importing the SYS Module

Import sys for I in sys.argv: print (I) print ('\n python path ', sys.path)Copy the code

Import the argv,path members of the sys module

Select * from sys import argv,path from sys import argv,pathCopy the code