格式化输出的三种方式

一、占位符

#占位符

name = 'nick'

age = 19

print('my name is %s my age is %s' % (name, age))

age = 19

print('my name is %d' % age)

my name is nick my age is 19

my age is 19

二、format格式化

name = 'nick'

age = 19

print("Hello, {}.You are {}.".format(age, name))

print("Hello, {1}. You are {0}-{0}".format(age, name))

print("Hello, {name}. You are {age}-{age}.".format(age = age, name = name))

Hello,nick. You are 19.

Hello,nick. You are 19-19

Hello,nick. You are 19-19.

三、f-String格式化

name = "nick"

age = 19

print(f'Hello, {name}. You are {age}.')

pint(F'Hello, {name}. You are {age}.')

print(f'{age*2}')

salary = 6.66666

print(f'{salary:.2f}')

Hello, nick. You are 19.

Hello, nick. You are 19.

38

6.67

横向输出是在print中加一个参数end=' '

流程控制之if判断

一、语法

# if

cls = 'human'

gender = 'female'

age = 19

if cls == 'human' and gender == 'female' and age > 16 and age a < 22:

print('开始表白')

print('end....')

# if...else

cls = 'human'

gender = 'female'

age = 38

if cla == 'human' and gender == 'female' and age > 16 and age < 22:

print('开始表白')

else:

print('阿姨好')

# if...elif...else

cls = 'human'

gender = 'female'

age = 28

if cls == 'human' and gender == 'female' and age > 16 and age < 22:

print('开始表白')

elif cls == 'human' and gender == 'female' and age > 22 and age < 30:

print('考虑下')

else:

print('阿姨好')

开始表白

end…..

阿姨好

考虑下

二、if的嵌套

cls = 'human'

gender = 'female'

age = 19

is_successful = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:

print('开始表白')

if is_successful:

print('那我们一起走吧。。。')

else:

print('我逗你玩呢')

else:

print('阿姨好')

开始表白

我逗你玩呢

练习

练习一:成绩评判

如果 成绩>=90,打印"优秀"

如果 成绩>=80 并且 成绩<90,打印"良好"

如果 成绩>=70 并且 成绩<80,打印"普通"

其他情况:打印"差"

# 成绩评判

score = input("your score: ")

score = int(score)

if score >= 90:

print('优秀')

elif score >= 80:

print('良好')

elif score >= 70:

print('普通')

else:

print('差')

your score: 80

良好

练习二:模拟登录注册

#模拟登录注册

user_from_db = 'nick'

pwd_from_db = 123

user_from_inp = input('username:')

pwd_from_inp = input('password: ')

if user_from_inp == user_from_db and pwd_from_inp == pwd_from_db:

print('login successful')

else:

print('username or password error')

username: nick

password: 123

username or password error

python中if的输入格式_Python基础之输出格式和If判断相关推荐

  1. python中for循环语句格式_Python基础-10循环语句

    Python Python开发 Python语言 Python基础-10循环语句 10.循环语句 对于循环语句,Python仅提供了while和for两个,并未像其他编程语言一样提供for...eac ...

  2. python中如何键盘输入列表_python怎么输入一个列表

    在python中可以使用方括号"[ ]"来表示列表,其输入列表的语法如"bicycles = ['trek'.'cannondale','redline']", ...

  3. python中使用函数的优点_Python基础之函数基本用法与进阶详解

    本文实例讲述了Python基础之函数基本用法与进阶.分享给大家供大家参考,具体如下: 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数 ...

  4. python中的点的作用_Python基础学习中关键点的作用(三),python,重点,之,函数,3

    函数学习之匿名函数 定义: 匿名函数是指在python中使用lambda所创建函数,称之为匿名函数. 特点: 创建函数不再使用def创建,而是使用lambda关键字创建一个形式主义的函数. 匿名函数的 ...

  5. python中if语句的实例_Python基础入门-IF语句

    今天给大家分享一下Python中的IF语句的使用场景以及注意事项.主要内容如下: 1.python中的真假 2.Python操作符 3.if语句实例和嵌套实例 4.if语句中的if嵌套实例 5.and ...

  6. python中if else语句格式_Python if else条件语句

    我们前面看到的代码都是按顺序执行的,也就是先执行第一条语句,再执行第二条和第三条语句--一直到最后一条语句,这就是所谓的顺序结构. 然而,在许多情况下,序列结构的代码远远不够.比如一个程序只限成人使用 ...

  7. python中abc属于字符串吗_Python基础学习:字符串

    Python 版本: 3.6.2 操作系统: Windows 作者: SmallWZQ 在 Python 中,字符串也是一种数据类型.相比其它数据类型,字符串算是比较复杂的.为 何呢?因为字符串不仅包 ...

  8. python中函数的定义实例_Python基础之函数的定义与使用实例

    此文实例介绍了Python基础之函数的定义与使用.推荐给大伙学习一下,内容如下: Python 定义函数使用 def 关键字,一般格式如下: def 函数名(参数列表): 函数体 让我们使用函数来输出 ...

  9. python中字符串函数的作用_python 基础学习笔记(2)---字符串功能函数

    **上一篇写到了,基本的数据类型,今天重点来讲一下字符串的功能函数** 回顾一下上篇的内容: 一.int 整型,在python 3 中与long型合并 可以达到 -922337203685477580 ...

最新文章

  1. python输入转化为数字_Python中如何将输入数据转换为数字?
  2. 星际2的一些技术特性
  3. 学习——JavaWeb05:JSP入门
  4. Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)
  5. java 监听器 分类_java过滤器和监听器详解 分类: 学习专区
  6. 信息学奥赛一本通(1144:单词翻转)
  7. IIS不能下载ini文件
  8. 从淘宝来看后端架构发展
  9. python 在线字典_python3
  10. it is not your reason to quit.
  11. 上周热点回顾(5.8-5.14)
  12. virt viewer Usbredir USB重定向
  13. Protel DXP 2004 SP3_SP4 注册机
  14. 联想计算机电源维修,自己动手修理联想X1 YOGA电源故障
  15. 天猫达尔文商品管理体系通俗简介
  16. Druid连接池原理
  17. 【已解决】 “discovered_interpreter_python“: “/usr/bin/python“
  18. 【微信小程序】扫描二维码/条形码,并获取信息
  19. 常用计算机 启动bios,常见电脑进入bios的方法
  20. ionic 构建 Cannot load gulp tasks: Error: Error in module: .\gulpfile.js:

热门文章

  1. 【clickhouse】clickhouse 表引擎 之 AggregatingMergeTree
  2. 【Kafka】Kafka ERROR [ConsumerFetcherThread-console-consumer], Error for partition [xx,5] to broker 10
  3. Google Guice 一个轻量级的依赖注入框架
  4. MyBatis插入数据返回插入对象的主键
  5. linux扩容根目录空间_记一次生产线上Linux系统根目录爆满问题解决办法
  6. 用了这么久,你真的真的明白 HttpClient 的实现原理了吗?
  7. 如何计算虚拟化vcpu_首次公开:腾讯云虚拟化技术原理及可用性提升实践
  8. J2EE五层架构概念[转+整理]
  9. 基于 YOLOV3 和 OpenCV的目标检测
  10. centos apache 腾讯云ssl证书配置