为什么交互?
计算机取代人类,解放劳动力
如何交互
print('-'*100)
input('请输入你的姓名:')
print(""100)
python2和python3的交互(熟悉)
name = raw_input('请输入你的姓名:')
pwd = raw_input('请输入你的密码:')
print(type(name)) # 'str'
print(type(pwd)) # 'str'
占位符
%s(针对所有数据类型)、%d(仅仅针对数字类型)
name = 'nick'
age = 19
print('my name is %s my age is %s' % (name, age))
my name is nick my age is 19
age = 19
print('my age is %d' % age)
my age is 19
fORMat格式化
name = 'nick'
age = 19
print("Hello, {}. You are {}.".format(name, age))
》 Hello, nick. You are 19.
name = 'nick'
age = 19
print("Hello, {1}. You are {0}-{0}.".format(age, name)
》Hello, nick. You are 19-19.
f-String格式化
- name = "john"
age = 19
print(f"Hello, {name}. You are {age}.") - Hello, john. You are 19.
大写的F也适用
- salary = 6.6666
print(f'{salary:.2f}') - 6.67
算术运算符
如:加减乘除
比较运算符
-
pwd = '123'
print(pwd != '123')
print(pwd =='123')
false
true
赋值运算符
此处直接贴图了,以下假设变量a为10,变量b为20。
age = 19
age = age + 1
print(age)
20
age = 19
age *= 10
print(age)
190
逻辑运算符
此处直接贴图了,以下假设变量a为10,变量b为20。
# 从左到右的方式找到逻辑运算符,找到逻辑运算符的左边,左边成立,再去找到逻辑运算符的右边
print(3 > 3 and 1 > 2 or 2 > 1) # False
True
身份运算符
身份运算符用于比较两个对象的存储单元
is和==的区别:is用于判断两个对象是否为同一个(是否在同一个内存空间),==用于判断引用变量得知是否相等。
x = 257
y = x
z = 257
print(f'x is y:{x is y}')
print(f'x == y:{x == y}')
print(f'x is z:{x is z}')
print(f'x == z:{x == z}')
x is y:True
x == y:True
x is z:False
x == z:True
Python运算符优先级
python运算符的优先级是由括号决定的
链式赋值
a = 10
b = 10
c = 10
d = 10
print(f'a:{a}, b:{b}, c:{c}, d:{d}')
a:10, b:10, c:10, d:10
a = b = c = d = 10
print(f'a:{a}, b:{b}, c:{c}, d:{d}')
a:10, b:10, c:10, d:10
交叉赋值
x = 100
y = 200
temp = x
x = y
y = temp
print(f'x:{x}')
print(f'y:{y}')
x:200
y:100
x,y = y,x
print(f'x:{x}')
print(f'y:{y}')
x:100
y:200
解压缩
name_list = ['nick', 'eGon', 'jason']
x = name_list[0]
y = name_list[1]
z = name_list[2]
print(f'x:{x}, y:{y}, z:{z}')
x:nick, y:egon, z:jason
解压法
name_list = ['john', 'egon', 'jason',]
x,y,z = name_list
print(f'x:{x},y:{y},z:{z}')
x:john,y:egon,z:jason
name_list = ['nick', 'egon', 'jason', 'tank', 'kevin', 'jerry']
x, y, , , _, z = name_list
x, y, *_, z = name_list
字典也可以
info = {'name': 'nick', 'age': 18}
x, y = info
print(x, y)
name age
0