Several unfamiliar functions in Python
1. List ()
Convert tuples to lists
2, Random.sample (range (16), 2)
Select any of the two numbers in 0-15
3. Print output line in Python. If you want to output more than the content of the line, you can add a comma after print
4, Choice ()
The choice () function is a random item that returns a list, tuple, or string
5. Input () and raw_input ()
Input transforms the appropriate type according to the user input, and if you want to enter characters and strings, you must enclose them in quotation marks, and raw_input will be converted to a string regardless of what type the user enters.
6.format () function
Format the function of the string s format Tr.format (), with the% format can be compared with each other, through {} and: instead of%
eg
By location
In[1]: ' {0},{1} '. Format (' Kzc ', 18)
OUT[1]: ' kzc,18 '
In[1]: ' {},{} '. Format (' Kzc ', 18)
OUT[2]: ' kzc,18 '
In[3]: ' {1},{0},{1} ' format (' Kzc ', 18)
OUT[3]: ' 18,kzc,18 '
The Format function of a string can accept an unlimited number of arguments, and the position can be out of order, without or using multiple
by keyword parameter:
IN[5]: ' {name},{age} '. Format (age=18,name= ' KZC ')
OUT[5]: ' kzc,18 '
Through object properties:
Class Person:
def __init__ (self,name,age):
Self.name,self.age = Name,age
def __str__ (self):
Return ' This guy was {self.name},is{self.age} old '. Format (self.self)
IN[2]:STR (Person (' kzc ', 18))
OUT[2]: ' This guy is KZC
by subscript:
IN[7]:p =[' kzc ', 18]
In[8]: ' {0[0]},{0[1]} '. Format (P)
OUT[8]: ' kzc,18 '
Python knowledge tells us that list and tuple can be "broken" into normal parameters to the function, and dict can be broken into the keyword to the function (through *)
Format qualifier
It has a rich "format qualifier" (syntax is {} with:), such as:
Fill and align
Padding is used in conjunction with alignment
^,<,> are centered, left-aligned, right-aligned, followed by width
: The fill character after the number, can only be a character, if not specified, the default is to fill with a space
IN[15]: ' {: >8} '. Format (' 189 ')
OUT[15]: ' 189 '
In[16]:]:0>8].format (' 189 ')
OUT[16]: ' 00000189 '
IN[17]: ': a>8 '. Format (189)
OUT[17]: ' aaaaa189 '
Accuracy and type F
Accuracy is often used in conjunction with Type F
IN[44]: ':. 2f '. Format (321.33345)
OUT[44]: ' 321.33
Other types
Mainly in the system, B, D, O, X are binary, decimal, octal, hexadecimal.
12345678 |
In [
54
]:
‘{:b}‘
.
format
(
17
)
Out[
54
]:
‘10001‘
In [
55
]:
‘{:d}‘
.
format
(
17
)
Out[
55
]:
‘17‘
In [
56
]:
‘{:o}‘
.
format
(
17
)
Out[
56
]:
‘21‘
In [
57
]:
‘{:x}‘
.
format
(
17
)
Out[
57
]:
‘11‘
|
Use, the number can also be used to make the amount of thousands separator.
?
12 |
In [ 47 ]: ‘{:,}‘ . format ( 1234567890 ) Out[ 47 ]: ‘1,234,567,890‘ |
‘
A few of the guys I met today don't know how to function.