1、占位符和格式化输出

示例代码

  1. #格式化输出
  2. # % s d
  3. # name = input('请输入姓名')
  4. # age = input('请输入年龄')
  5. # height = input('请输入身高')
  6. # msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
  7. # print(msg)
  8. """
  9. name = input('请输入姓名:')
  10. age = input('请输入年龄:')
  11. job = input('请输入工作:')
  12. hobbie = input('你的爱好:')
  13.  
  14. msg = '''------------ info of %s -----------
  15. Name : %s
  16. Age : %d
  17. job : %s
  18. Hobbie: %s
  19. ------------- end -----------------''' %(name,name,int(age),job,hobbie)
  20. print(msg)
  21. """
  22. name = input('请输入姓名')
  23. age = input('请输入年龄')
  24. height = input('请输入身高')
  25. msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
  26. print(msg)

  1. '''
  2. 用户登陆(三次输错机会)且每次错误时显示剩余的错误次数(提示:使用字符串格式化)
  3. '''
  4. i =
  5. while i < :
  6. i +=
  7. name = input("请输入您的登录名:")
  8. passwd = input("请输入您的密码:")
  9. if name == '张三' and passwd == '':
  10. print("登陆成功!")
  11. break
  12. else:
  13. print ("您的登录名或者密码不正确!您还有%s次输入机会!" % str( - i)) #要在print内才可以,否者报错:TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

2、while else语句

  1. count = 0
  2. while count <= 5 :
  3. count += 1
  4. if count == 3:break
  5. print("Loop",count)
  6.  
  7. else:
  8. print("循环正常执行完啦")
  9. print("-----out of while loop ------")

3、逻辑运算

  1. #and or not
  2. #优先级,()> not > and > or
  3. # print(2 > 1 and 1 < 4)
  4. # print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
  5. # T or T or F
  6. #T or F
  7. # print(3>4 or 4<3 and 1==1) # F
  8. # print(1 < 2 and 3 < 4 or 1>2) # T
  9. # print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # T
  10. # print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # F
  11. # print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
  12. # print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
  13.  
  14. #ps int ----> bool 非零转换成bool True 0 转换成bool 是False
  15. # print(bool(2))
  16. # print(bool(-2))
  17. # print(bool(0))
  18. # #bool --->int
  19. # print(int(True)) # 1
  20. # print(int(False)) # 0
  21.  
  22. '''x or y x True,则返回x'''
  23. # print(1 or 2) # 1
  24. # print(3 or 2) # 3
  25. # print(0 or 2) # 2
  26. # print(0 or 100) # 100
  27.  
  28. # print(2 or 100 or 3 or 4) # 2
  29.  
  30. # print(0 or 4 and 3 or 2)
  31. '''x and y x True,则返回y'''
  32. # print(1 and 2)
  33. # print(0 and 2)
  34. print(2 or 1 < 3)
  35. print(3 > 1 or 2 and 2)

python基础之二:占位符、格式化输出、while else 、逻辑运算的更多相关文章

  1. Python基础__字符串拼接、格式化输出与复制

    上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...

  2. python基础之常用模块以及格式化输出

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  3. python 基础(while 循环、格式化输出、运算符、编码初识)

    while循环 break 终止当前循环 count = 1 while count < 3: print(count) count += 1 break # while循环中一旦代码执行到br ...

  4. 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别

    <Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...

  5. Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?

    Python中使用百分号占位符的字符串格式化方法中%s和%r表示需要显示的数据对应变量x会以str(x)还是repr(x)输出内容展示. 关于str和repr的关系请见: <Python中rep ...

  6. python基础知识(二)

    python基础知识(二) 字符串格式化 ​ 格式: % 类型 ---- > ' %类型 ' %(数据) %s 字符串 ​ print(' %s is boy'%('tom')) ----> ...

  7. python入门6 字符串拼接、格式化输出

    字符串拼接方式    1  使用 + 拼接字符串 2 格式化输出:%s字符串 %d整数 %f浮点数 %%输出% %X-16进制 %r-原始字符串 3 str.format() 代码如下: #codin ...

  8. 『Go基础』第8节 格式化输出

    输出就是将数据信息打印到电脑屏幕上. 本节我们就来学习一下Go语言中的三种输出方式: Print().Println().Printf(). 1.Print() Print()主要的一个特点就是打印数 ...

  9. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

  10. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

随机推荐

  1. 前后台$.post交互并返回JSON对象

    1.前台代码: $.post(url,{"blogId":blogId},function(reData){ if(reData.state=="success" ...

  2. Qt Quick 常用元素:ComboBox(下拉列表) 与 ProgressBar(进度条)

    一.ComboBox ComboBox,即下拉列表框,由一个列表框和一个标签控件(或编辑控件)组成.ComboBox 的下拉列表是使用 Menu 实现的,列表内的每个条目对应一个 Menultem. ...

  3. [数据库] SQL 语法之基础篇

    一.什么是 SQL ? SQL 是 Structured Query Language(结构化查询语言)的缩写,是一种专门用来与数据库沟通的语言.与其他语言(如英语或 C.C++.Java 这样的编程 ...

  4. Salesforce学习之路(二)Profile

    如上篇文章所述,针对User来讲,最重要的概念便是Profile和Role,因为Profile于Security息息相关,这是一个合格的产品中十分重要的一环. 何为Profile? 前文所讲--就是一 ...

  5. 动态引用存储——集合&&精确的集合定义——泛型

    1,集合宏观理解 1.1,为什么引入集合? 对于面向对象的语言来说,操作对象的功能不可或缺. 为了方便对对象进行操作和处理,就必须要对对象进行暂时的存储.[数据最终存在数据库里] 使用数组来存储对象的 ...

  6. 遇到 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 ...

  7. sql server取日期各个组成部分的datename()函数

    SQL Server中的日期类型datetime的默认格式是yyyy-mm-dd hh:mi:ss:mmm,很多时候我们可能会需要获取日期中的某个组成部分,因此SQL Server提供了一个daten ...

  8. lua中,两种json和table互转方法的效率比较

    lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...

  9. [跨域问题]ssm+vue前后台分离跨域问题解决方法

    跨域未解决时: Access to XMLHttpRequest at 'http://localhost:8080/vue/findall from origin 'http://localhost ...

  10. 【spring boot】加载同名Bean解决方法

    原文地址:https://blog.csdn.net/liuyueyi25/article/details/83280239 @SpringBootApplication @ComponentScan ...