输入函数input( )与常用运算符

#  输入函数input()

password = int(input('请输入你的密码:\n'))

 # input()为输入函数,获得的输入值统一为string型,可对其做强制类型转换
print('Password is ', password)
print('type of password is ', type(password))
"""
运算结果如下:
请输入你的密码:
1234
Password is  1234
type of password is  <class 'int'>
"""


#  常用运算符

a = 6
b = 5
print(1+1)
print(2*4)
print(1/2)
print(11/2)
print('a/b=', a/b)  # python中int/int可以得到float
print(2.1**3)  # 幂运算
print(11//2)  # //为整除运算符
print(11 % 3)  # 取余运算
print()
"""
运算结果如下:
2
8
0.5
5.5
a/b= 1.2
9.261000000000001
5
2
"""

# 取整运算是向下取整
print(' 9// 4= ', 9//4)
print('-9//-4= ', -9//-4)
print('-9// 4=', -9//4)
print(' 9//-4=', 9//-4)
print()
"""
运算结果如下:
9// 4=  2
-9//-4=  2
-9// 4= -3
9//-4= -3
"""

# 取余数运算遵循公式:余数=被除数-除数*商
print(' 9%-4= 9-(-4)*(-3)=', 9 % -4)
print('-9% 4=-9-( 4)*(-3)= ', -9 % 4)
print()
"""
运算结果如下:
9%-4= 9-(-4)*(-3)= -3
-9% 4=-9-( 4)*(-3)=  3
"""

# 赋值运算
# 赋值中,只要值一样,就指向同一个内存单元
a = b = c = 1  # 链式赋值
d = 1
e = 1
f = 1
print('value of a is ', a, '; id of a is', id(a))
print('value of b is ', a, '; id of b is', id(b))
print('value of c is ', a, '; id of c is', id(c))
print('value of d is ', a, '; id of d is', id(d))
print('value of e is ', a, '; id of e is', id(e))
print('value of f is ', a, '; id of f is', id(f))
print()
"""
运算结果如下:
value of a is  1 ; id of a is 1943661078832
value of b is  1 ; id of b is 1943661078832
value of c is  1 ; id of c is 1943661078832
value of d is  1 ; id of d is 1943661078832
value of e is  1 ; id of e is 1943661078832
value of f is  1 ; id of f is 1943661078832
"""

g, h, i = 2, 3, 4  # 解包赋值
print('value of g is ', g, '; id of g is', id(g))
print('value of h is ', h, '; id of h is', id(h))
print('value of i is ', i, '; id of i is', id(i))
print()
"""
运算结果如下:
value of g is  2 ; id of g is 1943661078864
value of h is  3 ; id of h is 1943661078896
value of i is  4 ; id of i is 1943661078928
"""

g, h = h, g  # 通过解包赋值实现交换变量
print('value of g is ', g, '; id of g is', id(g))
print('value of h is ', h, '; id of h is', id(h))
print()
"""
运算结果如下:
value of g is  3 ; id of g is 1943661078896
value of h is  2 ; id of h is 1943661078864
"""

print('\n\n比较运算符:')
print('g>h吗?', g > h)  # 布尔运算符的返回值为bool类项
print('a==b :', a == b)  # ==是用于判断两个变量的值是否一样
print('a is b :', a is b)  # is 是用于判断两个变量的内存地址是否一样
print()
"""
运算结果如下:
比较运算符:
g>h吗? True
a==b : True
a is b : True
"""

list1 = [0, 1, 2]
list2 = [0, 1, 2]
print('list1==list2:', list1 == list2)
print('list1 is list2:', list1 is list2)
print('list1 is not list2:', list1 is not list2)
"""
对于单个变量,其值相同,则地址相同
对于链表,其值相同,但是其地址不相同
"""
print()
"""
运算结果如下:
list1==list2: True
list1 is list2: False
list1 is not list2: True
"""

print('\n\nbool运算符:')
print(a == 1 and b == 1)
print(a == 1 and b != 1)
print(a == 1 or b != 1)
print(a == 1 and (not(b != 1)))
"""
and:与运算
or :或运算
not:非运算
"""
print()
"""
运算结果如下:
bool运算符:
True
False
True
True
"""

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值