Basic syntax of Python
"Life is short, I use Python", today we will learn the basic syntax of Python. Mainly include the following knowledge points:
1、 Data types and variables
2、 Naming rules
3、 Input and output
4、 Operator
type of data
1、 Number type (Number)
Python3 Number supports int, float, bool, complex. For students who are new to programming, I will give you an example to facilitate understanding of data types. For example, during Double 11, as an e-commerce operation platform, an order information form will be issued.
The quantity is the int type, and the unit price can have a decimal point is the floating point type. Whether to pay is only true (True) or false (False). Belongs to bool type. Complex is rarely used in normal work, mainly to solve math-related problems, and can be ignored for the time being.
2、 String
Strings are enclosed by **single quotes or double quotes ** in English.
E.g:
'Life is short, I use Python'
'The quality of this product is quite good, and the express delivery is awesome'
"Me and my hometown"
The information commented on major platforms can be represented by strings.
3、 List
The list can store a set of data, encapsulated by a pair of square brackets [] in English.
For example: the content of a record in the order information table above is a list.
[20201101 ,'Notebook', 8000, 1, True]
A list can store a group of data together to form a record. The scenes used in daily life are very frequent.
Through the use of the above common types of numbers, strings, and lists, most of the content in the work can be solved. The key is to choose different usage scenarios according to different data types.
variable
As the name implies, a variable is an amount that can be changed during the running of the program.
For example: 1 million sales today, 1.2 million tomorrow. We need to use variables to express.
sales = 100
sales = 120
The sales means variable. Determine the value and data type of sales based on the data on the right side of "=".
sales = 100, which means that 100 is assigned to the variable sales, and sales is the name of the variable.
Product names can also be represented by variables.
E.g:
Whether the product was successfully purchased
Naming rules
Variables are equivalent to giving each data of different data types a name for later use. So variable naming is also standardized. Everyone needs to be clear.
Identifiers can be composed of letters, numbers, and underline (_), where numbers cannot start.
Identifiers cannot be Python keywords, but they can contain keywords.
The identifier cannot contain spaces.
Strictly case sensitive
The keywords of Python are as follows:
Input and output
The output in Python uses the print() function.
E.g:
These are the basic usage methods of print(). I believe everyone has a preliminary understanding of print. We will explain in detail later.
Input in Python uses the input() function
E.g:
The input function and the output function correspond to the use. This is mainly to have a preliminary understanding of the input and output, and we will think about it later.
Operator
Operators in Python include arithmetic operators, relational operators, logical operators, and assignment operators. This time we first learn arithmetic operators. Including addition, subtraction, multiplication, division, remainder, etc. The same as the symbols in mathematics.
E.g:
I believe that everyone has learned the use of arithmetic operators through the above examples, and will continue to explain the related content of operators later.
Recommended Posts