Integral type of 1.python
During programming, it is often necessary to work with some numbers, and at most sometimes to find their absolute values, as shown below:
A = 100
b =-20
Print (a)
Print (b.__abs__ ()) # #这种以下划线开头的很少用
Print (ABS (a) + ABS (b)) # #这种函数形式经常用
And when we get variables, especially between shaping and string, the problem of type conversion between string and integer variables is most likely to occur.
Integral type in the operation, often encountered the problem of decimal point accuracy, such as: a = 1/2, that a equals how much? 0.5, huh?
No, a = 0,python choice is to go to a method, regardless of what is behind the decimal point, all Discard
If you want to keep a few digits behind the decimal point, it involves floating point numbers.
2. Python floating-point type
Round () built-in method
When round (float) contains only numbers, the default is 1 decimal places, which are rounded.
Example:
Round (2.5)
3.0
Round (1.5)
2.0
When round (float,ndigits) contains numbers and precision, float represents the number, and ndigits represents the precision that needs to be retained.
The general situation is also rounded, but encountered. 5 When this happens, if the number of digits before the choice is even,
is discarded directly, if it is odd, go up one.
In general, the last digit of decimal precision can only be an even number.
Example:
Roude (2.555, 2) #奇数
2.56
Roude (2.545, 2) #偶数
2.54
Both 6 and 4 are the last digits of the decimal precision, and are even.
Summary: First to rounding the operation, if the decimal point accuracy of the last one is even, meet the conditions;
If the last digit of the decimal precision is odd after rounding out, discard all digits after the original decimal point precision,
And the last digit that guarantees the decimal point precision must be an even number.
3.python of Boolean type
The Boolean is two values, one is false and the other is true
In general, when assigning a value, it is a return value of the final judgment, and then the value is manipulated.
4.python string
Strings are often used, we use "," "," "" "", can define a string,
However, the result of the final (print) output is a string that defaults to ' (single quotation marks). [Python default]
Therefore, the most canonical method is to define a string with "' (single quote)
For strings, there are many ways to manipulate strings
A = ' sssss '
Print (dir (a))
The function dir () is used to see how Python data types are printed out in many ways, but we
Just use the most common methods, and later use other methods, we can again through this method query.
The most common method of string
The string is labeled by default, starting with [0], as follows:
s = ' Hello '
Print (S[0], s[1], s[2])
Find Method:
Finds the string if it finds the subscript information that returns the first letter of the string, and returns 1 if it does not match
Example:
A = ' asdf1234ccasdfasdffkjlaklsjljlksjf '
Print (A.find (' 1234 '))
Get the subscript that is the string ' 1 '
What if it doesn't exist?
Print (A.find (' q1234 '))
Get-1
Add: Find also has the RFind method, from the right to start looking
Replace method:
A = ' asdf1234ccasdfasdffkjlaklsjljlksjf '
Print (A.replace (' 1234 ', ' 4321 '))
Get: ' asdf4321ccasdfasdffkjlaklsjljlksjf '
Split method:
A = ' asdf1234ccasdfasdffkjlaklsjljlksjf '
Print (A.split (' d '))
The string, with the string ' a ' as the delimiter, is cut into a list.
[' as ', ' F1234ccas ', ' FAs ', ' FFKJLAKLSJLJLKSJF ']
Join method:
Connect the list generated by the previous example with ' Hello '
c = [' as ', ' F1234ccas ', ' FAs ', ' FFKJLAKLSJLJLKSJF ']
Print (' Hello '. Join (c))
Get: ' Ashellof1234ccashellofashelloffkjlaklsjljlksjf '
Strip method:
b = ' asdf kjhgfd weryui MNBVC '
(The left and right side of the space, this situation is often encountered in the work, need to remove the two spaces, in the case of extracting strings)
Want to remove the left space
Print (B.strip ())
Get ' asdf kjhgfd weryui MNBVC '
Other than that:
Print (B.lstrip ()) removes the leftmost space in the string
Print (B.rstrip ()) removes the rightmost space from the string
This will make the code more prescriptive, and if you don't need a space, I'll take the string out and remove the space.
Habit will avoid many problems in the future.
Format method:
name = ' Lounan '
Print (' hello ' + name)
Print (' Hello%s '% name) #%s represents the string%d representing the shaping%f for floating point type
Print (' Hello {0} '. Format (name))
The Format method has the highest execution efficiency for the above three types of execution.
Multiple variables, using the Format method
Variable:
name = ' Lounan '
Age = ' 20 '
Output Style 1:
Print (' Hello {0}, my age Is:{1} '. Format (name, age)
Output Style 2:
Print (' Hello {name}, my age Is:{age} '. Format (name = ' along ', age = ' + ') # #更换了原变量的值
5. Comments for the string:
Using # Annotations in Python, #号后面的内容都会被解释器忽略
Typically, a string ("" "" "") is added after the "Preset header text" to explain the project or the action or explanation of the file.
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2017/10/21 7:52
# @Author: Nan
# @File: aa.py
"" "This Is Test file!" "
# test File
This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1974780
8. Python Data type