Operations on Python basic data types

    Before learning Python syntax, please be sure to note that Python defines the code level through indentation, that is, the code at the same level is left-aligned, and the code block at the next level will have 4 spaces compared with the current code block. Indentation.

    The number of indented spaces here is customary. Of course, if the number of indented spaces is 3 or 5, there will be no grammatical problems, but this will cause some confusion to the reading and maintenance of the code.

    The basic elements of Python programs are various types of data. The more common basic data types are integer, floating-point, string and boolean. Except for string data, which will be described in the next section, in the following In the DataDemo.py case, we will demonstrate operations for various basic data types.    

1	age=16
2	print(age+1) #17
3	returnVal=0xff
4	print(returnVal) #255
5	price=20.8
6	print(20.8*2) #41.6
7	lightSpeed=3e5 #300000km
8	print(lightSpeed*10) #3000000
9	oneNm=1e-9
10	print(oneNm*5) #5e-09
11	isExpensive=price<30
12	print(isExpensive)#True

    Before analyzing the code, please pay attention to two points. First, because all codes are at the same level, they are aligned to the left without indentation. Second, after lines 2 and 4, for example, , We use # to write single-line comments.

    In line 1, we define an integer variable age and assign it to 16. In the print statement on line 2, we add 1 to age, so the output is 17. Before the returnVal variable in the third line, we use the 0x prefix to represent the hexadecimal number, and through the print language in the fourth line, we can see that the value of the variable is 255. Above we demonstrated the operation on integer variables.

    In line 5, the price we defined has a decimal point, which is called floating-point data. In addition to directly defining it with a decimal point, we can also define it with e. For example, in line 7, we define light through 3e5. The speed of, the unit is kilometers, which means that the number is 3 followed by 5 zeros. In the 9th line, we define a length of one nanometer through 1e-9, which means that the specific unit is 1 times 10- 9th power. In the above, we have demonstrated the operation of floating-point data, and the output results of the sixth line, the eighth line and the tenth line can all be seen in the comments after the corresponding line.

    In line 11, the result of the isExpensive variable is the boolean value True, because price is less than 30. This result can be seen from the output statement in line 12. Boolean variables can be directly assigned to True or False, or they can be assigned by expressions using statements similar to the 11th line.

    The above code demonstrates the operation for the basic data types. Please note that since there is no need to specify the data type when defining variables in Python, for example, in the first line of the definition of age, there is no need to use int age=16 to define, so in the definition and Don't confuse the definition when using it. For example, in the future, don't use the statement of age=16.5 to assign a floating-point number type to the age variable, because the maintainability of the code becomes worse.

    Moreover, when defining variables, the variable name should be as meaningful as possible. For example, from the variable name price defined in line 5, it can be seen that it is floating-point data. It is not recommended to use meaningless words such as a or b to define variables.

Guess you like

Origin blog.csdn.net/sxeric/article/details/113747189