python note 02 格式化与判断、字符串转换
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)
ps:输入的内容要与格式化输出的数量的相匹配
2、格式化输出如果有%,就用%%转义
name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
print(msg)
3、当while else 语句被break打断了,将不会执行else语句,将break改为pass,程序将继续执行else语句
count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count) else:
print("循环正常执行完啦")
print("-----out of while loop ------")
4、
最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字。
01010101
ascii 只能表示256种可能,太少,
创办了万国码 unicode
16表示一个字符不行,32位表示一个字符。
A 01000001010000010100000101000001
B 01000010010000100100001001000010
我 01000010010000100100001001000010
Unicode 升级 utf-8 utf-16 utf-32
8位 = 1字节bytes
utf-8 一个字符最少用8位去表示,英文用8位 一个字节
欧洲文字用16位去表示 两个字节
中文用24 位去表示 三个字节
utf-16 一个字符最少用16位去表示
gbk 中国人自己发明的,一个中文用两个字节 16位去表示。
1bit 8bit = 1bytes
1byte 1024byte = 1KB
1KB 1024kb = 1MB
1MB 1024MB = 1GB
1GB 1024GB = 1TB
5、逻辑运算优先级
#and or not
#优先级,()> not > and > or
print(2 > 1 and 1 < 4) # T
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2) # T
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 #也可以从前往后看
True and ==> 继续看后面
True or ==> True
False and ==> False
False or ==> 继续看后面
ps:int ----> bool 非零转换成bool True,0 转换成bool 是False
print(bool(2)) # T
print(bool(-2)) # T
print(bool(0)) # F
# #bool --->int
print(int(True)) #
print(int(False)) #
'''x or y 若 x True,则返回x'''
print(1 or 2) #
print(3 or 2) #
print(0 or 2) #
print(0 or 100) #
print(2 or 100 or 3 or 4) #
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) # T
#(以数字为例)从前往后看,如果遇到and,两边都为True的话输出后面的数字,如果一边为False则输出0.
6、字符串转换
#将字符串转化为数字
a = ""
print(type(a),a)
#输出<class 'str'> 123
b = int(a)
print(type(b),b)
#输出<class 'int'> 123
#将八进制转化为十进制
num = ""
v = int(num, base= 8)
print(v)
#输出64
#将十六进制转化为十进制
num = ""
v = int(num, base= 16)
print(v)
#输出256
python note 02 格式化与判断、字符串转换的更多相关文章
- python note 03 切片及对字符串操作
1.计算 1 - 2 + 3 ... + 99 中除了88以外的数之和 i = 1 sum = 0 while i < 100 : if i == 88 : i = i + 1 continue ...
- python学习笔记(datetime、字符串转换)
datetime对象与字符串可以互相转化 代码如下: from datetime import datetime def datetime_string(time): return time.strf ...
- python基础入门 整型 bool 字符串
整型,bool值,字符串 一.整型 整型十进制和二进制 整型:整型在Python中的关键字用int来表示; 整型在计算机中是用于计算和比较的 可进行+ - * / % //(整除) **(幂运算) 十 ...
- Delphi判断字符串中是否包含汉字,并返回汉字位置
//1,函数代码{判断字符串是否包含汉字// judgeStr:要判断的字符串//posInt:第一个汉字位置}function TForm2.IsHaveChinese(judgeStr: stri ...
- Python判断字符串编码以及编码的转换
转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...
- Python系列之模块、和字符串格式化
Python 模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 模块分为 ...
- Python数据类型-02.字符串
本文主要记录字符串的相关知识,包括字符串的定义特点,常用方法和 请知悉: 计算机中,一切皆为对象世界万物,皆为对象,一切对象皆可分类 1.什么是字符串? 类似"hello world&quo ...
- Python语法速查: 3. 字符串格式化
返回目录 (1)简易字符串格式化 字符串属于不可变序列,只能生成新的,不能改变旧的.“字符串格式化”有点像以前C语言的sprintf,可以将若干变量代入格式化的字符串,生成一个符合要求的新字符串. 转 ...
- 14.python类型总结,集合,字符串格式化
借鉴:https://www.cnblogs.com/linhaifeng/articles/5935801.html https://www.cnblogs.com/wupeiqi/article ...
随机推荐
- 深入学习Motan系列(四)—— 客户端
困惑的袋鼠,对框架的把握有些茫然,但是仍然一步步向前,行动总比一直迷茫停止不前要好,您说呢,各位客官? 这篇开始客户端的分析.有些地方的代码,就不每段都标出了,中间有跳跃的地方,请自己对照代码来看.鄙 ...
- linux下查询java进程以及杀掉其进程
1.使用命令: ps -ef|grep java 查询到到自己想要kill掉的进程id 2.使用命令: kill -9 id(这里的id为你上一步查找到的id)
- ios-上传图片到后台
做第一个项目时,有个版块的个人信息的编辑涉及到头像修改,老大说项目里有通用的代码,让我自己去找.总算找到,搞了许久才弄好,看来理解能力还需要提高啊!! #pragma mark- 修改头像上传后保存 ...
- 使用openresty && minio && thumbor 构建稳定高效的图片服务器
备注: minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python 的图片处理服务器,支持图片的裁剪 ...
- What’s New In GRANDstack?
转自:https://blog.grandstack.io/whats-new-in-grandstack-310c067fea4a There’s been a lot of activity in ...
- 01python语言程序设计基础——初识python
1.python的字符串中format函数用法 format 函数可以接受不限个参数,位置可以不按顺序. In [2]: "{} {}".format("hello& ...
- php微服务框架 PHP-MSF 的容器部署和使用
评论:1 · 阅读:8412· 喜欢:1 一.需求 PHP-msf 是 Carema360 开发的 PHP 微服务框架,目前我没有实际用过,但是市面上的微服务框架要么在推崇 Spring 系,要么是 ...
- WIN7X64SP1极限精简版by双心
WIN7X64SP1极限精简版by双心 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=405044&page=1&ext ...
- 报错:org.apache.hadoop.hive.metastore.HiveMetaException: Failed to get schema version.
报错环境: CDH中集成的hive服务,启动报错,所以初始化一下元数据. 配置文件:/etc/hive/conf hive-site.xml 命令目录:/opt/cloudera/parcels/CD ...
- linux 配置vue环境
系统 [root@Gao conf.d]# uname -a 工具 1.Final Shell 2.工具截图 需要下载的部分 node.js npm cnpm vue-cli 安装nod ...