Day17 python common built-in functions

Source: Internet
Author: User
Tags iterable

There are two ways to convert any number into a string.

(1) Str () is used to convert a numeric value into a form that is easy to read. Print (str ("I am Chinese")) >>> I am Chinese

(2) repr () is the official standard for the string that is used to convert a value into an easy-to-read form. The value of STR is a string to be seen, and the value of repr is given to the machine, and any content in parentheses comes out on top of it with a layer of quotation marks.
Print (repr ("I am Chinese") >>> "I am Chinese". Python appears in any Chinese, although we see in the editor is Chinese, but secretly all is a string of code. Don't trust print! easily. Print XX shows it to you,
In fact, not the true face of XX!

. Built-in functions:

List (iterable) converts an iterative object to a table

Tuple (iterable) converts an iterative object into a Narimoto group

STR (obj) converts an object to a string (the string representation of an object)


Zip () zipper function: A function is used to take an iterative object as an argument, package the corresponding element in the object into a tuple, and then return an iterator consisting of these tuples.

If the number of elements of each iterator is inconsistent, the returned list is the same length as the shortest object, and the * operator allows the tuple iterator to be used.

l1=[1,2,3]l2=[4,5,8,3]print(list (Zip (L1,L2)))print(*(Zip (L1, L2)) Results: [(1, 4), (2, 5), (3, 8)] (1, 4), (2, 5), (3, 8)


. Data functions:

Divmod () Function: The function combines the divisor and remainder operations to return a tuple containing quotient and remainder (A//b, a% b).
Print (Divmod (3,2))
Results:

(1, 1)
Min () function:

Min (iterable, *[, key, default])

Min (arg1, arg2, *args[, key])

The function function is to take the minimum value from multiple arguments passed in, or to pass in the smallest value in an iterative object element. The default numeric type parameter, the value is small, the character type parameter, the alphabetical table is sorted by the former. You can also pass in the named parameter key, which is a function that specifies the method that takes the minimum value. The default named parameter is used to specify the value returned when the minimum value does not exist. function is the opposite of the max function.

Print (min (26,7,8,9))
Results
Can be compared between the same type

Print (min ([1,2],[1,3]))
[1, 2]
The other function of the key parameter is that the different types of objects can not be compared to the minimum value, the appropriate key function, to be able to compare the minimum value.

Print (min, ' 3 ', key = int))
Results:

1

Callable ()

#判断是否可调用的

A='s'def  func ():    print(a)print( Callable (a)# cannot invoke   Falseprint(callable (print))#  Can call  trueprint(callable (func))# can call  true

#ctrl + Left-click: Pycharm
#help: Contains all the method names and how he uses them--I don't know how to use them.
#dir: Contains only method names--to see if a method is in this data type


# f = open (' filename ', ' w ', encoding= ' utf-8 ')
#打开模式:R, W, A, RB, Wb,ab file operation is best not to use with the + number of the operation method, that is, do not read and write at the same time, this will easily cause the cursor confusion
#编码 UTF-8/GBK

Hash#Print (ID (1))#Print (ID (2))#Print (hash (' Sajghfj;eyrwodnvjnz,.jifupwk ')) #算法#Print (hash (125342))#Print (hash ((1,2,3,4)))#storage and lookup of data#module: Hashlib#{' k ': ' V '}#[1,2,3,4,5,6,]#hash ([1,2,3,4,5,6,])#Hash Determines whether a data type can be hashed#In the course of a program execution, the result of a hash of the same value is always constant # Multiple executions, the hash result for the same value may change

Use of print

def print (self, *args, sep= ", end= ' \ n ', File=none)
End= ' \ n ' if  the user does not set the word, the default is to enter the next line after printing is complete.
Sep= "  inserts a string between values, the default space
#Print progress barImport Time forIinchRange (0,101,2): Mun=i//2#number of asterisks to print # \ r The default is to return the output to the first pointer, so that later content overwrites the previous contentTo_print ='\r%s%%:%s\n'% (I,'*'*mun)ifi==100Else '\r%s%%:%s'% (I,'*'*mun)#It's just a i=100 when you're here. #还有这句话的用法 Remember    Print(to_print,sep='--', end="', flush=true)#here end is set to "" Otherwise print a line, will be wrapped onceTime.sleep (0.2)
# Print (1,2,3,4,5,sep= ' * ')  #sep是指定多个要打印的内容之间的分隔符
li=[1,2,3,5]
Print (li,sep= '//')
Print (1,2,3,4,sep= ' * ')
Print (' AA ', ' BB ', sep= ' 122 ')
#打印的结果
# [1, 2, 3, 5]
# 1*2*3*4
# AA122BB

#Simple evaluation expressions with Eval#Code2 = ' 1 + 2 + 3 + 4 '#compile2 = Compile (Code2, ', ' eval ')#Print (eval (compile2))#code3 = ' name = Input ' ("Please input your name:") '#compile3 = Compile (Code3, "', ' single ')## name #执行前name变量不存在#exec (compile3)#print (name)#exec#Eval#Compile

Common built-in functions with key

Min Max sort sorted map filter

Day17 python common built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.