The road to Python learning-----basic syntax

python version 3.7.7

Python identifiers

In python, identifiers are composed of letters , numbers , and underscores , and are case-sensitive. All identifiers can include English, numbers, and underscores, but cannot start with numbers .

Usually starting with a single underscore, it represents the class attributes that cannot be directly accessed, and needs to be accessed through the interface provided by the class, and double underscores represent the private members of the class

Python can display multiple statements on the same line by separating them with a semicolon;.

print ( 'hello' ) ; print ( 'world' ) ; 
'''
hello
world
'''
  • 1
  • 2
  • 3
  • 4
  • 5

Python reserved characters:

All Python keywords contain only lowercase letters:

and withNone is like C/C++ NULLnot not
assert assertion, to determine the truth or falsehood of a variable or expressionfinally is used for exception statementsor or
break break loop statementfor loop bodypass empty statement
**class **classfrom import moduleprint print
continue to jump out of this loop and enter the nextglobal defines global variablesraise exception throw
def defines a function or methodif conditional statementreturn return
del deletes the value of a sequence or variableimport import moduletry contains exception statements that may occur
elif conditional statementin judges that the variable is in the sequencewhile loop body
**else** conditional statementis determines whether a variable is an instance of a classwith Simplified Pyton Statement
except contains code for what to do after the exception is caughtlambda defines anonymous functionyield is used to sequentially return values ​​from a function
import keyword
 print ( keyword . kwlist ) 
'''
['False', 'None', 'True', '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']
'''
  • 1
  • 2
  • 3
  • 4
  • 5

The Pyhton language does not use {}, but instead uses indentation . If there is no strict indentation, an error will be reported! .

It is recommended to use a single tab or two spaces or four spaces at each indentation level , remember not to mix

Multi-line statement:

Python generally ends a statement with a newline. However, a slash () can be used to divide a line of statements into multiple lines for display

y = a * x * x + \
	b * x + \
	c
  • 1
  • 2
  • 3

Notes:

Single-line comments use #

#!/usr/bin/python 
# -*- coding: UTF-8 -*- 
# File name: test.py
  • 1
  • 2
  • 3

Use three single quotes (''') or three double quotes (""") for multi-line comments

'''
For multi-line comments, use single quotes.
For multi-line comments, use single quotes.
'''

"""
For multi-line comments, use double quotes.
For multi-line comments, use double quotes.
"""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

keyboard input:

a = input ( "Please enter a number\n" ) 
print ( a ) 
'''
Please enter a number
5
5
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

print output:

The default output of print is a newline. If you want to achieve no newline, you need to add a comma (,) at the end of the variable

x =  "a" 
y =  "b" 
print ( x ) 
print ( y ) 
print ( " --------- " ) 
print ( x ) , print ( y ) 
print ( x , y )

'''
a
b
 ---------
a
b
ab
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Number (Number) type:

There are four types of numbers in python: integer, boolean, float and complex

  • int (integer), such as 1, has only one integer type int, which is represented as a long integer, and there is no Long in python2.
  • bool (Boolean), such as True, False.
  • float (floating point number), such as 1.23, 3E-2
  • complex (plural number), such as 1-3j

String

Single and double quotes are used exactly the same in python.

Strings can be concatenated together with the + operator and repeated with the * operator

a =  "I " 
b =  "Love " 
c =  "You" 
fin = a + b + c
 print ( fin ) 
fin = a * 3 
print ( fin ) 
'''
I Love You
III
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Strings in Python cannot be changed (as immutable data type)

Python does not have a separate character type, a character is a string of length 1 .

Strings in Python have two indexing methods, starting with 0 from left to right, and starting with -1 from right to left (because strings are special tuples, and operations such as sequence slicing can also be performed)

The syntax format of string interception is as follows: variable [head subscript: tail subscript: step size]

love =  "I Love You!" 
print ( love [ 0 ] ) 
print ( love [ - 2 ] ) 
print ( love [ 0 : 7 ] )      #The length of the interception is tag_tail - tag_head - 1, that is, from 0 to (7- 1) 
print ( love [ 0 : 10 : 2 ] ) 
'''
I
u
I Love
ILv o
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

import and import...from

Use import or from...import in python to import the corresponding module.

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

Import a function from a module in the format: from somemodule import somefunction

Import multiple functions from a module, the format is: from somemodule import firstfunc, secondfunc, thirdfunc

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

from sys import argv , path   # import specific members
 
print ( '================python from import============================== =======' ) 
print ( 'path:' , path )  # Because the path member has been imported, there is no need to add sys.path when quoting here
  • 1
  • 2
  • 3
  • 4

Related: The road to Python learning-----basic syntax