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

最新文章

  1. HTML 事件(一) 事件的介绍
  2. NFS服务器配置文档
  3. spring-Formatter(格式化器)-validator(验证器)-错误信息定制
  4. svn工具安装下载Tomcat源码以及导入eclipse
  5. Ubuntu Linux 12.04 LTS amd64系统本地root提权
  6. 【USACO】namenum
  7. HDU 1247 Hat’s Words (字符串匹配,暴力)
  8. HTML5小时钟
  9. 关于Failed to convert property value of type [org.quartz.impl.StdScheduler] to required type [org.springframework.scheduling.quartz.SchedulerFactoryBean
  10. 为自己的Android应用添加广告
  11. 简单RTP发送类c++实现
  12. BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
  13. MySQL列:innodb的源代码的分析的基础数据结构
  14. 动手Jquery插件
  15. win10*64+vs2015+opencv3.0工程模板配置
  16. mysql filesort 的解决方案
  17. cpp函数回调
  18. Autel Maxisys MS908CV Description
  19. Win7 VS2015环境使用SDL2-2.0.4
  20. Android 计算器

热门文章

  1. 路飞学城Python-Day78
  2. 22 链表中倒数第k个节点(第3章 高质量的代码-代码的鲁棒性)
  3. 终极对决!Dubbo 和 Spring Cloud 微服务架构到底孰优孰劣
  4. python第四周:装饰器、迭代器、内置方法、数据序列化
  5. php strtotime 同样的函数为何在不同的地方输出的结果不同?
  6. POJ 2190
  7. HDU 4350
  8. hive join 优化 --小表join大表
  9. 杭电 1016 Prime Ring Problem
  10. firewall 允许app访问网络