Day 04 [与用户交互,格式化输出,基本运算符]
2024-08-31 09:56:49
Python 的与用户交互
name=input("请输入姓名:")
height=input('请输入身高:')
weight=input('请输入体重:')
在python3中,input()方法的作用只是接收值,并且无论我们输入的值是数字类型、字符串类型、列表类型,input的接收值都是字符串类型。
python2当中的raw_input()和python3中的input()是一模一样的。
格式化输出
- 占位符 %s(针对所有类型) %d(仅仅针对数字类型)
name='egg'
age=19
print('my name is %s , my age is %s' %(name,age))
- format 格式化
name='egg'
age=19
print('my name is {name},my age is {age}'.format(name=name,age=age))
print('my name is {0},my age is {1}'.format(name,age))
print('my name is {},my age is {}'.format(name,age))
- f -String格式化
相比较占位符的方式,python3.6版本新增了f-String格式化的方式,比较简单易懂,这是目前我用的最多的方式,推荐使用这种方式。
name='egg'
age=18
print(f'my name is {name},my age is {age}')
salary=6.6666
print(f'{salary:.2f}')
基本运算符
1.算术运算符
+ - * / % //
2.比较运算符
< ,>,>=,<=,==
3.赋值运算符
=
4.逻辑运算符
and or not
5.身份运算符
is (比较的是id,id相同的值一定相同,值相同的id不一定相同,除了小整数池的除外)
Python运算符优先级
链式赋值
a=10
b=10
c=10
d=10
print(f'a:{a},b:{b},c:{c},d:{d}')
a=b=c=d=10
交叉赋值
#让x与y的值互换
x=100
y=200
第一种方法:
x,y=y,x
第二种方法:
temp=x
x=y
y=temp
解压缩
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}')
name_list=['nick','egon','jason']
x,y,z=name_list
#打印结果
#nick egon jason
name_list=['nick','egon','jason']
x,_,z=name_list
#打印结果
#nick jason
name_list = ['nick', 'egon', 'jason', 'tank', 'kevin', 'jerry']
x, y, _, _, _, z = name_list
x, y, *_, z = name_list
#打印结果
#nick egon jerry
#nick egon jerry
info = {'name': 'nick', 'age': 18}
x, y = info
print(x, y)
#打印结果
#name age
#字典解压缩的是key
最新文章
- HTML 事件(一) 事件的介绍
- NFS服务器配置文档
- spring-Formatter(格式化器)-validator(验证器)-错误信息定制
- svn工具安装下载Tomcat源码以及导入eclipse
- Ubuntu Linux 12.04 LTS amd64系统本地root提权
- 【USACO】namenum
- HDU 1247 Hat’s Words (字符串匹配,暴力)
- HTML5小时钟
- 关于Failed to convert property value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean
- 为自己的Android应用添加广告
- 简单RTP发送类c++实现
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
- MySQL列:innodb的源代码的分析的基础数据结构
- 动手Jquery插件
- win10*64+vs2015+opencv3.0工程模板配置
- mysql filesort 的解决方案
- cpp函数回调
- Autel Maxisys MS908CV Description
- Win7 VS2015环境使用SDL2-2.0.4
- Android 计算器