Catalog
for loop of control process
Basic grammar
for variable name (each value of container class element is obtained) in container class element: Print (variable name)
for i in range(5): print(i) # Print results: 0 1 2 3 4
while can recycle everything
for loops provide a means of not relying on index values
for+break
for i in range(1, 101): # Take 1-100, range careless if i == 50: break # Interrupt the loop, only to 1-49 print(i)
for+continue
for i in range(1, 101): if i == 51: continue # continue jumps out of this loop without executing the following code print(i)
for+else
for i in range(1, 101): if i == 51: break print(i) print('Not being break I'll come out after interruption.')
The for loop executes the code after the else without break ing, otherwise it will not execute.
for loop printing lodaing
import time print('Loading', end='') for i in range(10): print('.', end='') time.sleep(0.2)
print(1, end='*') # end after print controls the state of printing print(1, end='*') print(1, end='*') print(1, end='*') # Print result: 1*1*1*1*1*
Digital Type Built-in Method
Integer int
Role: Describing Age
Definition: x = 10
Usage: + - */%/**
Ordered or disordered: the theory that number types are not ordered or disordered
Variable immutability: (value variable id value changes, become immutable; value variable id value remains unchanged, become changeable) integer immutable
Floating-point float
Role: Describe salary
Definition: x = 2.1
Usage: + - */%/**
Ordered or disordered: the theory that number types are not ordered or disordered
Variable and immutable: (the value of variable id value changes to be immutable; the value of variable id value remains unchanged, to be variable) Floating point type is immutable
String Built-in Method
Function: Describe names
Definition: single quotation mark/double quotation mark/three single quotation mark/three double quotation mark
Usage method:
Priority:
- Index value
s = 'hello world' print(s[1]) # Get e
- Section
s = 'hello world' print(s[0:4]) # hell print(s[4:0:-1]) # olle print(s[:]) # hello world
- Length len
s = 'hello world' print(len(s)) # Character length 11
- Membership operation in / not in
s = 'hello world' print('hello' in s) # True print('hello' not in s) # False
- Remove strip
s = ' hello world ' print(s.strip()) # hello world removes both ends of the blank by default s = ' **hello world **' print(s.strip('* ')) # hello world
- split segmentation
s = 'cwz|19|180|140' print(s.split('|')) # ['cwz', '19', '180', '140']
- loop
s = 'cwz123' for i in s: print(i) # Print results: c w z 1 2 3
Need to master:
- startswith / endswith begins / ends with
s = 'hello world' print(s.startswith('hello')) # True print(s.endswith('world')) # True
- lstrip or rstrip
s1 = '**hello world**' s2 = '**hello world**' print(s1.lstrip('*')) # hello world** print(s2.rstrip("*")) # **hello world
-
lower or upper
s3 ='HellO WOrLD' print(s3.lower()) print(s3.upper())
rsplit
s4 = 'cwz|123|neo|140' print(s4.split('|',1)) # ['cwz', '123|neo|140'] print(s4.rsplit('|',1)) # ['cwz|123|neo', '140']
- join
s4 = 'cwz|123|neo|140' s_list = s4.split('|') print('*'.join(s_list) # cwz*123*neo*140
- replace
s5 = 'hello world' print(s5.replace('h', '6')) # 6ello world
- isdigit and isalpha
s6 = '122324' print(s6.isdigit()) # True judges whether it's all numbers print(s6.isalpha()) # False judges whether all letters are alphabetic
Other operations:
- find / rfind / index / rindex / count
s = 'my name is cwz, age is 20' print(s.find('m')) # Find finds the index value, finds the first one, finds it later, and finds no return-1. print(s.rfind('s')) # 21 rfind finds index values from right to left print(s.index('s')) # 9 Returns the index value print(s.rindex('s')) # 21 print(s.count('s')) # 2 count
- center(),ljust(),rjust(),zfill()
s = 'hello world' print(s.center(20,'*')) # **** ** hello world ** ** * in the middle print(s.ljust(20, '*')) # hello world********* is on the left print(s.rjust(20, '#')) # #########hello world on the right print(s.zfill(20)) # Fill in 000000000hello world with 0 by default
- expandtabs()
s = 'aaa\tbbb' print(s.expandtabs()) # AAA BBB default tab 8 characters
- capitalize(),swapcase(),title()
s = 'HeLlO WoRlD' print(s.capitalize()) # Hello world initials capitalized, other lowercase print(s.swapcase()) # hElLo wOrLd case-to-case conversion print(s.title()) # Capitalize each word in Hello World
Ordered or disordered: Strings can be indexed, ordered
Variable or Invariant: String Value Variable id Value Variable, --"String Invariant
List built-in methods
Function: [] Memory stores multiple values separated by commas
Definition: LT = 1,23,4
Usage method:
Priority:
- Index Value/Index Modified Value
lt = [1,2,3,4,5] print(lt[1]) # 2 lt[0] = 66 print(lt) # [66, 2, 3, 4, 5]
- Section
lt = [1,2,3,4,5] print(lt[0:4]) # [1, 2, 3, 4]
- Member operation
lt = [1,2,3,4,5] print(1 in lt) # True print(0 in lt) # False
- for cycle
lt = [1,2,3,4,5] for i in lt: print(i)
- append additive value
lt = [1,2,45] lt.append([12,3]) # [1, 2, 45, [12, 3]] print(lt)
- Delete value
lt = [1,2,3,4,5] del lt[4] print(lt) # [1,2,3,4]
- len
lt = [1,23,4,[8,5],'www'] print(len(lt)) # 5
Need to master:
- sort
# sort lt = [1,23,8,9,4,21,412] lt.sort() # sort print(lt) # [1, 4, 8, 9, 21, 23, 412]
- reverse
# reverse reversal lt = [9,23,1,4,0,8] lt.reverse() # List inversion print(lt) # [8, 0, 4, 1, 23, 9]
- remove
# remove lt = [9,23,1,4,0,8] lt.remove(9) # Delete by value print(lt) # [23, 1, 4, 0, 8]
- pop
lt = [9,23,1,4,0,8] lt.pop(1) # Delete values by index print(lt) # [9, 1, 4, 0, 8]
- extend
# extend lt1 = [9,23,1,4,0,8] lt2 = [1111,2222] lt1.extend(lt2) # Extended list print(lt1) # [9, 23, 1, 4, 0, 8, 1111, 2222]
- copy
# copy print([1,23,4,0].copy()) # [1, 23, 4, 0]
- clear
# clear lt = [9,23,1,4,0,8] lt.clear() # clear list print(lt) # []
- count
# count lt = [9,23,1,4,0,1,23,4] print(lt.count(1)) # Count 2
- index
# index lt = [9,23,1,4,0,8] print(lt.index(0)) # Returns index value 4
- insert
# insert lt = [9,23,1,4,0,8] lt.insert(0, 777) # Insert value before index print(lt) # [777, 9, 23, 1, 4, 0, 8]
Ordered or disordered: can index values, list orderly
Variable or immutable: list variable