for loop, numbers, strings, and a list of built-in method

Control flow of the for loop

The basic syntax

for 变量名(会拿到容器类元素的每一个值) in 容器类元素:
    print(变量名)
for i in range(5):
    print(i)

# 打印结果:
0
1
2
3
4

while everything that can be recycled

The for loop provides a means, not dependent on the value of the index

for+break

for i in range(1, 101):    # 取1-100,range顾头不顾尾
    if i == 50:
        break    # 中断循环,只取到1-49
    print(i)

for+continue

for i in range(1, 101):
    if i == 51:
        continue    # continue跳出本次循环,不执行下面代码
    print(i)

for+else

for i in range(1, 101):
    if i == 51:
        break
    print(i)
print('没有被break中断我就出来')

The for loop will not be terminated break the code that follows else, or do not perform

The for loop print lodaing

import time
print('Loading', end='') 
for i in range(10):
    print('.', end='')
    time.sleep(0.2)
print(1, end='*')   # print后面的end控制打印的状态
print(1, end='*')
print(1, end='*')
print(1, end='*')

# 打印结果:1*1*1*1*

Built-in digital type method

Int int

Role: Description Age

Defined method:x = 10

Instructions:+ - * / % // **

Orderly or disorderly: a digital order-disorder type does not say

The variable value becomes immutable :( id value becomes, as immutable; id value becomes the same value as a variable) integer immutable

Float float

Role: Salary Description

Defined method:x = 2.1

Instructions:+ - * / % // **

Orderly or disorderly: a digital order-disorder type does not say

The variable value becomes immutable :( id value becomes, as immutable; id value becomes the same value as a variable) float immutable

String built-in method

Role: Name Description

Definition: single quote / double quotes / three single quotation / three double quotes

Instructions:

Priority grasp:

  1. Index values
s = 'hello world'
print(s[1])   # 取到e
  1. slice
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))   # 字符长度为11
  1. Member operator in / not in
s = 'hello world'
print('hello' in s)  # True
print('hello' not in s)   # False
  1. Remove the strip
s = '     hello world    '
print(s.strip())   # hello world  默认去除两端空白

s = '   **hello world   **'
print(s.strip('* '))   # hello world
  1. split Segmentation
s = 'cwz|19|180|140'
print(s.split('|'))   # ['cwz', '19', '180', '140']
  1. cycle
s = 'cwz123'
for i in s:
    print(i)

# 打印结果:
c
w
z
1
2
3

You need to know:

  1. startswith / endswith to ... begin / end 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 判断是否全为数字
print(s6.isalpha())   # False 判断是否全为字母

Other operations:

  1. find / rfind / index / rindex / count
s = 'my name is cwz, age is 20'

print(s.find('m'))  # 0  find查找索引值,找到第一个就不往后找了,找不到返回-1

print(s.rfind('s'))    # 21  rfind从右往左查找索引值

print(s.index('s'))  # 9  返回索引值

print(s.rindex('s'))  # 21

print(s.count('s'))  # 2  计数
  1. center (), light (), rjust (), zfill ()
s = 'hello world'

print(s.center(20,'*'))   # ****hello world*****  居中

print(s.ljust(20, '*'))   # hello world*********  居左

print(s.rjust(20, '#'))   # #########hello world  居右

print(s.zfill(20))   # 默认用0填充    000000000hello world
  1. expandtabs()
s = 'aaa\tbbb'
print(s.expandtabs())   # aaa     bbb 默认制表符8个字符
  1. capitalize()、swapcase()、title()
s = 'HeLlO WoRlD'

print(s.capitalize())   # Hello world  首字母大写,其他小写

print(s.swapcase())   # hElLo wOrLd  大小写转换

print(s.title())    # Hello World   每个单词首字母大写

Ordered or disordered: string can be indexed and orderly

Variable or non-variable: variable value string id value change, - "String immutable

A list of built-in method

Action: [] in the memory a plurality of values, each of the elements separated by commas

Defined method:lt = [1,23,4]

Instructions:

Priority grasp:

  1. Index values ​​/ value index modifier
lt = [1,2,3,4,5]
print(lt[1])    # 2

lt[0] = 66
print(lt)   # [66, 2, 3, 4, 5]
  1. slice
lt = [1,2,3,4,5]
print(lt[0:4])   #  [1, 2, 3, 4]
  1. Member operator
lt = [1,2,3,4,5]
print(1 in lt)  # True

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

You need to know:

  1. sort
# sort
lt = [1,23,8,9,4,21,412]
lt.sort()   # 排序
print(lt)   # [1, 4, 8, 9, 21, 23, 412]
  1. reverse
# reverse 反转
lt = [9,23,1,4,0,8]
lt.reverse()     # 列表反转
print(lt)   # [8, 0, 4, 1, 23, 9]
  1. remove
# remove
lt = [9,23,1,4,0,8]
lt.remove(9)   # 按值删除
print(lt)    # [23, 1, 4, 0, 8]
  1. pop
lt = [9,23,1,4,0,8]
lt.pop(1)   # 按索引删除值
print(lt)    # [9, 1, 4, 0, 8]
  1. extend
# extend
lt1 = [9,23,1,4,0,8]
lt2 = [1111,2222]
lt1.extend(lt2)   # 扩展列表
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()   # 清空列表
print(lt)   # []
  1. count
# count
lt = [9,23,1,4,0,1,23,4]
print(lt.count(1))   # 计数   2
  1. index
# index
lt = [9,23,1,4,0,8]
print(lt.index(0))   # 返回索引值   4
  1. insert
# insert
lt = [9,23,1,4,0,8]
lt.insert(0, 777)   # 按索引前插入值
print(lt)   # [777, 9, 23, 1, 4, 0, 8]

Ordered or disordered: energy index value, an ordered list

Variable or immutable: Variable list

Guess you like

Origin www.cnblogs.com/setcreed/p/11519824.html