Table of Contents of Series Articles
Chapter 1: Python basic syntax
Article Directory------Python basic syntax
Preface
It mainly contains basic grammatical knowledge of Python, including literals, annotations, data types, data type conversions, identifiers, operators and data input knowledge (Note: These are the notes taken by the editor while studying the Python class of Dark Horse Programmer)
1. Literal value
1. Literal meaning: a fixed value written down in code
2. Common types: numbers, strings, lists, tuples, sets, dictionaries 2.1
Number types: including integers, floating point numbers, complex numbers, and Boolean types
2.2 String type: Also known as text , it consists of any number of strings (including Chinese, English, various strings, numbers, etc.) and needs to be wrapped in quotation marks, such as: " ", ' ', """ """
Three definition methods
1. Single quotation mark definition: name = 'Kindergarten Boss'
2. Double quotation mark definition: name = "Kindergarten Boss"
3. Three quotation mark definition: name = """Kindergarten Boss""" (can be wrapped)
Quote nesting of strings
The string itself to be defined contains: single quotes, double quotes
1. Single quote definition method can contain double quotes
2. Double quote definition method can contain single quotes
3. Use transfer characters (\) to remove the quotation marks and turn them into ordinary strings
String concatenation (cannot be concatenated with non-string types) Generally, string formatting
is performed between literals and variables or variables and variables :
1. Placeholder format: %s : % means I want to place a placeholder; s means convert the variable into a string and put it into the placeholder
2. Quick format: print(f"What to eat today {name}")
Formatting precision control:
Use "mn" to control the width and precision of data:
m: Control the width (rarely used), the set width is smaller than the number itself, it will not take effect
. n: Control the decimal point precision, the requirement is a number, it will be rounded
Format the expression:
Expression: a code statement with a clear execution result, for example: 1+1, 8*6, name = 'Zhang San' in 'Zhang San'
Case: Stock Pricing Applet
Note: Other types will be explained one by one later.
2.3 Case: Use print statement to output literals
Output integers, floating point numbers and strings
Question: Are the numbers and strings in the print statement considered literals?
Answer: Yes, it conforms to the meaning of the fixed value written in the code
2. Comments
Function: Text that explains the program code so that people can understand the function of the program code and cannot be executed
Single-line comments: start with # (recommendation: the # sign and the comment content are generally separated by a space (standard)) are
generally used for: one line/a small part of the code.
Multi-line comments: enclosed in a pair of three double quotes ("" "Comment content """);
generally used to explain: the entire Python code file, class, method
3. Variables
Variable meaning: When the program is running, it is used to record data to facilitate reuse.
Variable definition format: variable name = variable value
characteristics: the value of the variable can be changed.
The print statement outputs multiple data formats: print (content 1, content 2, …, content n), separated by commas
4. Data type
Check the data type statement: type() , fill in the brackets with the data of the type you want to query
Usage:
1. In the print statement, directly output the type [type of literal]
2. Use variables to store the result (return value) of type() [type of literal]
3. Check the data type stored in the variable
Question: Does type (variable) check the type of variable or the type of data? Answer: The type of data
stored in the variable
5. Data type conversion
The role (purpose) of data type conversion:
Common conversion statements
1. int(x): Convert x to an integer
2. float(x): Convert x to a floating point number
3. str(x): Convert the object x Convert to string, everything can be converted to string
Note: The three statements all have results (return values). You can use print to output them directly, or use variables to store the result values.
6. Identifier
Identifier: refers to a series of names used in programming, used to name variables, classes, methods, etc.
Naming rules:
1. Content is limited and can only be used: English, Chinese (not recommended), numbers (cannot start with), underscore (_)
2. Case sensitive
3. Keywords cannot be used (similar to 110, 119...)
Naming standards:
1. See the meaning of the name
2. Underline nomenclature
3. All English letters are lowercase
7. Operators
1. Arithmetic operators
2. Assignment operators
8. Data input
Input statement (function): used to obtain keyboard input. You can write prompt information directly in the input statement
. No matter what type of data is input, it will be treated as a string.