用户交互、运算符
用户交互
输入
py3版本输入
def input(*args, **kwargs): # real signature unknown
"""
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
"""
pass
# 接受任意类型的数值参数作为输入项,返回的是用户输入的结果
example:
res = input([1,2,3,4])
print(res)
# 结果
[1, 2, 3, 4]你说啥
你说啥
py2版本输入
raw_input
# py2中raw_input()对于任何类型的输入,都会以字符串的形式返回
>>> a = raw_input("请输入:")
请输入:[1,2,3,4]
>>> print a
[1,2,3,4]
>>> print type(a)
<type 'str'>
input
# py2中的input是以你输入的是什么类型的数据就返回什么类型的数据
>>> a = input("请输入:")
请输入:[1,2,3,4]
>>> print a
[1, 2, 3, 4]
>>> print type(a)
<type 'list'>
输出
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space. 在两个数据之间的参数,默认使用空格
end: string appended after the last value, default a newline. 结束输出的后面有默认换行\n
flush: whether to forcibly flush the stream.
"""
pass
# 接受任何格式的输出,默认输出换行\n
print("hello","world",sep=",",end = "!")
# hello,world!
格式化输出
1、百分号格式化
# %s可以接受任何数据传入,%d只能接受数字传入并且默认取整数部分
print("%s今年%d岁" % ("我", 18))
# 我今年18岁
2、format格式化
print("{}今年{}岁".format("我",18))
print("{0}今年{1}岁".format("我", 18))
print("{name}今年{age}岁".format(name = "我", age = 18))
# 我今年18岁
# 我今年18岁
# 我今年18岁
3、f""格式化
print(f"{'我'}今年{18}岁")
# 我今年18岁
print(f"{'我':=>10}今年{18:=>10}岁")
# =========我今年========18岁
4、模板格式化
from string import Template
name = '我'
age = 18
tem_str = "$name今年$age岁"
res = Template(tem_str).substitute(name=name, age=age)
print(res)
总结
1. 如果字符串由用户输入,基于安全问题考虑,使用模板
2. 如果使用的是py3.6+版本使用f"",效率高
3. 如果是为了兼容py2.x版本用format
4. 除非测试使用,一般不用百分号
运算符
赋值运算符
1、增量赋值
= 赋值
+= 自加
-= 自减
*= 自乘
/= 自除
%= 自取余
//= 自取整
**= 自幂
2、链式赋值
x = y = z = 100
print(x,y,z)
# 100 100 100
3、交叉赋值
x = 100
y = 99
x,y = y,x
print(x,y)
# 99 100
4、解压赋值
字符串、列表、元组、字典、集合都支持解压赋值
普通解压
x,y,z = (100,99,98)
print(x,y,z)
# 100 99 98
特殊解压
x,y,*a = (100,2,4,5)
print(x,y,*a)
100 2 4 5
逻辑运算
not
and
or
1、连续多个or或连续多个and
a = 1 or 0 or 12 or True
print(a)
# 1
a = 1 and 0 and 12 and True
print(a)
# 0
2、短路运算(优先级:()> not > and > or)
a = 1 and (not 3) or False
print(a)
# False
# 根据运算的优先级,到哪一步结束就返回当前步骤的值
Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
and or not | 逻辑运算符 |
以下实例演示了Python所有运算符优先级的操作:
#!/usr/bin/python3
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print ("(a + b) * c / d 运算结果为:", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("((a + b) * c) / d 运算结果为:", e)
e = (a + b) * (c / d); # (30) * (15/5)
print ("(a + b) * (c / d) 运算结果为:", e)
e = a + (b * c) / d; # 20 + (150/5)
print ("a + (b * c) / d 运算结果为:", e)
以上实例输出结果:
(a + b) * c / d 运算结果为: 90.0
((a + b) * c) / d 运算结果为: 90.0
(a + b) * (c / d) 运算结果为: 90.0
a + (b * c) / d 运算结果为: 50.0