字符串的运算
——字符串可以和数字相乘,表示连续拼接
print('_'*20,'|'*100)
——用布尔型来判断是否为数值是否为空
False:0 ‘’ “” None () [] {}
Ture:非0的字符串 非空的列表 字典 元组
空格也表示字符
算数运算符
/是求商 //求整数商 %是求余数 **是求平方
复合运算
a+=b 同比与 a=a+b
比较运算符
== != < > <= >=
例如 print(2>=2)
输出结果将会用布尔型输出 (Ture 或者 False)
逻辑运算符
and :与的关系 一假则假
or是或的关系 一真则真
not 是非 取反的意思
b=1>2
b1=1<2
b2=2>1
print(b and b1 and b2)#一假则假
print(b or b2)#一真则真
a=0
b=3<1
c='aa'
print(a or b or c) #取第一个真的。如果全是假的输出False
优先级: not and or
if语句
age_str =input('how old are you' )
age=int(age_str)
if age<=18:
print('你太年轻')
print('结束了')
注意:if 后面的冒号
嵌套
格式``
`if 判断语句1:
代码
if判断语句2:
代码2
ticket ='有'
daozi_length=70
if ticket=='有':
print('嗯 能过')
if daozi_length<=60:
print('还是被击毙了')
else:
print('顺利通过')
print("欢迎乘坐死亡列车")
## 生成随机数
**import random
num=random.randint(1,222)
print(num)**
游戏案例:
player_str = input(‘请输入你出的东西 石头/1 剪刀/2 布/3’)
player = int(player_str) # 类型转换
import random
computer = random.randint(1, 1)
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print(‘恭喜赢得比赛’)
elif player == computer:
print(‘平局’)
else:
print(‘很遗憾您输了’)
作业 :求数组的各元素数量
str = input('请随便写 \n')
zm = 0
sz = 0
xhx = 0
qt = 0
for char in str:
if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
zm += 1
elif '0' <= char <= '9':
sz += 1
elif " ":
xhx += 1
else:
qt += 1
print('字母有{}个'.format(zm), '数字有{}个'.format(sz), '下划线有{}'.format(xhx),
'其他有{}个'.format(qt))
运行结果如下图所示