Notes
Comments are the interpreted language of the code and do not affect the execution of the code. They are for programmers to see.
Comments are an important part of code!
# single line comment ''' multi-line comment Type three single quotes in a row ''' """ multi-line comment Type three double quotes in a row """
variable
What are variables?
Variables are quantities used to record the state of a transaction.
When we enter the game every day, we will find that the game character has a game level, the game backpack has various items, and the game character is in a certain position on the map. The amount of these changes needs to be recorded while the program is running. This is the concept of variables.
variable syntax
variable name = variable # Note: The equal sign is not an equal sign in the mathematical sense, but an assignment sign # means assign the value on the right to the variable on the left eg: name = 'leethon' In this way, the variable value is assigned to the variable name, and then the variable value can be found from the variable name print(name) # Running result=="leethon
Variable underlying mechanism
When we run the code, all data values will be stored in our memory, but the memory is as big as the sea, the amount of data in GB level, and the leethon in the example just now is only a few bytes (Bytes), want to find This data value is as hard as finding a needle in a haystack.
So variables are needed to store the address of this data value, a process called association.
pay attention:
- A variable name can only be associated with one variable value at a time
- A variable value may be associated with several variable values.
- Variable names cannot be related to each other
Let's combine two examples to understand.
num1 = 10 num2 = num1 print(num2) # Q: What is associated with num2 at this time?
We parse it one by one:
The first sentence: num1 is associated with 10
The second sentence: num2 is associated with num1? wrong! Because the two variable names are not related
So look at the right side of the equal sign first, num1 will find its associated 10 and assign it to num2, that is, num2 is also associated with 10
x = 10 x = x + 1 print(x) # Excuse me: Is the running result 10 or 11?
Note: = is an assignment symbol, not an equal sign in the mathematical sense, so the second sentence is not 10 = 11
Instead, add 1 to the 10 associated with x in the first sentence and then assign it to x. At the same time, x is only associated with 1 value. The first sentence is 10, and the second sentence is 11.
Naming rules for variable names
-
The python interpreter can only recognize variable names that contain a combination of letters, numbers, and underscores
-
A number cannot start a variable name
-
Do not conflict with keywords in grammar
# The left side of the equal sign is a valid variable name name = 'leethon' num_1 = 10 _info = 'information' _ = 'a value' # The left side of the equal sign is an invalid variable name num-1 = 10 1list = [] print = 'keywords' ''' In fact, you can assign the variable value to the variable name print,But the function of the keyword itself will be lost print()becomes illegal '''
ps: In fact, python supports Chinese character combinations as variable names, but programmers generally do not use them, and in some cases, errors are prone to occur.
In addition, there are some language habits that coders need to know.
First of all, the variable name needs to be familiar with the name, otherwise it may happen that when I want to call a variable, I forget the meaning of the variable, and I need to search through dozens or hundreds of lines of code.
age = 18 # When I want to call the age variable, I know the meaning of the value of this variable x = 18 # Not knowing the meaning of this x variable will bring confusion to programming.
Second, there are several common styles of variable naming:
-
Underscore: underscore between words - this is a common naming style in python
eg: son_of_beach
-
CamelCase: Capitalize the first letter of the word - the following two other languages such as JS are often used
eg: SonOfBeach
-
Small hump: consistent with hump body except for the first word
eg: sonOfBeach
constant
A constant, as the name implies, is a quantity that does not change.
In daily life, some description quantities do not change frequently, so the concept of constants is needed for recording.
Such as: National Day is October 1, it will not change.
In many languages, constants are bound to a data value, and the constant name cannot be bound to other data values.
but! !
Constants in python are just concepts, no different from variables.
We usually use all uppercase letters to distinguish constants and variables.
NATIONAL_DAY = '10 1st of the month' # If you encounter a variable with all uppercase, you should consciously not change its value. If you do: NATIONAL_DAY = 'October 1st' print(NATIONAL_DAY) # Result of operation: October 1st # You will find that the value of this constant will also be re-bound to the new value
Therefore, in python, don't touch the variable in all uppercase, because the programmer who wrote the variable is telling you that it is a constant and cannot be moved.
type of data
When recording variables or constants, the data value needs to be recorded with the appropriate type,
For example, grades can be recorded numerically,
For example, the name needs to be recorded in words,
In python, there are several commonly used data types that need to be mastered, which can help us better record data
The data type can be judged by an instruction type (data or variable)
Numeric types int, float
int is a digital integer, float is a digital floating point, corresponding to the integers and decimals we usually use
print(type(10)) # <class 'int'> print(type(10.1)) # <class 'float'>
The underlying storage methods of int and float are different, but there is no difference in the use of int and float in python, and they can be used as ordinary numbers.
string str
Strings can record our text content, including letters, Chinese characters, etc.
name = 'leethon' print(type(name)) # <class 'str'>
list list
Lists are used to record multiple different values, which can be stored in a variable
# Compare the following two ways to store multiple values name_list1 = "Zhang San Li Si Wang Erma" # string name_list2 = ['Zhang San', 'Li Si', 'Wang Erma'] # list
In terms of storage, the two name tables seem to be similar, so why do you need to specialize in a list-type data?
Here is an important thought:
For data, it is necessary to do everything possible to store, retrieve, and retrieve it easily and quickly.
All data is stored for the purpose of quickly locating the data we want in the future.
Knock on the blackboard, put pictures
So what are the advantages of fetching data from lists?
print(name_list2[0]) # Zhang San print(name_list2[1]) # Li Si print(name_list2[2]) # Wang Erma print(name_list2) # ['Zhang San', 'Li Si', 'Wang Erma'] """ variable name corresponding list, variable name[subscript]corresponds to an element in the list We can get the entire list with the variable name, You can also use variable names[subscript]get any element """
Finally, let's sort out the details and features of the list:
-
Lists are enclosed in [] and separated by commas
list1 = [Data 1, Data 2, Data 3, ...] # The list1 variable is stored in the list data type
-
Lists can store several different data in order in a continuous space
-
The list value can be marked by pressing, and the subscript of the first element starts from 0
-
The elements of the list can be another list, and the lists can be nested
[1, 2, 3, [4, 5],[6,[7, 8]]] # nesting of lists
dictionary dict
The list allows us to get the value of an element according to the subscript, but when the content of a list is very complicated, such as:
info = ['leethon', 18, 1.8, 'read'] # If the content of the above list is not explained, it is difficult to distinguish the meaning of each element of this dictionary. Even if the meaning can be distinguished, the value of info[0] cannot be associated with the meaning of the element.
So there is a dictionary data type to solve this problem.
Dictionary definition:
Format: braced frame column, with k: v In the form of key-value pairs, each key-value pair is separated by a comma {key: value, key2: value2, ......} # example info_dict = {'name': 'leethon', 'age': 18, 'height': 1.8, 'hate': 'read' }
In this way, we can clearly recognize that 18 means age, 1.8 is height, and reading is something this person hates.
However, it seems to be more troublesome to store in this way. Each data has an additional description information. At this time, it is still the same sentence:
To save, to do everything possible to save, to take, to take easily and quickly
How to get it?
name = info_dict['name'] print(name) # leethon # As long as the variable name of the dictionary is followed by [keyword], the corresponding content can be retrieved
to be continued. .