python学习-变量和简单类型(二)
学习笔记中的源码:传送门
2.python标准数据类型:数值(numbers)、字符串(string)、布尔(True、False)、列表(list)、元组(tuple)、字典(dict)
其中数值类型有:int(整型,有四种表示形式:十进制、二进制、八进制、十六进制)、float(浮点型,有两种表示形式:十进制、科学计数法,注意浮点数必须包含一个小数点,否则会被当作整型处理)、complex(复数)
关于python的复数:
a.表示复数的语法是real + image j
b.实部和虚部都是浮点数
c.虚部的后缀可以是 “j” 或者 “J”
d.复数的 conjugate 方法可以返回该复数的共轭复数。
e.虚数不能单独存在,它们总是和一个值为 0.0 的实数部分一起构成一个复数
python中标识符(就是用于给程序中变量、类、方法命名的符号)的命名规则:
1)由字母、数字、下划线(_)组成,但不能数字打头
2)不能是python关键字,但可以包含关键字
3)不能包含空格
python中变量的特点:
1)变量无需声明即可直接赋值:对于一个不存在的变量赋值就相当于定义了一个新变量;
2)变量的数据类型可以动态改变
3.print的用法:
sep可以改变变量间的默认分割方式,不指定的话默认空格
file参数指定print()函数的输出目标,file参数的默认值为sys.stdout,该默认值代表了系统标准输出,也就是屏幕,可以通过改变该参数让print()函数输出到指定文件中
f = open("poem.txt", 'w') print('沧海月明珠有泪', file=f) print('蓝田玉暖日生烟', file=f) f.close()
拼接字符串: 使用+
字符串与数字拼接 :需使用str() 进行类型转换
获取用户输入: input() 函数 ,返回的总是一个字符串
长字符串:
原始字符串: 就是说原始字符串以‘r’开头,原始字符串不会把反斜线当成特殊字符,但是如果包含引号同样需要转义
bytes:保存的是原始的字节(二进制格式)数据,这里暂且模仿字符串的名字称其为字节串
"""将一个字符串转换成bytes对象的三种方式""" b1 = bytes() # 直接在ASCII字符串前添加b前缀来得到字节串 b2 = b'' b3 = b'hello' print(b3) print(b3[0]) print(b3[2:4]) # 调用bytes()函数来构建字节串 b4 = bytes('我爱python编程',encoding='utf-8') print(b4) # 调用字符串的encode方法来构建字节串 b5 = "学习python很有趣".encode('utf-8') print(b5)
st = b5.decode('utf-8') # 将bytes对象解码成字符串,默认使用UTF-8进行解码
print(st)
输出结果: b'hello' 104 b'll' b'\xe6\x88\x91\xe7\x88\xb1python\xe7\xbc\x96\xe7\xa8\x8b' b'\xe5\xad\xa6\xe4\xb9\xa0python\xe5\xbe\x88\xe6\x9c\x89\xe8\xb6\xa3'
学习python很有趣
python支持的转义字符:
\b:退格符
\n:换行符
\r:回车符
\t:制表符
\":双引号
\':单引号
\\:反斜线
字符串格式化:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
user = 'mengmeng' age = 25 print("%s is a %s years old girl" % (user, age)) # %s使用str()将变量或表达式转为字符串 # %d表示转换为带符号的十进制形式的整数 num2 = 30 print("num2 is: %06d" % num2) # 0:表示不补充空格而是补充0 print("num2 is: %+06d" % num2) # +:表示数值总带着符号(正数带+,负数带-) print("num2 is: %-6d" % num2) # -:表示左对齐 # %f表示转换为十进制形式的浮点数 my_value = 3.001415926535 print("my_value is: %8.3f" % my_value) # 最小宽度为8,小数点后保留3位 print("my_value is: %08.3f" % my_value) # 最小宽度为8,小数点后保留3位,左边补0 print("my_value is: %+08.3f" % my_value) # 最小宽度为8,小数点后保留3位,左边补0,始终带符号 the_name = "Charlie" print("the name is: %.3s" % the_name) # 只保留3个字符 print("the name is: %10.2s" % the_name) # 只保留2个字符,最小宽度为10 输出结果: mengmeng is a 25 years old girl num2 is: 000030 num2 is: +00030 num2 is: 30 my_value is: 3.001 my_value is: 0003.001 my_value is: +003.001 the name is: Cha the name is: Ch
大小写相关方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
a = 'our domain is crazyit.org' print(a.title()) # 每个单词的首字母大写 print(a.lower()) # 每个字母小写 print(a.upper()) # 每个字母大写 输出结果: Our Domain Is Crazyit.Org our domain is crazyit.org OUR DOMAIN IS CRAZYIT.ORG
删除空白:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = ' this is a puppy ' print(s.lstrip()) # 删除字符串左边的空白 print(s.rstrip()) # 删除字符串右边的空白 print(s.strip()) # 删除字符串两边的空白 print(s) # s本身不会被改变 s2 = 'i think it is a scarecrow' print(s2.lstrip('itow')) # 删除左边的i、t、o、w字符 print(s2.strip('itow')) # 删除右边的i、t、o、w字符 print(s2.rstrip('itow')) # 删除两边的i、t、o、w字符 输出结果: this is a puppy this is a puppy this is a puppy this is a puppy think it is a scarecrow think it is a scarecr i think it is a scarecr
查找、替换方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = 'crazyit . org is a good site' print(s.startswith('crazyit')) # 判断s是否以crayit开头 print(s.endswith('site')) print(s.find('org')) # 查找子串在字符串中出现的位置 print(s.index('org')) print(s.find('org', 9)) print(s.replace('it', 'xxxx')) # 替换字符串 输出结果: True True 10 10 10 crazyxxxx . org is a good sxxxxe
分割、连接方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
"""分隔、连接方法""" s = 'crazyit.org is a good site' print(s.split()) # 使用空白对字符串进行分割 print(s.split(None, 2)) # 使用空白对字符串进行分割,最多只分割前两个单词 print(s.split('.')) # 使用.进行分割 mylist = s.split() print('/'.join(mylist)) # 使用/作为分隔符,将mylist连接成字符串 print(','.join(mylist)) # 使用,作为分隔符,将mylist连接成字符串 输出结果: ['crazyit.org', 'is', 'a', 'good', 'site'] ['crazyit.org', 'is', 'a good site'] ['crazyit', 'org is a good site'] crazyit.org/is/a/good/site crazyit.org,is,a,good,site
赋值运算符: = 扩展后的赋值运算符:+= -=等等
算术运算符:+ - * /(普通除法,有小数) //(整除) %(取余) **(乘方)
索引运算符:如列表的索引
比较运算符与bool类型:> >= < <= ==(判断变量值是否相等)) != is(判断两个变量引用的对象是否相同) is not True False
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import time """python的两个bool值是True和False,特别地,True可被当做整数1使用,Flase可被当做整数0使用""" print("1和True是否相等", 1 == True) print("0和False是否相等", 0 == False) """== 比较两个变量的值,is比较两个变量是否引用同一个对象""" a = time.gmtime() b = time.gmtime() print(a == b) print(id(a)) print(id(b)) # 查看对象的内存地址 print(a is b) # 每次调用gmtime()都返回不同的对象 输出结果: 1和True是否相等 True 0和False是否相等 True True 1897810265904 1897810266040 False
逻辑运算符:and or not
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
"""逻辑运算符 and or not:与 或 非""" print(not False) print(5 > 3 and 20 > 10) print(4 >= 5 or "c" > "a") 输出结果: True True True
三目运算符:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
"""True_statements if expression else False_statements 三目运算符的规则:先对逻辑表达式expression求值,如果逻辑表达式返回True,则返回True_statements; 如果逻辑表达式返回False,则执行并返回False_statements """ a = 5 b = 3 st = "a 大于 b" if a > b else "a不大于b" print(st) 输出结果: a 大于 b
in运算符:判断某个成员是否位于序列中,反义词:not in
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = 'crazyit.org' print('it' in s) print('it' not in s) 输出结果: True False