交互
现阶段暂时用input进行交互
name=input('please input your name:')
print(name)
input()的作用是接收值,而且不管输入的是什么类型的值,最后被赋值的对象都是字符串类型。
格式化输出
- 占位符
name='zhaojiahao'
hieght=23
weight=155
print('My name is %s,my height is %s' % (name,height))
- format格式化
print('My name is {},my height is {},my weight is {}'.format(name,height,weight))
- f-string格式化
print(f'My name is {name},my height is {height},my weight is {weight}')
基本运算符
- 算数运算符
+,-,*,/,%(取余), **(幂), //(取整除)
- 比较运算符
>
<
>=
<=
==
!=
<>
- 赋值运算符
=,+=,-=,*=,/=,%=,**=,//=
- 逻辑运算符
and:与,如果x为False,x and y返回False,否则返回y的计算值
or:或,如果x是非零,它返回x的值,否则返回y的计算值
not:非,如果x为True,则返回False,如果x为False,则返回True
- 身份运算符
is:判断两个标识符是不是引用自同一个对象
is not:判断两个标识符是不是引用自不同对象
python赋值
- 链式赋值
a=b=c=d=10
- 交叉赋值
x = 100
y = 200
temp = x
x = y
y = temp
print(f'x:{x}')
print(f'y:{y}')
或者:
x, y = y, x
print(f'x:{x}')
print(f'y:{y}')
解压缩
name_list = ['nick', 'egon', 'jason', ]
x, y, z = name_list
print(f'x:{x}, y:{y}, z:{z}')
当有些解压缩的值不需要时:
name_list = ['nick', 'egon', 'jason', 'tank']
x, y, z, a = name_list
x, _, z, _ = name_list # _相当于告诉计算机不要了,不能以_开头和结尾
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) #字典输出key值
原文地址:https://www.cnblogs.com/zhoajiahao/p/10902234.html
时间: 2024-10-09 08:22:35