Python变量及其运算

Python中最常见变量有数字型 字符串 和布尔值

运算又有成员运算数值运算逻辑运算

下面以变量为线索展开:

  • 数字的运算

【1】返回值为数字:
常见的加减乘除 +, - ,* , /
(优先级:先乘除后加减)
另外:

count+=1   等价于    count=count+1
count*=1   等价于    count=count*1
count/=1   等价于    count=count+1
count-=1   等价于    count=count-1

取余和取整 % , // 这两种运算 也可用上述简写

平方 **

【2】返回值为布尔:

== ,> ,< , >=, <= , !=

  • 字符串的运算

【1】 返回值为布尔
包含于和不包含于 in , not in
【2】返回值为字符串
常见加和乘 + , *

input函数返回的值为字符串

n=input("please input something")

print(n)

print(n*3)

print(n+n)

if "n" in n:
    print("里面包含n")
if "m" not in n:
    print("里面不包含m")
  • 布尔值的运算

    只返回布尔值

    or 或者

    and 并且

(无优先级从左到右运算,逻辑上需要优先计算时只能通过加括号实现)

n=input("please input some thing")
m=input("please input other thing")

if n=="mint" and m=="mint"or 2==2+1 or 3>=2 and 1+1==3:
    print("yes")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值