table of Contents
One: Python language foundation
1.2.1 Integers, floating point numbers, complex numbers
One: Python language foundation
1.1python syntax specific
1.1.1 Notes
# 单行注释
“”“ 多行注释 “”“
1.1.2 Code indentation
In python, for class definitions, function definitions, flow control statements, exception handling statements, the colon at the end of the line and
The indentation of the next line indicates the beginning of a code block, and the end of the indentation indicates the end of a code block
1.1.3 Identifier
Identifiers are mainly used to identify the names of variables, functions, classes, modules and other objects
Naming rules for python identifiers:
1) Consists of letters, underscore "_" and numbers, the first character cannot be a number
2) Cannot use reserved words in python
3) The identifier cannot contain special characters such as spaces, @, %, ¥, $, etc.
1.1.4 Variables
Python is a dynamically typed language, which means that the type of variables can change at any time
Use the built-in function type() to return the variable type
In python, multiple variables are allowed to point to the same value. For example, if you assign two variables to 2048, and then use the built-in function id() to obtain the memory address of the variable , you will get the same result.
num1 = 2048
num2 = 2048
print(id(num1))
print(id(num2))
Output result:
1.2 Basic data types
1.2.1 Integers, floating point numbers, complex numbers
1.2.2 String type
A string is a continuous sequence of characters, and a string is an immutable sequence.
Usually use single or double quotation marks or triple quotation marks.
[Note] Strings in python also support escape characters. The so-called escape characters refer to the use of backslash "\" to escape some special characters
\n newline
\ Continuation character
\\ a backslash
1.2.3 Boolean type
Boolean type represents true or false value, and the identifiers True and False are interpreted as Boolean values
Supplement: Everything in python is an object, and all objects have a boolean value. To get the boolean value of an object, use the built-in function bool() .
The Boolean value of the following objects is False: False, 0, None, empty string, empty list, empty tuple, empty dictionary, etc.
1.2.4 Data type conversion
Python is a dynamically typed language (also known as a weakly typed language). It does not need to declare the type of the variable before using it like Java.
Although python does not need to declare the type of the variable first, sometimes it still needs to use type conversion.
int(x) convert x to an integer type
float() convert to floating point type
str() string type
ord() converts the character x to its corresponding integer value
chr(x) converts integer x to a character
1.2.5 Operator
% Remainder
// Arrangement
/ Division
[Note]: 1) When an integer or a negative number is rounded, it should be rounded down;
2) Involving the remainder operation involving negative numbers, we use the most primitive formula: remainder = dividend-divisor * quotient
print(1/2)
print(11//2)
print(9//-4)
print(-9//4)
print(9%-4)
print(-9%4)
operation result:
1.2.6 Assignment operator
The order of operations of assignment operators is from right to left. There are three common assignment forms
1) Chain assignment a = b = c = 30
2) Parameter assignment a+=10
3) Support series unpacking assignment a, b = b, a two variables exchange value
Supplement: A variable consists of three parts: identification, type, and value
==: The comparison is the value
is: The comparison is the identity (id)
a =10
b =10
print(a==b)
print(a is b)
list1 = [11,12]
list2 = [11,12]
print(list1==list2)
print(list1 is list2)
The results of the operation are:
1.2.7 Logical operators
and: logical and
or: logical or
not: logical negation
1.2.8 Bitwise operators
&: bit and
|: Bit or
^: Bit XOR
~: Invert
<<: shift left
>>: shift right
1.2.9 Input and output
The built-in input() function can receive the user's keyboard input.
Note: In python, regardless of whether the input is a number or a character, it will be read as a string. If you want to receive a value, you need to convert the received string.
age = int(input("请输入数字: "))
Two: flow control statement
2.1 Select statement
1) if statement
2) if...else... statement
3) if... elif ... else... statement
4) Nesting of if
2.2 Loop statement
for loop
A for loop is a loop that is executed repeatedly in sequence, and is usually suitable for enumerating or traversing sequences, and iterating elements in an object.
for i in 对象
We most commonly use the built-in range() function for traversal
Introduction to the range() function
1) Used to generate a sequence of integers
2) Three methods to create range()
range(stop)
range(start,stop)
range(start,stop,step)
3) The return value is an iterator object
4) in / not in judges whether the integer sequence exists/does not exist the specified integer
5) How to view the range() object, use list()
r = range(3)
print(list(r))
while loop: don't produce an endless loop
Note on the use of break and continue: in the double loop, break and continue are used to control this layer of loop