# 一、流程控制之for循环
```bash
names = ['egon', "lxx_dsb", "xc", "zhoujielun"]
方案1:
i=0
while i < len(names):
print(names[i])
i+=1
方案2:
for i in names:
print(i)
结果:
egon
lxx_dsb
xc
zhoujielun
```
# 二、for:迭代循环
## 1.遍历值
```bash
for x in d:
print(x,d[x])
for x in names:
print(x)
info = [["name", "egon"], ["age", 18], ["gender", "male"]]
for x,y in info: # x,y=["name", "egon"]
print(x,y)
```
## 2.range()
```bash
i=0
while i < 3:
print('步骤1')
print('步骤2')
print('步骤3')
i+=1
for i in range(3):
print('步骤1')
print('步骤2')
print('步骤3')
for i in range(10,0,-1):
print(i)
l=[111,222,333,444,555]
i=len(l) - 1
while i > -1:
print(l[i])
i-=1
或者
for i in range(len(l)-1,-1,-1):
print(l[i])
结果倒叙打印
```
## 3.for+enumerate(列举)
```bash
l=[111,222,333,444,555]
for i,y in enumerate(l):
print("index: %s value: %s" % (i,y))
```
## 4.for+break(干掉当前循环)
```bash
for i in range(5):
if i == 3:
break
print(i)
```
## 5.for+continue(略过本次,返回上面继续本次的下一次循环)
```bash
for i in range(5):
if i == 2:
continue
print(i)
```
## 6.for+else
```bash
for i in range(5):
if i == 3:
break
print(i)
else:
print('++++>')
```
## 7.嵌套多层的for循环
```bash
for i in range(3):
print("-----loop1-----> %s" % i)
for j in range(2):
print("loop2--->%s" % j)
结果:
-----loop1-----> 0
loop2--->0
loop2--->1
-----loop1-----> 1
loop2--->0
loop2--->1
-----loop1-----> 2
loop2--->0
loop2--->1
```
# 三、可变与不可变类型
```bash
可变类型:值改变的情况下,内存地址/id不变,证明改变的就是原值,即可变类型=>不可hash类型
不可变类型:值改变的情况下,内存地址也变,证明时产生了新的值,即原值不可变=》可hash类型
数字类型和字符串类型属于不可变类型
列表和字典属于可变类型
```
# 四、进制转换
```bash
# 进制
# 十进制:0-9
# 二进制:0-1
# 八进制:0-7
# 十六进制:0-9 a b c d e f
# 十进制<-二进制
# 1011
# 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0
# 8 + 0 + 2 + 1 =》11
# 十进制<-八进制
# 1011
# 1*8^3 + 0*8^2 + 1*8^1 + 1*8^0
# 十进制<-十六进制
# 1011
# 1*16^3 + 0*16^2 + 1*16^1 + 1*16^0
# 十进制-》二进制
# 11
```
# 五、数字类型的定义及使用方法
## 1.整型int的基本使用
```bash
1、用途:年龄、等级、号码
2、定义方式
age=18 # age=int(18)
数据类型转换
x=int(" 103 ") # 把纯数字组成的字符串转换成int类型
print(x,type(x))
3、常用操作+内置的方法
# + - * % > >=
总结:存一个值,不可变类型
```
## 2.整型float的基本使用
```bash
1、用途:薪资、身高、体重
2、定义方式
salary=3.3 # salary=float(3.3)
数据类型转换
x=float(" 3.3 ")
print(x,type(x))
3、常用操作+内置的方法
# + - * % > >=
总结:存一个值,不可变类型
```
## 3.复数(了解)
```bash
x=2+3j
print(x,type(x))
print(x.real)
print(x.imag)
```
# 六、字符串类型的定义及使用方法
```bash
1、用途:存各种值
2、定义方式
msg="abc" # msg=str("abc")
数据类型转换
res=str(1111) # 可以把所有类型转换成字符串类型
print(res,type(res))
3、常用操作+内置的方法
优先掌握的操作:
#1、按索引取值(正向取+反向取) :只能取
msg="hello world"
print(msg[0])
msg[0]="H" # 不能改
print(msg[-1])
#2、切片(顾头不顾尾,步长)
msg="hello world"
print(msg[3:5])
print(msg[3:8])
print(msg[3:8:2]) # 3 5 7
print(msg[8:3:-1]) # 8 7 6 5 4
row o
print(msg[:3])
print(msg[::])
print(msg[:])
print(msg[::2])
print(msg[10::-1])
print(msg[::-1])
#3、长度len:统计的是字符个数
msg="h e你"
print(len(msg)) #结果为4
#4、成员运算in和not in
msg="hello world"
print("he" in msg)
print("h" in msg)
print(not 'egon' in msg)
print('egon' not in msg) # 推荐
#5、移除空白strip(只可移除左右两边)
name=" egon "
print(name.strip())
print(name)
name="** *eg*on***"
print(name.strip("*"))
x="*(-)=+abc=-)*/"
print(x.strip("*()-=+/")) #可指定任意字符移除,移除顺序从外到里
#6、切分split
info="root:123:0:0"
res=info.split(":",2)
print(res[0])
print(res)
结果:
root
['root', '123', '0:0']
总结:指定:为分隔符,数字为指定分隔几次
#7、循环
msg="hello"
for i in msg:
print(i)
字符串总结:
存一个值,有序,不可变类型
```