目录
input
password=input('请输入密码')
print(type(password)) #str
print(type(int(password))) # int
# 2.1
# 遇到input,等待用户输入
# 2.2
# 接收input存变量
# 2.3
# input接收到的数据类型都是字符串
类型转换
# float
num=1
str1 ='10'
print(type(float(num))) #<class 'float'>
print(float(str1)) # 10.0 也可将字符串转换成浮点
# str
print(str(num)) #1
#tuple 列表转换成元组
list1=[10,20]
print(type(tuple(list1))) #<class 'tuple'>
print(tuple(list1)) # (10, 20)
t1=(10,20,30)
print(list(t1)) #[10, 20, 30]
#eval() 计算在字符串中的有效Python表达式,并返回一个对象
# 就是子识别字符串中的内容 进行类型转换
str2 = '1'
str3 = '1.1'
str4 = '(1000, 2000, 3000)'
str5 = '[1000, 2000, 3000]'
print(type(eval(str2))) #<class 'int'>
print(type(eval(str3)))#<class 'float'>
print(type(eval(str4)))#<class 'tuple'>
print(type(eval(str5)))#<class 'list'>
python console 仅仅适合简单的交互使用 运行的时候只会存储数据到内存中 不会存储硬盘
运算符
- 算数运算符
- 赋值运算符
多变量赋值
num1,float1,str1=10,0.5,'haha'
print(num1,float1,str1)
#10 0.5 haha
a=b=2
print(a,b)
#2 2
- 复合赋值运算符
- 比较运算符
- 逻辑运算符
普通方式与特殊方式使用对比
a = 1
b = 2
c = 3
print((a < b) and (b < c)) # True
a = 0
b = 1
c = 2
# and运算符,只要有⼀个值为0,则结果为0,否则结果为最后⼀个⾮0数字
print(a and b) #0
print(b and a) #0
print(a and c) #0
print(c and a) #0
print(b and c) #2
print(c and b) #1
# or运算符,只有所有值为0结果才为0,否则结果为第⼀个⾮0数字
print(a or b) #1
print(a or c) #2
print(b or c) #1
总结 只要不是数字对比 True False 数字之间对比 就只会返回 0 或者其他数字
结合if考虑 and or 0就是False 其他就是True 就容易记住了 只需记住 数字间使用逻辑运算 也是返回数字 不是bool
if的使用
1.简单的网吧上网案例
age=int(input('请输入您的年龄:'))
if age>=18:
print(f'您的年龄是{age},已经成年可以上网')
else:
print('您的年龄不达标')
print('系统关闭')
if xx: elif xx: 而不是elseif xx:
age=int(input('请输入您的年龄:'))
if age<18:
print(f'您的年龄是{age},未成年')
# elif (age>=18) and (age<=60):
# print('壮年')
elif 18<=age<=60:
print('壮年')
elif age>60:
print('老年')
print('系统关闭')
三元运算符
a=1
b=2
c=b if a>b else a
print(c)
之前 c=a>b?a:b 而python是真正通过if else去判断
如果a>b 则返回b 否则返回a
while
1加到100的值
i=1
result=0
while i<=100:
result+=i
i+=1
print(result)
break 条件不成立就跳出循环
i=1
result=0
while i<=100:
if i%2==0:
result+=i
break
i+=1
print(result) #2
continue 若是成立 不向下执行继续去循环
i=1
result=0
while i<=100:
if i%2==0:
result+=i
print(result) #结果无线循环 一直加 因为continue 一旦成立 循环体内他下面的其他程序就不走了 继续向上走 此时i 不会再加
print(i)
continue
i+=1
print(result)
嵌套
j=0
while j<3:
i=0
while i<3:
print('哈哈')
i+=1
print('hehe')
j+=1
'''
哈哈
哈哈
哈哈
hehe
哈哈
哈哈
哈哈
hehe
哈哈
哈哈
哈哈
hehe
'''
j=0
i=0
while j<3:
while i<3:
print('哈哈')
i+=1
print('hehe')
j+=1
'''
哈哈
哈哈
哈哈
hehe
hehe
hehe
'''
打印 *
i=0
while i<5:
j=0
while j<5:
print('*',end='')
j+=1
print()
i+=1
'''
*****
*****
*****
*****
*****
'''
i=0
while i<5:
j=0
while j<=i:
print('*',end='')
j+=1
print()
i+=1
'''
*
**
***
****
*****
'''
i=1
while i<=9:
j=1
while j<=i:
print(f'{j}*{i}={j*i}',end='\t')
j+=1
print()
i+=1
'''
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
'''
循环加else
str1 = 'itheima'
for i in str1:
if i == 'e':
print('遇到e不打印')
break
print(i)
else:
print('循环正常结束之后执⾏的代码')
'''
i
t
h
遇到e不打印
'''
换成continue就会打印结束之后的 毕竟for else 是一体的