python基础之二:占位符、格式化输出、while else 、逻辑运算
1、占位符和格式化输出
示例代码
- #格式化输出
- # % s d
- # name = input('请输入姓名')
- # age = input('请输入年龄')
- # height = input('请输入身高')
- # msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
- # print(msg)
- """
- name = input('请输入姓名:')
- age = input('请输入年龄:')
- job = input('请输入工作:')
- hobbie = input('你的爱好:')
- msg = '''------------ info of %s -----------
- Name : %s
- Age : %d
- job : %s
- Hobbie: %s
- ------------- end -----------------''' %(name,name,int(age),job,hobbie)
- print(msg)
- """
- name = input('请输入姓名')
- age = input('请输入年龄')
- height = input('请输入身高')
- msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
- print(msg)
- '''
- 用户登陆(三次输错机会)且每次错误时显示剩余的错误次数(提示:使用字符串格式化)
- '''
- i =
- while i < :
- i +=
- name = input("请输入您的登录名:")
- passwd = input("请输入您的密码:")
- if name == '张三' and passwd == '':
- print("登陆成功!")
- break
- else:
- print ("您的登录名或者密码不正确!您还有%s次输入机会!" % str( - i)) #要在print内才可以,否者报错:TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
2、while else语句
- count = 0
- while count <= 5 :
- count += 1
- if count == 3:break
- print("Loop",count)
- else:
- print("循环正常执行完啦")
- print("-----out of while loop ------")
3、逻辑运算
- #and or not
- #优先级,()> not > and > or
- # print(2 > 1 and 1 < 4)
- # print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
- # T or T or F
- #T or F
- # print(3>4 or 4<3 and 1==1) # F
- # print(1 < 2 and 3 < 4 or 1>2) # T
- # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # T
- # print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # F
- # print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
- # print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
- #ps int ----> bool 非零转换成bool True 0 转换成bool 是False
- # print(bool(2))
- # print(bool(-2))
- # print(bool(0))
- # #bool --->int
- # print(int(True)) # 1
- # print(int(False)) # 0
- '''x or y x True,则返回x'''
- # print(1 or 2) # 1
- # print(3 or 2) # 3
- # print(0 or 2) # 2
- # print(0 or 100) # 100
- # print(2 or 100 or 3 or 4) # 2
- # print(0 or 4 and 3 or 2)
- '''x and y x True,则返回y'''
- # print(1 and 2)
- # print(0 and 2)
- print(2 or 1 < 3)
- print(3 > 1 or 2 and 2)
python基础之二:占位符、格式化输出、while else 、逻辑运算的更多相关文章
- Python基础__字符串拼接、格式化输出与复制
上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...
- python基础之常用模块以及格式化输出
模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- python 基础(while 循环、格式化输出、运算符、编码初识)
while循环 break 终止当前循环 count = 1 while count < 3: print(count) count += 1 break # while循环中一旦代码执行到br ...
- 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别
<Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...
- Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?
Python中使用百分号占位符的字符串格式化方法中%s和%r表示需要显示的数据对应变量x会以str(x)还是repr(x)输出内容展示. 关于str和repr的关系请见: <Python中rep ...
- python基础知识(二)
python基础知识(二) 字符串格式化 格式: % 类型 ---- > ' %类型 ' %(数据) %s 字符串 print(' %s is boy'%('tom')) ----> ...
- python入门6 字符串拼接、格式化输出
字符串拼接方式 1 使用 + 拼接字符串 2 格式化输出:%s字符串 %d整数 %f浮点数 %%输出% %X-16进制 %r-原始字符串 3 str.format() 代码如下: #codin ...
- 『Go基础』第8节 格式化输出
输出就是将数据信息打印到电脑屏幕上. 本节我们就来学习一下Go语言中的三种输出方式: Print().Println().Printf(). 1.Print() Print()主要的一个特点就是打印数 ...
- Python基础学习二
Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...
- Python 基础语法(二)
Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...
随机推荐
- 前后台$.post交互并返回JSON对象
1.前台代码: $.post(url,{"blogId":blogId},function(reData){ if(reData.state=="success" ...
- Qt Quick 常用元素:ComboBox(下拉列表) 与 ProgressBar(进度条)
一.ComboBox ComboBox,即下拉列表框,由一个列表框和一个标签控件(或编辑控件)组成.ComboBox 的下拉列表是使用 Menu 实现的,列表内的每个条目对应一个 Menultem. ...
- [数据库] SQL 语法之基础篇
一.什么是 SQL ? SQL 是 Structured Query Language(结构化查询语言)的缩写,是一种专门用来与数据库沟通的语言.与其他语言(如英语或 C.C++.Java 这样的编程 ...
- Salesforce学习之路(二)Profile
如上篇文章所述,针对User来讲,最重要的概念便是Profile和Role,因为Profile于Security息息相关,这是一个合格的产品中十分重要的一环. 何为Profile? 前文所讲--就是一 ...
- 动态引用存储——集合&&精确的集合定义——泛型
1,集合宏观理解 1.1,为什么引入集合? 对于面向对象的语言来说,操作对象的功能不可或缺. 为了方便对对象进行操作和处理,就必须要对对象进行暂时的存储.[数据最终存在数据库里] 使用数组来存储对象的 ...
- 遇到 npm WARN npm npm does not support Node.js vx.x.x的解决办法
遇到如下警告 PS C:\Users\Administrator> npm npm WARN npm npm does not support Node.js v9.11.2 npm WARN ...
- sql server取日期各个组成部分的datename()函数
SQL Server中的日期类型datetime的默认格式是yyyy-mm-dd hh:mi:ss:mmm,很多时候我们可能会需要获取日期中的某个组成部分,因此SQL Server提供了一个daten ...
- lua中,两种json和table互转方法的效率比较
lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...
- [跨域问题]ssm+vue前后台分离跨域问题解决方法
跨域未解决时: Access to XMLHttpRequest at 'http://localhost:8080/vue/findall from origin 'http://localhost ...
- 【spring boot】加载同名Bean解决方法
原文地址:https://blog.csdn.net/liuyueyi25/article/details/83280239 @SpringBootApplication @ComponentScan ...