Python Study to 4 basics

Source: Internet
Author: User

Basic built-in functions

Supplemental built-in functions:

1, callable () #判断 (function) can be executed or called, this function returns a value of Ture or False def F1 (): Pass print (callable (F1)) 2, Chr () #把数字转换成ASCII字符 Print (Chr (65)) 3, Ord () #把ASCII字符串转换成对应的数字 Print (Ord (",")) 4, Randrange () #需要导入 import random module, this function produces a random number I=import ran D.range (3,5) Print (i) 5, compile (s, "<string>", "exec") #把一个字符串编译成python代码 m= "Liang Waijun" s= "Print (m)" R = Compil E (S, "<string>", "exec") print (R) Exec (r) 6, eval () converts a string into a Python expression, and Python executes an expression to get the return value s= "8*8" R=eval (s) print (R) 7, EXEC () #执行python code or string, exec () has no return value 8, dir () #快速查看一个对象提供了那些功能 print (dir (dict)) 9, Help () #帮忙查找一个对象的操作源码 Help (DIC T) 10, filter (function, iterated object) #筛选, matches the condition has return value, does not meet the condition of no return value Def f2 (a): If a > 22:return True li = [11,22,33,44,55] # fi Lter Inner Loop second parameter # result = [] # for item in second parameter: # r = First parameter (item) # if r: # result () # RET     Urn Result # filter ret = filter (F2,li) #循环第二个参数, let each loop element execute a function, if the function returns a value of true, represents a legitimate element. Print (ret) lambda notation: li = [11,22,33,44,55]ret = Filter (lambda a:a >, li) Print (list (ret)) 11, Map (function, iterated object (data type for loop)) Li = [one, one, one, one, one] def F2 (a): Return a + 100result = map (F2, Li) Print (list result) uses the representation of a lambda expression: result = Map (lambda a:a + $, li) prin T (list result) Note: The essential difference between filter and map filter: The function returns true to add the element to the result Map: Add the function return value to the result 12, globals () #定义所有的全局变量13, Locals ( ) #定义所有的局部变量14, Hash () #转换成哈希值, commonly used for dictionary keys to save s = "gang" Print (Hash (s)) 15, Len () #测试字符串长度 # python 3 default by character () s = "Li Jie" Print (len (s)) # byte length, python2.7 default by byte s = "Li Jie" b = bytes (s, encoding= "Utf-8") print (len (b)) 16, sum () #求和函数 s = [11,22,33,44 ] Print (sum (s)) 17, pow () #求某个数的多少次方S = POW (2,10) print (s) 18, slince () #切片函数 s = "sssssssss" Print (S[0:2:2]) 19, s Orted () #对有序数据类型进行排序 (including: list, tuple, string) Li = (11,22,33,1,5,8,) s =sorted (LI) print (S,type (s)) 20, round () #四舍五入Round (1.4) 21,     Zip () #把相同数据类型的相同位置的元素组成一个元素 L1 = ["Alex", one, one, all,] L2 = ["is", one, 11, and] L3 = ["NB",, 22, 33,] r = Zip (L1, L2, L3) #Print (list (r)) temp = list (r) [0] ret = "". Join (temp) print (ret) 

  

Python Study to 4 basics

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.