[Python ③] basic python data types, variables and constants, python
Basic Data Type
In Python, the following types of data can be processed directly:
Integer
Python can process Integers of any size, including negative integers. The writing method in the program is the same as that in mathematics, for example: 6,-666
, 8888 ......
The computer uses binary. Therefore, it is convenient to use hexadecimal to represent integers.0x
Prefix and 0-9, a-f, such
>>> 0xaa6643622>>> 0xaf56dc11491036
Floating Point Number
Floating Point number, that is, decimal number. Floating point numbers can be written in mathematics, such0.681
,-6.58 ...... However
A large or small floating point number must be represented by the E notation (Scientific Notation), replacing 10 with e, and 3.5x109 is 3..5e9。
Integers and floating-point numbers are stored in different computers. Integer operations are precise, while floating-point operations may have rounding errors.
Boolean Value
A boolean value onlyTrue
,False
Two values, Boolean value can be usedand
,or
Andnot
Operation.
String
>>> 'Let\'s go!'"Let's go!"
Escape characters\
Can escape many characters, such\n
Line feed,\t
Tab, Character\
It also needs to be escaped, so\\
The character is\。
>>> Print ('newline \ nTab \ t \ ') newline Tab \
Python is also allowedr''
Indicates''
Internal strings are not escaped by default.
>>> Print (r'newline \ nTab \ t \ ') newline \ nTab \ t \\
Python is also allowed'''...'''
The format of '\ n' indicates multiple lines of content, which simplifies a bunch of' \ n' and likes one.
>>> Str = ''' the spring-blown petals are not produced for future fruits, but just for the sake of Instant happiness. -- The stream collection ''' >>> str 'petals blowing in spring, \ n is not born for the future fruit, \ n is just for the moment. \ N -- the stream collection of Tagore >>> print (str) Spring is blowing the petals, not for the future fruit, just for the sake of a moment. -- Tagore stream collection
Null Value
A null value is a special value in Python.None
.None
Cannot be understood0
Because0
It makes sense.
Variable
Python variables do not need to be declared. You can enter them directly:
>>> py=6.88>>> print(py)6.88>>> print(type(py))<class 'float'>
Then you have a variable py in your memory. Its value is 6.88, and its type is float (floating point number ). Before that, you do not need to make any special declaration, but the data type is automatically determined by Python.
Here, we learned a built-in function type () that can query the type of a variable.
Constant
A constant is a variable that cannot be changed. For example, a common number π is a constant. In Python, variable names in all uppercase are usually used to represent constants, PI, and so on.
Small Division
The division of integers in python3.X is different from that in 2.x.
In 3.x(Real Division: returns true values regardless of integer or floating-point type ):
>>> 5/22.5>>> 5.0/22.5>>> 5/2.02.5>>> 5.0/2.02.5
2. X(Traditional Division ):
>>> 5/22>>> 5.0/22.5>>> 5/2.02.5>>> 5.0/2.02.5
>>> From _ future _ import division # precise division >>> 5/22. 5
Python OPERATOR:
(1): single object OPERATOR: positive (+), negative (-)
(2): binocular operators: +,-, *,/, % ,**,//
About floor Division(Based on the Python Version, the Python Version <= 2.6 is the traditional division: INTEGER: removes the decimal part, and returns an integer ):
>>> 5//22
Summary of the day
Understand the basic data types, constants, and variables of python. Assign values to variables, and the difference between division in python2.X and 3.x.
Python, what are Python constants and variables?
There is no constant in the world of python, and only variables that you take the initiative not to modify are disguised as constants.
How does one define the data type in python, that is, declare the variable type?
This is not a declaration type problem, because a and B are already int when you call, so the number to be divided is only an integer. If you declare C in time, the result is only 0.0.
Suggestion:
Def test (a, B ):
A1 = float ()
B1 = float (B)
C = a1/b1
Return c