python 字符串格式化、运算符

字符串格式化

利用%格式化

  • %d:整数
  • %s:字符串
s = "I am %d years old"
print(s)
print(s%18)

s = "I love %s"
print(s%"yocy")

#如果出现多个占位符,则相应内容需要用括号括起来,不然会报错,参数不够
s = "I am %s,I am %d years old"
print(s%yocy,21)#这就是错的
print(s%(yocy,21)

format函数格式化

print('{} a word she can get what she {} for.'.format('With','came')) #按顺序填充
print('{preposition} a word she can get what she {verb} for.'.format(preposition = 'With',verb = 'came')) #用=定义
print('{0} a word she can get what she {1} for.'.format('With','came')) #索引定位,可重复使用

None

  • 表示什么都没有
  • 若函数无返回值,可返回None
  • 用来占位置、接触变量绑定

算数运算符

  • %:取余
  • //:取商,也叫地板除
  • 所有数学运算符都有缩写形式:-=,*=,/=,//=,**=

逻辑运算符

  • and,or,not
  • 运算规则:and看做乘法,or看做加法;true看做1,false看做0;则将逻辑运算换做整数数学运算;最后的结果,0为false、1为true
  • 短路规则:按照运算顺序计算,一旦能够确定整个式子未来的值,则不再进行计算,直接返回
a = True
b = False
c = True

d = a and b or c
#等价于 d = 1 * 0 + 1
print(d) #为True

d = a or b and a
#等价于 d = 1 + 0 * 1
print(d) #为True

#短路案例
a = True or xxxxxx #结果一定为True
a = False and (xxxxxx) #结果一定为False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值