Data type
A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:
Integral type
- integer int can store 2^32 numbers, ranging from 2,147,483,648 to 2147483647 for example: 0,100,-100
Long integer type
- Long has a large range, and almost any large integer can be stored. To differentiate between ordinary integers, you need to add L or L after an integer. Example: 2345l,0x34al
Floating point Type
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal.
Floating-point numbers can be written in mathematical notation, such as,, 1.23 3.14 , and -9.01 so on.
But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.
- Round () built-in method
Using the round () built-in method to take the decimal point precision is commonly used
When round (float) contains only numbers, it retains 1 decimal places by default, using rounding , for example:
When round (float,ndigits), which contains numbers and precision, float represents the number, ndigits need to retain the precision, which is generally also the rule of rounding,
But met. 5 in this case, if the number of digits before the choice is even, then directly discard, if it is odd then go forward one, summing up the decimal point can only be an even;
Cases:
Boolean type
The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to indicate a Boolean value directly with True, false (note case), or by Boolean operations:
>>> TrueTrue>>> FalseFalse>>> 3 > 2True>>> 3 > 5False
Boolean values can be used and , or and not operations.
andOperations are and operations, and only all are True , the result of the and operation is True :
>>> True and TrueTrue>>> True and FalseFalse>>> False and FalseFalse>>> 5 > 3 and 3 > 1True
orAn operation is or an operation, as long as one of them is True , the result of the or operation is True :
>>> True or TrueTrue>>> True or FalseTrue>>> False or FalseFalse>>> 5 > 3 or 1 > 3True
notAn operation is a non-operation, which is a single-mesh operator that turns True into False False True :
>>> not TrueFalse>>> not FalseTrue>>> not 1 > 2True
Boolean values are often used in conditional judgments, such as:
if age >= 18: print(‘adult‘)else: print(‘teenager‘)
String type
Strings or strings (string) is a string of characters consisting of numbers, letters, and underscores.
We use "," "," ", we can define the string, but in the final display, Python for our saved strings are saved with" ", so the most canonical way to define the method is to use" ""
字符串常用方法
The string default is subscript, and the subscript is starting with 0, as follows:
Find, finds the string, if it finds the subscript information that returns the first letter of the string, if it does not match, returns-1;
split delimiter: equivalent to the AWK-F option in the shell
Join
Strip
When there is a space on the left, there are spaces behind, we want to remove the left and right space, the use of this method, this programming time for the integrity of the code, often used; Lstrip remove the left space, rstrip the right space
Python data type