For loops, built-in methods for numbers, strings, and lists

Posted by zapa on Sat, 14 Sep 2019 13:46:02 +0200

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:

  1. Index value
s = 'hello world'
print(s[1])   # Get e
  1. Section
s = 'hello world'
print(s[0:4])   # hell
print(s[4:0:-1])   # olle
print(s[:])  # hello world
  1. Length len
s = 'hello world'
print(len(s))   # Character length 11
  1. Membership operation in / not in
s = 'hello world'
print('hello' in s)  # True
print('hello' not in s)   # False
  1. 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
  1. split segmentation
s = 'cwz|19|180|140'
print(s.split('|'))   # ['cwz', '19', '180', '140']
  1. loop
s = 'cwz123'
for i in s:
    print(i)

# Print results:
c
w
z
1
2
3

Need to master:

  1. startswith / endswith begins / ends with
s = 'hello world'
print(s.startswith('hello'))  # True
print(s.endswith('world'))   # True
  1. lstrip or rstrip
s1 = '**hello world**'
s2 = '**hello world**'
print(s1.lstrip('*'))   # hello world**
print(s2.rstrip("*"))   # **hello world
  1. lower or upper

    s3  ='HellO WOrLD'
    print(s3.lower())
    print(s3.upper())
  2. rsplit

s4 = 'cwz|123|neo|140'
print(s4.split('|',1))   # ['cwz', '123|neo|140']
print(s4.rsplit('|',1))  # ['cwz|123|neo', '140']
  1. join
s4 = 'cwz|123|neo|140'
s_list = s4.split('|')
print('*'.join(s_list)   # cwz*123*neo*140
  1. replace
s5 = 'hello world'
print(s5.replace('h', '6'))  # 6ello world
  1. 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:

  1. 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
  1. 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
  1. expandtabs()
s = 'aaa\tbbb'
print(s.expandtabs())   # AAA BBB default tab 8 characters
  1. 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:

  1. 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]
  1. Section
lt = [1,2,3,4,5]
print(lt[0:4])   #  [1, 2, 3, 4]
  1. Member operation
lt = [1,2,3,4,5]
print(1 in lt)  # True

print(0 in lt)  # False
  1. for cycle
lt = [1,2,3,4,5]
for i in lt:
    print(i)
  1. append additive value
lt = [1,2,45]
lt.append([12,3])     # [1, 2, 45, [12, 3]]
print(lt)   
  1. Delete value
lt = [1,2,3,4,5]
del lt[4]
print(lt)   # [1,2,3,4]
  1. len
lt = [1,23,4,[8,5],'www']
print(len(lt))   # 5

Need to master:

  1. sort
# sort
lt = [1,23,8,9,4,21,412]
lt.sort()   # sort
print(lt)   # [1, 4, 8, 9, 21, 23, 412]
  1. reverse
# reverse reversal
lt = [9,23,1,4,0,8]
lt.reverse()     # List inversion
print(lt)   # [8, 0, 4, 1, 23, 9]
  1. remove
# remove
lt = [9,23,1,4,0,8]
lt.remove(9)   # Delete by value
print(lt)    # [23, 1, 4, 0, 8]
  1. pop
lt = [9,23,1,4,0,8]
lt.pop(1)   # Delete values by index
print(lt)    # [9, 1, 4, 0, 8]
  1. 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]
  1. copy
# copy
print([1,23,4,0].copy())   # [1, 23, 4, 0]
  1. clear
# clear
lt = [9,23,1,4,0,8]
lt.clear()   # clear list
print(lt)   # []
  1. count
# count
lt = [9,23,1,4,0,1,23,4]
print(lt.count(1))   # Count 2
  1. index
# index
lt = [9,23,1,4,0,8]
print(lt.index(0))   # Returns index value 4
  1. 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

Topics: Python