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

Fundamentals of python (III)

編輯:Python

List of articles

  • Public operation
    • Operator
    • Public methods
    • Container type conversion
  • The derived type
    • List derivation
    • Dictionary derivation
    • Set derivation

Public operation

One General containers support The operation of is called public operation , Include Operator , Public methods and Container type conversion etc. .

Operator


+: Merge function ( Dictionary not supported )
*: Replication effect


Public methods


2.1 len():

2.2del()

2.3 max()

2.4min()

2.5 range(): Generated from start Start to end end , Compensation for step Can be cut
range()⽣ The generated sequence does not contain end Numbers . If you don't write the beginning , The default from the 0 Start . If you don't write the step size , The default is 1.

2.6 enumerate()

The return is Tuples , The first data of the tuple is the subscript corresponding to the iteration object data , The second data is the data of the original iteration object .

enumerate( Traversable objects ,start=0)
#start Parameter is used to set the subscript starting value of traversal data , The default is 0
list1 = ['a', 'b', 'c', 'd', 'e']
for i in enumerate(list1):
print(i)
for index, char in enumerate(list1,start=1):
print(f' The subscript is {
index}, The corresponding character is {
char}')

Container type conversion

tuple(): Convert a sequence to a tuple

list(): Convert a sequence to a list

set(): Convert a sequence into a set
Quickly complete the list de duplication + Subscripts are not supported

The derived type

effect : Simplify the code

List derivation

effect : Create a with an expression Regular pattern Or control a regular list ( List generator )

# Create a 0-10 A list of 
list1=[]
i=0
while i<10:
list1.append(i)
i+=1
print(list1)
list1=[]
for i in range(10):# The default from the 0 Start , In steps of 1
list1.append(i)
# Implementation of list derivation 
list1=[i for i in range(10)]
print(list1)
# first i It refers to the return value of the list , the second i refer to 0-9
# belt if List expressions for 
list1 = [i for i in range(0, 10, 2)]
print(list1)
list1 = [i for i in range(10) if i % 2 == 0]
print(list1)

Multiple for Loop to implement the list derivation :

list1=[]
for i in range(1,3)
for j in range(3)
list.append(i,j)
list1 = [(i, j) for i in range(1, 3) for
j in range(3)]
print(list1)

Dictionary derivation

The function of dictionary derivation : Quickly merge lists into dictionaries or extract from dictionaries ⽬ Bid data .

Set derivation


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