• 目录
标准数据类型Number(数字)int(整型) float(浮点型)bool(布尔型) complex(复数)string(字符串)List(列表)Tuple(元组)Dict(字典) Sets(集合)NoneNone为空值,不能解释为0。0是有意义的,None则为「特殊空值」。 Number, String,Tuple 为不可变类型 List,Tuple,Dict,Sets 可称之为容器
数值分类整数类型 浮点数类型复数类型关系相互转换运算BIF(内建函数)random 随机数String (字符串)字符串内涵用引号括起来,单引号( 撇号)‘string' 或双引号"string",两者可以相互包含,使用灵活。 字符串的独特表示 三引号 ‘’‘ ’‘’ 或“”“ ”“” 。特点 ①可跨多行,多用来对py文件的解释说明。②可包含单/双引号原始字符串 ,r' ' 如果在字符串前面加r字符,则表示让这个字符串里面的内容失去转义的意义 字符串 有不可变性 ,指向永远不变。类型转换 list(str)tuple(str) 内建函数标准列表的操作都适用于字符串 常用操作大小写str.title() 每个单词首字母大写 str.lower(); str.upper() 全部单词都转为小写;全部单词都转为大写str.swapcase() 大小写反转str.capitalize() 首字母大写取余都小写 合并/拼接 ① +号 ② % 或 join() 空白概述:任何非打印字符,如空格,制表符和换行符添加空白 \t (制表符), \n(换行符)删除空白 strip/lsrip/rstrip()更改显示宽度str.center() 使字符串居中,可指定宽度和填充的字符(默认为空格)str.ljust(); str.rjust() 左对齐;右对齐str.zfill() 编码str.encode(); str.decode()标准数据类型
Number(数字)
int(整型)
float(浮点型)
bool(布尔型)
complex(复数)
string(字符串)
List(列表)
Tuple(元组)
Dict(字典)
Sets(集合)
None
None为空值,不能解释为0。0是有意义的,None则为「特殊空值」。
Number, String,Tuple 为不可变类型
List,Tuple,Dict,Sets 可称之为容器
数值
分类
整数类型
0b 2进制
a = 0b1101
print(a) #输出十进制 13
0o 8进制
a = 0o012
print(a) #输出十进制 10
0x 16进制
a = 0x000a
print(a) #输出十进制 10
浮点数类型
#带小数点的实数都是浮点数如:
print(type(0.12)) #<class 'float'>
print(type(1.12)) #<class 'float'>
复数类型
z = a +bj
a 为实数部分 z.real
b 为虚数部分 z.imag
关系
整数 →浮点数→复数
当做混合运算时取最宽类型
print(1+2) # 3
print(1.0+2) # 3.0
print(1.0+2.3) # 3.3
#加,减,乘,除都都遵循此法则
相互转换
int()
print(int(2.0)) # 2
print(int(2.3)) # 2
print(int(2.6)) # 2
# 会向上取整
float()
print(float(2)) # 2.0
print(float(2.3)) # 2.3
print(float(2.6)) # 2.6
complex()
print(complex(2)) # (2+0j)
print(complex(2.3, 2)) # (2.3+2j)
print(complex(2.6, 8)) # (2.6+8j)
运算
+ 加,- 减,* 乘,/ 除
print(9 + 2) # 11
print(9 - 2.0) # 7.0
print(9 * 2.0) # 18.0
print(9 / 3.0) # 3.0
# 不同类型混合运算时取宽类型
// 取整
print(9 // 2) # 4
print(9 // 2.0) # 4.0
% 求余/ 模
##############################################################
#1.操作数全为正整数
#X和Y均为正整数, X / Y的结果为Z
#则X % Y = X - (X * Z)
# 17 % 10 = 17 - (17 * (17 / 10))
print(17 - (17 * (17 / 10))) # 7
###############################################################
#2.有一个操作数为负数
#X和Y均为有一个为负整数,一个为正整数,X / Y结果的绝对值为Z,
#则X % Y的结果是 | X | -( | Y |*Z),符号位和Y相同。
#例如: 26 % -20
# 26 / -20 = -2,故Z = 2;
# | 26 | -( | -20 |*2) = 26 - 40 = -14
#由于除数Y为 - 20, 故26 % -20 = -14
#例如:-26 % 20
# -26 / 20 = -2,故Z = 2;
# | -26 | -( | 20 |*2) = 26 - 40 = -14
# 由于除数Y为20, 故 - 26 % 20 = 14
##############################################################
#3.两个操作数都为负数
#值的大小和两个操作数绝对值取模的结果相同,不过符号相反。
#######################################################################
#总结:除法中商的正负由两个操作数共同决定,相同为正,相异为负。
# 取模运算,结果的符号由第二个操作数的符号决定。
BIF(内建函数)
abs() 绝对值
print(abs(-20)) # 20
print(abs(20)) #20
ceil() 上入取整
import math
print(math.ceil(2.1)) # 3
floor() 下舍取整
import math
print(math.floor(2.6)) # 2
divmod() 返回元组
help(divmod)
#divmod(x, y, /)
# Return the tuple (x//y, x%y).
print(divmod(9, 4)) # (2, 1)
pow() 指数运
print(pow(2, 3)) #8
help(pow)
# pow(x, y, z=None, /)
# Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
#
# Some types, such as ints, are able to use a more efficient algorithm when
# invoked using the three argument form.
round() 四舍五入
print(round(2.5)) # 2
print(round(3.5)) # 4
print(round(4.4)) # 4
print(round(4.5)) # 4
print(round(4.6)) # 5
print(round(5.5)) # 6
print(round(6.5)) # 6
print(round(7.5)) # 8
print(round(8.8)) # 9
#虽说是四舍五入 看给个...
help(round)
#round(...)
# round(number[, ndigits]) -> number
#
# Round a number to a given precision in decimal digits (default 0 digits).
# This returns an int when called with one argument, otherwise the
# same type as the number. ndigits may be negative.
random 随机数
random.random()
#随机浮点型 区间在[0, 1)之间
import random
print(random.random())
random.randint()
#随机一个整数,范围在[1, 9]之间
import random
print(random.randint(1, 9))
help(random.randint)
#randint(a, b) method of random.Random instance
# Return random integer in range [a, b], including both end points.
random.randrange()
#在随机范围内去随机数 (1, 9)
import random
print(random.randrange(1, 9))
help(random.randrange)
#randrange(start, stop=None, step=1, _int=<class 'int'>) method of random.Random instance
# Choose a random item from range(start, stop[, step]).
#
# This fixes the problem with randint() which includes the
# endpoint; in Python this is usually not what you want.
random.choice()
#从非空序列中选择一个随机元素
import random
print(random.choice([1,2,3,4,5]))
help(random.choice)
#choice(seq) method of random.Random instance
# Choose a random element from a non-empty sequence.
random.uniform(x, y)
#随机浮点型 范围在[1, 9]
import random
print(random.uniform(1, 9))
help(random.uniform)
#uniform(a, b) method of random.Random instance
# Get a random number in the range [a, b) or [a, b] depending on rounding.
random.shuffle(lst)
#将序列中的所有元素随机排序,在原序列上修改,返回None
import random
a = [1,2,3,4,5,6,7]
random.shuffle(a)
print(a)
String (字符串)
字符串内涵
用引号括起来,单引号( 撇号)‘string' 或双引号"string",两者可以相互包含,使用灵活。
print('"sunck is a good man!"') # "sunck is a good man!"
字符串的独特表示
三引号 ‘’‘ ’‘’ 或“”“ ”“” 。特点 ①可跨多行,多用来对py文件的解释说明。②可包含单/双引号
原始字符串 ,r' ' 如果在字符串前面加r字符,则表示让这个字符串里面的内容失去转义的意义
"""
“多用来的Pyhton文件的功能的解释和说明”
“可以跨多行”
‘\n 为换行符’
"""
print('"sunck is a \ngood man!"') # "sunck is a
# good man!"
print(r'"sunck is a \ngood man!"') # "sunck is a \ngood man!"
字符串 有不可变性 ,指向永远不变。
类型转换
list(str)
str = "good man"
print(list(str)) # ['g', 'o', 'o', 'd', ' ', 'm', 'a', 'n']
tuple(str)
str = "good man"
print(tuple(str)) # ('g', 'o', 'o', 'd', ' ', 'm', 'a', 'n')
内建函数
标准列表的操作都适用于字符串
常用操作
大小写
str.title() 每个单词首字母大写
str = "sunck is a good man"
print(str.title()) #Sunck Is A Good Man
str.lower(); str.upper() 全部单词都转为小写;全部单词都转为大写
str = "suNck IS a gooD man"
print(str.lower()) # sunck is a good man
print(str.upper()) # SUNCK IS A GOOD MAN
str.swapcase() 大小写反转
str = "suNck IS a gooD man"
print(str.swapcase()) # SUnCK is A GOOd MAN
str.capitalize() 首字母大写取余都小写
str = "suNck IS a gooD man"
print(str.capitalize()) # Sunck is a good man
合并/拼接
① +号 ② % 或 join()
str1 = "suNck IS a gooD man"
str2 = "SUNCK is a nice man"
str3 = "Superman"
Tuple = ("sunck", "is", "a", "superman")
print(str1 + str2) # suNck IS a gooD manSUNCK is a nice man
print("sunck is a %s " % (str3)) # sunck is a Superman
print("-".join(Tuple)) # sunck-is-a-superman 以-连接元组中的元素
print(" ".join(Tuple)) # ssunck is a superman 以空格连接元组中的元素
空白
概述:任何非打印字符,如空格,制表符和换行符
添加空白 \t (制表符), \n(换行符)
print("sunck\tis\ta\ngood\tman")
#sunck is a
#good man
删除空白 strip/lsrip/rstrip()
#删除字符串两边/左边/右边指定的字符,默认为空。为空时这删除字符串中指定位置的空白字符
str1 = " sunck is a good man "
str2 = "0000sunck ia a good man0000"
print(str1.strip()) #sunck ia a good man
print(str2.strip("0")) #sunck ia a good man
print(str1.lstrip())
print(str2.lstrip("0")) #sunck ia a good man0000
print(str1.rstrip())
print(str2.rstrip("0")) #0000sunck ia a good man
更改显示
宽度
str.center() 使字符串居中,可指定宽度和填充的字符(默认为空格)
str1 = "good"
print(str1.center(10, "*")) #***good***
help(str.center)
#S.center(width[, fillchar]) -> str
# Return S centered in a string of length width. Padding is
# done using the specified fill character (default is a space)
str.ljust(); str.rjust() 左对齐;右对齐
#返回一个指定宽度,原字符串左/右对齐的新字符串,默认填充字符为空格。如果指定的长度小于原字符串的长度则返回原字符串。
str1 = "good"
print(str1.ljust(10)) #good
print(str1.ljust(10, "*")) #good******
print(str1.rjust(10)) # good
print(str1.rjust(10, "*")) #******good
help(str.ljust)
help(str.rjust)
#S.ljust(width[, fillchar]) -> str
#
# Return S left-justified in a Unicode string of length width. Padding is
# done using the specified fill character (default is a space).
#S.rjust(width[, fillchar]) -> str
#
# Return S right-justified in a string of length width. Padding is
# done using the specified fill character (default is a space).
str.zfill()
#返回指定长度的字符串,原字符串右对齐,前面填充0。
str1 = "good"
print(str1.zfill(10)) #000000good
help(str.zfill)
#S.zfill(width) -> str
# Pad a numeric string S with zeros on the left, to fill a field
# of the specified width. The string S is never truncated.
编码
str.encode(); str.decode()
#str.encode(), 以 encoding 指定的编码格式编码字符串。
#str.decode(), 以 encoding 指定的编码格式解码字符串。
#一般以何种方式编码就以何种方式解码
str1 = "sunck ia a good man 凯哥"
str1 = str1.encode(encoding="utf-8")
print("Encode string:%s" % str1) #Encode string:b'sunck ia a good man \xe5\x87\xaf\xe5\x93\xa5'
print("decode string:%s" % (str1.decode(encoding="utf-8"))) #decode string:sunck ia a good man 凯哥
help(str.encode)
#S.encode(encoding='utf-8', errors='strict') -> bytes
#
# Encode S using the codec registered for encoding. Default encoding
# is 'utf-8'. errors may be given to set a different error
# handling scheme. Default is 'strict' meaning that encoding errors raise
# a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
# 'xmlcharrefreplace' as well as any other name registered with
# codecs.register_error that can handle UnicodeEncodeErrors.
检查/查找
Python 非空值转换
转载文章标签 Python 非空值转换 Python标准数据类型 数值 随机数 String 文章分类 Python 后端开发
-
Java使用JsonNode判断实体类非空
使用jsonNode去判断实体类的参数是否为空
jsonnode java hutool -
Python PDF转换为图片
PDF 另存为图片,pdf python操作,python 转换PDF, Python PDF图片
Python 转换 图片 pdf -
python获取非空对象代码 python非空值
一、Python对象要理解这个,首先要理解Python对象:python对象具有三个特性:身份、类型、值。
python获取非空对象代码 python Python mysql -
AS kotlin查看转成的java
一、数据类(data class) 在介绍数据类之前,我们先来看看这样一段代码:class Student(val name: String, var age: Int) fun main(args: Array<String>) { val student = Student("David", 12) println(student) }
AS kotlin查看转成的java Android 移动开发 Kotlin Data Class