319 Python基础之格式化输出、逻辑运算符、编码、in not in、while else、
一、格式化输出
占位符%,字符串占位符%s,数字占位符%d
第一种
name = input('姓名')
age = input('年龄')
hobby = input ("爱好")
msg="我叫%s,我今年%d,我喜欢%s"%(name,int(age),hobby)
print(msg) 第二种
dic = {'name': '老男孩', 'age': 51, 'hobby': '吃'}
msg = '我叫%(name)s,我今年%(age)d,我喜欢%(hobby)s' % dic
print(msg)
第三种
在格式化输出中单纯的显示% 用%% 解决
name=input('输入名字')
age=input('年龄')
msg='我叫%s,今年%d,学习进度是1%%' % (name,int(age))
print(msg)
二、while else
如果循环被break打断,程序不会走else
例如:
count=1
while True:
print(count)
if count==3:break
count+= 1
else:
print('正常') count=1
flag=True
while flag:
print(count)
if count ==3:
flag=False
count+=1
else:
print('正常'
三、in not in
判断子元素是否在原字符串(字典,列表,集合)中:
s = 'abcdefghijklmn'
print('abc' in s)
print('ds' in s)
print('abc' in s)
print('qwe' not in s)
应用于敏感词排查
comment = input('留言板')
if '进行' in comment:
print('用词过于随便,请您三思')
四、逻辑运算符 and or not
第一种:前后都是比较运算
优先级:()>not>and>or,同一个优先级,从左至右依次计算。
print(1 > 2 and 3 < 4 and 3 > 2 or 2 < 3) True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) True
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) False
第二种:前后都是数值运算
x or y if x True,则 return x,否则 return y
x and y if x True,则return y,否则return x
int ---> bool 非0即True,0为False
bool---> int True 1 False 0
print(int(True))
print(int(False))
print(bool(100))
print(bool(0))
print(1 or 3) 1
print(1 or 3)1
print(2 or 3)2
print(0 or 3)3
print(-1 or 3)-1
print(1 and 2)2
print(0 and 2)0
第三种:混合。
print(1 > 2 or 3 and 4)4
print(2 or 2 > 3 and 4)2
print(0 or 2 > 3 and 4)False
五、编码
计算机在存储和传输的时候,用的是 01010101的二进制编码。
最早的是asiic 包含数字,英文,特殊字符。8位,8位 = 1 byte 表示一个字符。现在Python2x还在用。
万国码unicode,将所有国家的语言包含在这个密码本。
初期:16位,两个字节,表示一个字符。
升级:32位,四个字节,表示一个字符。
优点,大而全,缺点,占据太多内存,资源浪费。
utf-8。最少用8位(一个字节),表示一个字符。(Python3x的默认编码)
英文:a :00010000 用8位表示一个字符。
欧洲:00010000 00010000 16位两个字节表示一个字符。
亚洲 中 :00010000 00010000 00010000 24位,三个字节表示一个字符。
gbk:国标。(国产)
只包含:英文中文。
英文:a :00010000 8位,一个字节表示一个字符。
中文:中:00010000 00010000 16位,两个字节表示一个字符。
8 bit = 1byte
1024byte=1kb
1024kb = 1MB
1024MB = 1GB
1024GB = 1TB
319 Python基础之格式化输出、逻辑运算符、编码、in not in、while else、的更多相关文章
- Python基础篇(格式化输出,运算符,编码):
Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s 替换字符串 %d 替换整体数字 ...
- python基础_格式化输出(%用法和format用法)(转载)
python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...
- 2.Python基础认识(格式化输出,while语句,运算符,编码,单位转化)
Python基础认识 1.字符串的格式化初识及占位符的简单应用 字符串的格式化 按照既定的要求进行有规定排版的一种输出方式. #我们想要输出的格式如下: ----------------------- ...
- python基础(5):格式化输出、基本运算符、编码问题
1. 格式化输出 现在有以下需求,让⽤户输入name, age, job,hobby 然后输出如下所⽰: ------------ info of Alex Li ----------- Name : ...
- python基础(格式化输出、基本运算符、编码)
1,格式化输出. 现有一练习需求,问用户的姓名.年龄.工作.爱好 ,然后打印成以下格式 ------------ info of Alex Li ----------- Name : Alex Li ...
- Python学习day05 - Python基础(3) 格式化输出和基本运算符
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python基础之格式化输出、运算符、数字与布尔值互换以及while...else
python是一天学一点,就这样零零碎碎…… 格式化输出 %是占位符,%s是字符串格式,%d整数格式,%f是浮点数格式 name = input('输入姓名') age = input('输入年龄') ...
- python基础_格式化输出(%用法和format用法)
目录 %用法 format用法 %用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 1 >>> print('%o' % 2 ...
- python之路-格式化输出、编码
格式化输出 user = input('Username:') pwd = input('Password:') msg = 'your username:%s,your password:%d,10 ...
随机推荐
- Python修改文件权限
os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...
- go http请求基础
1.请求方法: get post get 加请求参数,请求参数会加到url后面 post加请求参数,请求参数会放在body里面 请求方式:1.直接在url后面加参数 如:http://www.tes ...
- mysql主从从
1.从官网下载安装percona-xtrabackup2.xtrabackup只能备份和恢复innodb的表,所以这里用innobackupex,可以实现对myisam和innodb的表在线备份和恢复 ...
- org.springframework.beans.factory.UnsatisfiedDependencyException
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,启动时报错如下: 严重: Context initialization failed org.springframew ...
- reveal end of document
window - Preferences - Run/Debug - Console 将 Console buffer size (characters)设置大一点
- mysql中百万级别分页查询性能优化
前提条件: 1.表的唯一索引 2.百万级数据 SQL语句: select c.* FROM ( SELECT a.logid FROM tableA a where 1 = 1 <#if pho ...
- ssh不检查server变化
嵌入式linux开发时经常需要远程登录到板上,但由于开发过程还经常会重新下载内核和文件系统,导致登录时总提示host变了,blablabla,解决方案是在.ssh/config对应的Host项下面加上 ...
- 在多点环境下使用cas实现单点登陆及登出
CAS 介绍 CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以下特 ...
- u-boot中断功能初步分析之---------按键中断
作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 以前一直有个疑问,在U-boot下到底能不能使用中断,为了验证这个问题,于是乎,昨天晚上我在自己的 TQ2 ...
- 华为nova3超级慢动作酷玩抖音,没有办法我就是这么强大!
华为nova3超级慢动作酷玩抖音,没有办法我就是这么强大! 在华为最新发布的nova 3手机上,抖音通过华为himedia SDK集成了60fps.超级慢动作等华为媒体开放能力,在加持这些能力后,抖音 ...