python 基础2
格式化输出
1 #格式化输出 2 name=input('姓名') 3 age=int(input('年龄')) 4 height=int(input('身高')) 5 msg=""" 6 姓名%s 7 年龄%d 8 身高%d"""%(name,age,height); 9 print(msg)
%%输出一个%,前面一个转义
while else
当while循环被break,打断时,不执行else
1 #格式化输出 2 name=input('姓名') 3 age=int(input('年龄')) 4 height=int(input('身高')) 5 msg=""" 6 姓名%s 7 年龄%d 8 身高%d"""%(name,age,height); 9 print(msg)
初始编码
电脑的传输实际上都是0和1
电报,电脑的传输,存储都是01010101
最早的'密码本' 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位去表示。
11000000
1bit 8bit = 1bytes
1byte 1024byte = 1KB
1KB 1024kb = 1MB
1MB 1024MB = 1GB
1GB 1024GB = 1TB
运算符
优先级:从左至右执行
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 23 '''x or y x True,则返回x''' 24 # print(1 or 2) # 1 25 # print(3 or 2) # 3 26 # print(0 or 2) # 2 27 # print(0 or 100) # 100 28 29 30 # print(2 or 100 or 3 or 4) # 2 31 32 # print(0 or 4 and 3 or 2) 33 '''x and y x True,则返回y''' 34 # print(1 and 2) 35 # print(0 and 2) 36 print(2 or 1 < 3) 37 print(3 > 1 or 2 and 2)
基础数据类型
bo0l()转换成bool
int()转换成数值
整型
1 i = 100 2 print(i.bit_length()) 3 ''' 4 bit_length 5 1 0000 0001 1 6 2 0000 0010 2 7 3 0000 0011 2
bool
1 #bool True False 2 3 #int ----> str 4 i = 1 5 s = str(i) 6 #str ---> int 7 s = '123' 8 i = int(s) 9 10 #int ----->bool 只要是0 ----》False 非0就是True 11 i = 3 12 b = bool(i) 13 print(b) 14 #bool----> int 15 #True 1 16 #False 0 17 ''' 18 ps: 19 while True: 20 pass 21 while 1: 效率高 22 pass 23 ''' 24 25 #str --->bool 26 27 #s = "" -----> False 28 #非空字符串都是True 29 #s = "0" -----> True 30 31 # s 32 # if s: 33 # print('你输入的为空,请重新输入') 34 # else: 35 # pass
字符串
1 #bool True False 2 3 #int ----> str 4 i = 1 5 s = str(i) 6 #str ---> int 7 s = '123' 8 i = int(s) 9 10 #int ----->bool 只要是0 ----》False 非0就是True 11 i = 3 12 b = bool(i) 13 print(b) 14 #bool----> int 15 #True 1 16 #False 0 17 ''' 18 ps: 19 while True: 20 pass 21 while 1: 效率高 22 pass 23 ''' 24 25 #str --->bool 26 27 #s = "" -----> False 28 #非空字符串都是True 29 #s = "0" -----> True 30 31 # s 32 # if s: 33 # print('你输入的为空,请重新输入') 34 # else: 35 # pass
支持连贯用法
#字符串逆转,并大写,拆分
s='abcdefghijkl'
a=s[-1::-1].swapcase().split('D')
print(a)
数据类型
列表
元祖
元组被称为只读列表,即数据可以被查询,但不能被修改,所以,字符串的切片操作同样适用于元组。例:(1,2,3)("a","b","c")
列表list
列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:
li = [‘alex’,123,Ture,(1,2,3,’wusir’),[1,2,3,’小明’,],{‘name’:’alex’}]
列表相比于字符串,不仅可以储存不同的数据类型,而且可以储存大量数据,32位python的限制是 536870912 个元素,64位python的限制是 1152921504606846975 个元素。而且列表是有序的,有索引值,可切片,方便取值。
集合

浙公网安备 33010602011771号