程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - built in functions, sequences, and collections (day06)

編輯:Python

Catalog

One 、 Built in functions

Two 、 Mathematical operation function

3、 ... and 、 Type conversion function

Four 、 Sequence of operations

5、 ... and 、 aggregate

  6、 ... and 、 practice

One 、 Built in functions

 

Two 、 Mathematical operation function

(1)abs 

(2)round

(3)pow

z Is the remainder after division

(4)divmod

(5)eval

# Take the absolute value
# print(abs(-34))
# round Take an approximation
# print(round(3.66,1))
# pow Find the power
# print(3**3)
# print(pow(3,3))
# max For maximum
# print(max([23,123,4,5,2,1,786,234]))
# print(max(23,235))
# sum Use
# print(sum(range(50),3))
# eval Execute expression
a,b,c=1,2,3
print(' Dynamically executed functions ={}'.format(eval('a*b+c-30')))
def TestFun():
print(' Did I execute ?')
pass
# eval('TestFun()') # You can call functions to execute
# Type conversion function
# print(bin(10)) # Converted binary
# print(hex(23)) # Hexadecimal
# Tuple to list
tup=(1,2,3,4)
# print(type(tup))
li=list(tup) # Coercive transformation
# print(type(li))
li.append(' Cast succeeded ')
# print(li)
tupList=tuple(li)
# print(type(tupList))
# Dictionary operation dict()
# dic=dict(name=' Xiao Ming ',age=18) # Create a dictionary
# # print(type(dic))
# # # dict['name']=' Xiao Ming '
# # # dict['age']=18
# # print(dic)
# bytes transformation
# print(bytes(' I like python',encoding='utf-8'))

3、 ... and 、 Type conversion function

 (1)ord

(2)chr

(3)bin

(4)hex

(5)oct

(6)bytes

# Type conversion function
# print(bin(10)) # Converted binary
# print(hex(23)) # Hexadecimal
# Tuple to list
tup=(1,2,3,4)
# print(type(tup))
li=list(tup) # Coercive transformation
# print(type(li))
li.append(' Cast succeeded ')
# print(li)
tupList=tuple(li)
# print(type(tupList))
# Dictionary operation dict()
# dic=dict(name=' Xiao Ming ',age=18) # Create a dictionary
# # print(type(dic))
# # # dict['name']=' Xiao Ming '
# # # dict['age']=18
# # print(dic)
# bytes transformation
# print(bytes(' I like python',encoding='utf-8'))

Four 、 Sequence of operations

 (1)all

 (2)any

 (3)sorted

 (4)zip

 (5)enumerate

# Sequence of operations str Tuples 、 list
# all() # Sequence of operations str Tuples 、 list
# # all() result:bool The elements in the object are except 0、 empty 、FALSE Outside is TRUE, All elements are True
# # The results for True
#
# # print(all([])) #True
# # print(all(())) #True
# # print(all([1,2,4,False]))
# # print(all([1,2,4]))
# # print(all((3,4,0)))
# # any result:bool Similar to logical operators or The judgment of the , As long as one element is True The results for True
# # print('--------any-------------')
# # print(any(('',False,0)))
# # print(any((1,2,3)))
# # sort and sorted
# li=[2,45,1,67,23,10] # The original object
# # li.sort() #list Sorting method of Directly modify the original object
# tupArray=(2,45,1,67,23,10)
# # print('-------- Before sorting ---------{}'.format(li))
# # # # varList=sorted(li) # Ascending order
# # # varList=sorted(li,reverse=True) # null
# # # print('-------- After the sorting ---------{}'.format(varList))
# # varRs=sorted(tupArray,reverse=False)
# # print(varRs)
# # zip() : It's for packing , The element of the corresponding index position in the sequence will be stored as a tuple
# # s1=['a','b','c']
# # s2=[' you ',' I ','c He ','peter']
# # s3=[' you ',' I ','c He ',' ha-ha ',' ha-ha ']
# # # print(list(zip(s1))) Compress a data
# # zipList=zip(s2,s3) # Two parameters
# # print(list(zipList))
# def printBookInfo():
# '''
# zip Use of functions
# :return:
# '''
# books=[] # Store all book information
# id=input(' Please enter the number : Each item is separated by a space ') #str
# bookName = input(' Please enter the title of the book : Each item is separated by a space ') #str
# bookPos = input(' Please enter the location : Each item is separated by a space ')
# idList=id.split(' ')
# nameList = bookName.split(' ')
# posList = bookPos.split(' ')
#
# bookInfo=zip(idList,nameList,posList) # Packaging processing
# for bookItem in bookInfo:
# '''
# Traverse the book information for storage
# '''
# dictInfo={' Number ':bookItem[0],' Title ':bookItem[1],' Location ':bookItem[2]}
# books.append(dictInfo) # Add dictionary object to list In the container
# pass
# for item in books:
# print(item)
#
# printBookInfo()
# # enumerate Function is used to traverse a data object
# # ( As listing 、 Tuples or strings ) Combined into an index sequence , List both data and data index ,
# # Generally used in for Cycle of
#
# listObj=['a','b','c']
# # for index,item in enumerate(listObj,5):
# # print(index,item)
# # pass
# dicObj={}
# dicObj['name']=' Li Yifeng '
# dicObj['hobby']=' Sing a song '
# dicObj['pro']=' Art design '
# # print(dicObj)
#
# for item in enumerate(dicObj):
# print(item) result:bool The elements in the object are except 0、 empty 、FALSE Outside is TRUE, All elements are True
# The results for True
# print(all([])) #True
# print(all(())) #True
# print(all([1,2,4,False]))
# print(all([1,2,4]))
# print(all((3,4,0)))
# any result:bool Similar to logical operators or The judgment of the , As long as one element is True The results for True
# print('--------any-------------')
# print(any(('',False,0)))
# print(any((1,2,3)))
# sort and sorted
li=[2,45,1,67,23,10] # The original object
# li.sort() #list Sorting method of Directly modify the original object
tupArray=(2,45,1,67,23,10)
# print('-------- Before sorting ---------{}'.format(li))
# # # varList=sorted(li) # Ascending order
# # varList=sorted(li,reverse=True) # null
# # print('-------- After the sorting ---------{}'.format(varList))
# varRs=sorted(tupArray,reverse=False)
# print(varRs)
# zip() : It's for packing , The element of the corresponding index position in the sequence will be stored as a tuple
# s1=['a','b','c']
# s2=[' you ',' I ','c He ','peter']
# s3=[' you ',' I ','c He ',' ha-ha ',' ha-ha ']
# # print(list(zip(s1))) Compress a data
# zipList=zip(s2,s3) # Two parameters
# print(list(zipList))
def printBookInfo():
'''
zip Use of functions
:return:
'''
books=[] # Store all book information
id=input(' Please enter the number : Each item is separated by a space ') #str
bookName = input(' Please enter the title of the book : Each item is separated by a space ') #str
bookPos = input(' Please enter the location : Each item is separated by a space ')
idList=id.split(' ')
nameList = bookName.split(' ')
posList = bookPos.split(' ')
bookInfo=zip(idList,nameList,posList) # Packaging processing
for bookItem in bookInfo:
'''
Traverse the book information for storage
'''
dictInfo={' Number ':bookItem[0],' Title ':bookItem[1],' Location ':bookItem[2]}
books.append(dictInfo) # Add dictionary object to list In the container
pass
for item in books:
print(item)
# printBookInfo()
# enumerate Function is used to traverse a data object
# ( As listing 、 Tuples or strings ) Combined into an index sequence , List both data and data index ,
# Generally used in for Cycle of
listObj=['a','b','c']
# for index,item in enumerate(listObj,5):
# print(index,item)
# pass
dicObj={}
dicObj['name']=' Li Yifeng '
dicObj['hobby']=' Sing a song '
dicObj['pro']=' Art design '
# print(dicObj)
for item in enumerate(dicObj):
print(item)

5、 ... and 、 aggregate

# set Indexing and slicing are not supported , Is an unordered and non repeating container
# It's like a dictionary But only key No, value
# Create set
dic1={1:3}
set1={1,2,3}
set2={2,3,4}
# print(type(set1))
# print(type(dic1))
# Add operation
# set1.add('python')
# print(set1)
# Emptying operation
# set1.clear()
# print(set1)
# Subtraction operation
# rs=set1.difference(set2)
# print(rs)
# print(set1-set2)
# print(set1)
# Intersection operation
# print(set1.intersection(set2))
# print(set2&set1)
# Union operation
# print(set1.union(set2))
# print(set1 | set2)
# pop Is to take data from the set and delete it at the same time
# print(set1)
# quData=set1.pop()
# print(quData)
# print(set1)
# print(set1.discard(3)) # Specifies the element to be removed
# print(set1)
# update Two sets
set1.update(set2)
print(set1)

  6、 ... and 、 practice

# --------------------------- Homework 1 ----------------------------
# Find out 1 To 10,20 To 30,35 To 45 And
print('1 To 10 And :', sum(range(1,11)))
print('20 To 30 And :', sum(range(20,31)))
print('35 To 45 And :', sum(range(35,46)))
def sum1(m,n):
print('%d To %d And :' %(m,n), sum(range(m,n+1)))
print(sum1(1,10))
# --------------------------- Homework 2 ----------------------------
# 100 A monk eats 100 A steamed bread , Big monk 1 A meal 3 A steamed bread , Little monk 3 A meal 1 A steamed bread , How many big monks and little monks are there
def count(m,n):
"""
share m A monk ,n A steamed bread , Big monk a people , Little monk 100-a people
:param m:
:param n:
:return:
"""
for a in range(1,m):
if a*3+(100-a)*(1/3)==n:
return (a,100-a)
pass
pass
person = count(100,100)
print(' Big monk {} people , Little monk {} people '.format(person[0],person[1]))
# --------------------------- Homework 3 ----------------------------
# Specify a list , The list contains the only number that only appears in sequence , Find this number
li = [1,3,4,3,4,5,2,4,5,2,2,3]
set1 = set(li)
# print(set1)
for i in set1:
li.remove(i)
pass
set2 = set(li) # set2 It's a collection with duplicates
print(set1.difference(set2)) # Difference set
rs = set1.difference(set2)
print(type(rs))
for i in rs:
print(i)
# Law two
for i in set1: # set1 The set after data duplication in
if i not in set2:
print(i)
pass
pass


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved