字符串 列表 知识回顾

一,列表:

用途 用来存放多个值

定义[] 中以,号分开多个元素,列表内元素无类型

L1=[‘a’,’b’,’c’]

L1=list(‘hello’)

Print=(l1)    

优先掌握

1 索引取值

L=[‘a’,’b’,’c’]

Print (l[-1])

L[0]=’a’

Print

2,切片:

stue =【‘alex’,’egon’,’wx’】

print(stus【1:3】)

3,长度 len

4,成员运算:in和not in

Print(‘alex’in stus)

Print(‘alex’ not in stue)

5,追加

Stus=[‘alex’]

Stus.appand(‘wup’)

Stus.apped(‘pas’)

Print(stus)

插入

 Stus=【‘alex’,’ahh’,‘hfd’】

 Stus.Insert(1,’规划局’)print(stus)

6 删除

Stus=【‘alex’,‘egon’】

Stus.Remove(‘alex’)成员删除。单纯删除

Stus.pop(l) 不加()默认 尾  取走一个值

7,循环stus=[‘alex’,’egon’]

i=0

while<len(stus):

print(stus[i])

i+=1 (依赖索引)

不依赖索引

For item in stus:

Print(iten)   

补充for 循环

for i in range(0,5,2):#0 2 4

print(i)

for i in rang(10):默认从0开始

pring:(i)

for i in range(10,-2,-1)顾头不顾尾

printi(i)

 

stue=[‘alex’,’age’]

for i in range(len(stus)):

需要掌握的知识 stus clear()清空

print (stus)

l=stus.Copy

print(l)

stus.count()

print (stus.count(alex))

stus.extend([‘a’,’b’])     加后面

stus.Index()         取元素索引

stus.Reverse  

print(stus.everse())   到反过来

stus.Sort()l=[1,12,3 ]反向排

s1=’hello’

s2=’z’         比字母  列表按位置比较

(前提只能同类之间比大小对于引索引的按位置一对应比较)

练习 :对例:先进先出 l1[]

入队

L1.appent(‘first’)

L1.appent(‘seod’)

Print(li)

出队 li.pop(0)

Print(l1.pop(0))

  

字符串需要掌握的操作

Lstrip 左rstrip 右

 

1,  开始

Startuith,开始endswith 结果

Print(‘alex is sb‘ startuith(‘alex’))

2 format 格式化

第一种

‘my name ’.forat()

S1=’my name is%s my age is %s’(‘eagon’,18)

S2=’my name is[] myageis {}’.format(‘ego’,18)

第二种

S2=”my name is {1} my age is {0}”,format(‘alex’,18)

第三种

S2=’my name is{x} my age is {y} ’,format(x=’alex’,y=18)

3(ˉ▽ ̄~) 切~~片

Split / replit

Cmd=’geyy’:’ghg’

Print(cmd.split(‘:’,1)) (ˉ▽ ̄~) 切~~一次

4 拼接 join

Cmd=’egon:23:sereyr:ghfg’

L=cmd.spril(‘:’)

Res=().join(1)

Print(es.type(res))

Replace 替换:

Self当不存在

Mag=‘sdg:asy:re’

Print(msg.replace(‘sdg:gay”))

Print(msg.replace(‘sdg’:’asy’))

Isdigit  是否是数字

上节课复习

变量的三个特征:id type  value

"""上节课复习知识
变量的三个特征
id  type  value

8,数据类型 int和float
整型  年龄  号码
浮点型  身高体重
字符串  描述性
列表 存多个值
字典: 存多个值 比列表每一个都有相对应的值
9,与用户交互
input 在python3中只有一个
注意.input会把用户输入的值全部存储为字符串类型
在python2中
 input raw_input

10,文件头,!/usr/bin/env

11运算符:数字,逻辑,(从左往右第一个为真就为真 如果第一个为假 比较下一个 and就为假)比较,(!=)赋值 身份运算(==比较的是指
,is比较的是id)
流程控制 while if
while+break 跳出本层循环
while+continue跳过本次循环

补充知识
1,赋值方式
链式赋值  x=y  y=x x=y=b=c=1
交互式  n=1 m=2  n,m=m,n

2 变量的解压
s=【1,2,3,4】
s1,s2,s3,s4=s
print(s1)(s2)
s1 ,s2,s3 _*=s
print(s1)(s2)(s3)

3,input 与 raw input
在python当中
把用户输入的内容全部存为字符串类型
在python2当中
input 必须输入明确的类型,输入什么存什么
raw——input  用户输入内容全部存为字符串

4,10进制 转换2 print(bin())
10进制转8 print(oct())
10进制16 print(hex())

5,将‘123’转换int   int(‘123’)
将10转换str  str(10)

6 取倒数第一个字符
name="egon你好"
print(name[-1: ])
"""
msg="alex:123:admin"
input(msg.split(':')[0])
7,统计字符个数
print(len(name))

8,成员运算in 和 not in
for
item in students:
    if sb in item['name']:
        print(iten['name'],itenm['age'])

9,
用两种方式
nag=‘egon你好’
for item in msg:
    print(item)

c=0
while c<=len(msg):
    print(msg[c])
    c+=1

10
name='egon'
password='123'
tag=True
while
tag:
    inp_name=input('用户名:')
    inp_passwor=input('密码')
    if inp_name==name and inp_passwor==password:
    print('登录成功')
    while tag:
    cmd=input('>>:')
    if cmd=='quit'
       
tag=False
        continue
        break
       
print("%s" %cmd)
    else:
        print('失败')

原文地址:https://www.cnblogs.com/maojiang/p/8613438.html