格式化输出的三种方式

一、占位符

#占位符
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判断的更多相关文章

  1. python基础(二)条件判断、循环、格式化输出

    继续上一篇,今天主要总结一下条件判断.循环.格式化输出 一.条件判断 python中条件判断使用if else来判断,多分支的话使用if elif ... else,也就是如果怎么怎么样就怎么怎么样, ...

  2. python基础知识 变量 数据类型 if判断

    cpu 内存 硬盘 操作系统 cpu:计算机的运算和计算中心,相当于人类的大脑 飞机 内存:暂时存储一些数据,临时加载数据和应用程序 4G 8G 16G 32G 速度快,高铁 断电即消失 造价高 硬盘 ...

  3. Python基础1:if条件判断 break/continue语句

    计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. Python中,if语句被用来进行判断,它的语法结构是: 1 if 判断条件: 2 执行语句…… 3 var = input(" ...

  4. python基础教程 变量/输入输出/if判断

    python的运用越来越多.大数据经常被人谈及,数据从何而来?通过各个平台.app.网站数据的收集,分析,过滤,生成报告,这些都可以用python来处理,并且有很多成熟的库可以直接用了.那还不赶紧深入 ...

  5. Python基础(三):简化除法判断、分析apache访问日志、扫描存活主机、利用多线程实现ssh并发访问

    一.简化除法判断 目标: 编写mydiv.py脚本,主要要求如下: 提示用户输入一个数字作为除数 如果用户按下Ctrl+C或Ctrl+D则退出程序 如果用户输入非数字字符,提示用户应该输入数字 如果用 ...

  6. Python基础之流程控制if判断

    目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...

  7. Python基础——条件判断

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 到目前为止,Python基础系列的文章中的程序都是一条一条语句顺序执行的.在本章中,我会重点介绍让程序选择是否执行语 ...

  8. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  9. Python基础(条件判断,循环,占位符等)

    Python 自动化 系统开发用的语言和自动化脚本可以不同 学习peython可用于: 网路爬虫,数据分,web开发,人工智能,自动化运维,自动化测试,嵌入式,黑客 第三方库比较全 脚本语言:功能单一 ...

随机推荐

  1. 区间DP(超详细!!!)

    一.问题 给定长为n的序列a[i],每次可以将连续一段回文序列消去,消去后左右两边会接到一起,求最少消几次能消完整个序列,n≤500. f[i][j]表示消去区间[i,j]需要的最少次数. 则; 若a ...

  2. oracle--JOB任务

    1.创建一张测试表 -- Create table create table A8 ( a1 VARCHAR2(500) ) tablespace TT1 pctfree 10 initrans 1 ...

  3. C# HTTP系列7 HttpWebRequest.Method属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebRequest.Method属性,获取或设置请求的方法.用于联系 Internet 资源的请求方法. 默认值为 GET. Syst ...

  4. .NET Core:Token认证

    现在是WebAPI的时代,你所需要面对的不止是浏览器了,通常会使用Web, WebApp, NativeApp等多种呈现方式.其中诸如Ember,Angular,Backbone之类的前端框架类库正随 ...

  5. nginx 目录自动加斜线”/”

    默认配置当你访问http://abc.example.com/dir 时不会加”/” 常见做法 if (-d $request_filename){  rewrite ^/(.*)([^/])$ ht ...

  6. The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.

    springboot 表单体积过大时报错: The multi-part request contained parameter data (excluding uploaded files) tha ...

  7. Java学习:网络编程总结

    Java网络编程总结 一.概述 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统.网络编程就就是编写程序使联网的两个(或多个)设备( ...

  8. JavaIO学习:序列化流

    对象流 1.涉及到的类 ObjectInputStream 和 ObjectOutputStream 用于存储和读取基本数据类型数据或对象的处理流. 2.作用 ObjectOutputStream:内 ...

  9. 2019-11-29-VisualStudio-好用插件集合

    原文:2019-11-29-VisualStudio-好用插件集合 title author date CreateTime categories VisualStudio 好用插件集合 lindex ...

  10. 让你的 vs code 跑在云上,用手机浏览器就能写代码

    让你的vs code 跑在云服务器上 在B站上看到一个视频Run VS Code in the browser with massive computing resources(教你如何配置一个云ID ...