Python first,
1. Recognize programs1.1 programs
A program is a combination of the commands and data that are obtained and finally returned to us based on the instructions provided by the language in a certain logical order.
Programs are written in languages. Languages are classified into high-level and low-level languages.
Low-level languages are sometimes called machine languages or assembly languages.
There are two methods for translating advanced languages into machine code: Interpretation and compilation.
Interpreted language is executed while reading the source program. The advanced language is the source code.
Compile-type languages can be executed only after the source code is fully compiled into the target code, and do not need to be compiled later.
1.2 debugging
Program errors are called bugs, and the process of searching for bugs is called debugging ).
1.3 syntax error
The syntax refers to the structure of the program and the rules of this structure. For example ('hello, worldbank '), the single quotation marks in parentheses are paired and can be correctly executed during execution.
If you enter ('hello, world !) Or (hello, world '), an error is returned, which is a syntax error.
1.4 running error
In view of some internal and external factors such as the theoretical level of computer software and hardware, the level of industrial manufacturing, and the level of users, errors are not uncommon. the more complex the program is, the more likely it is to be abnormal.
There are many types of exceptions, such as memory exhaustion and division with Zero Divisor. To minimize the impact of errors, python provides special exception handling statements.
Runtime errors are generally easy to encounter when the amount of code is too large.
1.5 Semantic Error
Even if there is a semantic error, the order can run normally without generating any error information, but the results are inconsistent with what we expected.
In general, this error occurs because we do not have a thorough understanding of the statement running mechanism. We think it should be so executed, but it is not.
It is also possible that your solution is wrong, and the program you write is wrong.
2. Data Type
Integer 2.1
Int is usually called an integer or integer. It is a positive or negative integer without a decimal point.
2.2 floating point type
Float is composed of integer and decimal parts. It can also be expressed by scientific notation.
2.3 plural
A complex number consists of a real number and a virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual part B of a complex number are float.
2.4 data type conversion
Int (x) converts x to an integer.
Float (x) converts x to a floating point number.
Complex (x) converts x into a plural number. The real part is x, and the imaginary part is 0.
Complex (x, y) converts x and y into a plural number. The real part is x, and the virtual part is y. X and y are numeric expressions.
2.5 Constants
A constant is a variable that cannot be changed. For example, a common digital constant π is a constant.
In python, constants are usually represented by variable names in uppercase.
Python has two common variable names: PI and E.
PI: A mathematical constant pi (circumference rate, usually expressed as π ).
E: the mathematical constant e, that is, the natural logarithm.
3. Variables and keywords
3.1 Variables
The variable points to the names of various types of values. When this value is used later, you can directly reference the name without writing specific values.
In python, the environment for variables is very loose, there is no obvious variable declaration, and the type is not fixed.
You can assign an integer to a variable. If you think it is inappropriate, you can assign the string to the variable.
In python, equal sign (=) is a value assignment statement that can assign any data type to a variable.
How can I define a variable named? Enter the following information:
>>> A = 'xiaoming'
Note: A string must start with an enclosed quotation mark and end with a quotation mark.
This operation explains: a is the variable we created, = is the value assignment statement, xiaoming is the variable value, and must be marked with quotation marks.
Create variable a and assign it to xiaoming.
To output the content, perform the following operations:
>>> Print ()
Xiaoming
Assign values to variables before using them. Variables without values are meaningless, and the compiler will not compile and pass.
The same variable can be assigned multiple times and different types of variables can be entered as follows:
>>> A = 123
123
>>> A = 'abc'
ABC
This variable is a dynamic language and corresponds to a static language.
When defining a variable, the static language must specify the variable type. If the type does not match during the assignment, an error is returned.
Dynamic languages are more flexible than static languages.
You can use the built-in function type of the interpreter to confirm the variable or data type.
In the interaction mode, enter the following:
>>> Type ('hello, World ')
<Class 'str'>
The test result is of the string type (str ).
3.2 variable name
Programs often use meaningful names as variable names to mark the purpose of variables.
A variable name is a string of any length consisting of digits or characters and must start with a letter.
It is valid to use uppercase letters, but it is recommended that the variable name start with a lowercase letter. Python is case sensitive.
>>> Name = 'Study python is happy'
>>> Name = 'I agree with you'
>>> Print (name)
'Study python is happy
>>> Prit (Name)
I agree with you
The underscore "_" can appear in variable names and is often used to connect multiple phrases, such as happy_study and do_it_with_more_practice.
>>> Happy_study = 'stay hungry stay foolish'
>>> Print (happy_study)
Stay hungry stay foolish
If an invalid name is given to the variable, the interpreter displays a syntax error. As follows:
>>> 2 wrongtest = 'just for Test'
SyntaxError: invalid syntax
In this example, a syntax error is prompted. The error message is invalid because it does not start with a letter.
>>> Xiaoming @ me = 'suprised'
SyntaxError: can't assign to operator
In this example, a syntax error is prompted. The error message is that the specified operation cannot be performed because it contains an invalid character @.
Python does not allow the use of keywords as variable names, as shown below:
>>> From = 'from'
SyntaxError: invalid syntax
Python3 has 33 keywords and cannot be used as variable names.
4. Statements
A statement is a code unit that the python interpreter can run and can be understood as a executable command. Currently, we have used two types of statements: print statement and value assignment statement.
The value assignment statement has two functions: one is to create a new variable, and the other is to assign the value to the variable. Any variable must be assigned a value when used. Otherwise, it is regarded as a non-existent variable.
5. Expression
An expression is a combination of values, variables, and operators. A single value can be seen as an expression, and a single variable can also be seen as an expression.
6. Operators and operation objects
6.1 what are operators and operation objects?
An operator is a collection of special symbols. An operator object is an object connected by an operator.
Pythone supports the following 7-minute operators:
1. Arithmetic Operators 2. Comparison operators 3. Value assignment operators
4. logical operators 5. Bit operators 6. Member Operators 7. Identity Operators
6.2 Arithmetic Operators
6.3 comparison Operators
6.4 Value assignment operator
6.5-bit operator
6.6 logical operators
6.7 member operators
6.8 identity Operators
6.9 operator priority