变量可以存储不同类型的数据,并且不同类型可以执行不同操作
python有以下数据类型:
数据类型 | 关键字 |
---|---|
文本类型 | str |
数值类型 | int,float ,complex |
序列类型 | list ,tuple ,range |
映射类型 | dict |
集合类型 | set ,frozenset |
布尔类型 | bool |
二进制类型 | bytes ,bytearray,memotyview |
使用type()可以获取任何对象的数据类型
x=100
print(type(x))
在python中,当为变量赋值时,会设置数据类型
变量 | 数据类型 |
---|---|
x=“hello” | str |
x=1 | int |
x=1.2 | float |
x=2j | complex |
x=[“apple”,“orange”,“peach”] | list |
x=(“apple”,“orange”,“peach”) | tuple |
x=range(5) | range |
x={“apple”,“orange”,“peach”:100} | dict |
x={“apple”,“orange”,“peach”} | set |
x=frozenset({“apple”,“orange”,“peach”}) | frozenset |
x=true | bool |
x=b"hello" | bytes |
x=bytearray(5) | bytearray |
x=memoryview(bytes(50)) | memoryview |
如果需要制定数据类型,可以使用以下构造函数
数据类型 | |
---|---|
x=str(“hello”) | str |
x=list((“apple”,“orange”,“peach”)) | list |
x=tuple((“apple”,“orange”,“peach”)) | tuple |
x=range(5) | range |
x=dict(name=“sam”,age=8) | dict |
x=set((“apple”,“orange”,“peach”)) | set |
x=frozenset((“apple”,“orange”,“peach”)) | frozenset |
x=bool(5) | bool |
x=bytes(5) | bytes |
x=bytearray(5) | bytearray |
x=memoryview(5) | memoryview |
int:整数或者完整的数字,正数或者是负数,没有小数,长度不限
x=1
x=54214543212456214546144556
z=-459656565
print(type(x))
print(type(y))
print(type(z))
float:浮点数,包含小数的正数或者负数,浮点数也可以是带“e”的科学数字,表示10的幂
x=3.45
y=2.0
z=-62.7
m=12e4
n=30e8
d=-42.1e100
print(type(x))
print(type(y))
print(type(z))
print(type(m))
print(type(n))
print(type(d))
complex:复数,负数用j作为虚部编写
x=2+3j
y=7j
z=-3j
print(type(x))
print(type(y))
print(type(z))
可以使用int()
,float()
,complex()
从一种类型转化为另一种类型
x = 10 # int
y = 6.3 # float
z = 1j # complex
# 把整数转换为浮点数
a = float(x)
# 把浮点数转换为整数
b = int(y)
# 把整数转换为复数:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
无法将复数转化为其他数字类型
python没有random()
函数来创建随机数,但是有random
这个内置模块来生成随机数
例如:导入random
模块,并显示到1到9之间的随机数
import random
print(random.randrange(1,10))
指定变量类型,可以通过casting来完成
python是一门面向对象语言,因此使用类来定义数据类型,包括其原始类型
因此,使用构造函数完成在python中的转换
int()
整数字面量,浮点字面量构造函数(通过对数进行下舍入),或者用表示完整数字的字符串字面量
float()
用整数字面量,浮点字面量,或字符串字面量构造浮点数(提供表示浮点数或者证书的字符串)
str()
用各种数据类型构造字符串,包括字符串,整数字面量和浮点字面量
#整数
x=int(1) #x将是1
y=int(2.5) #y将是2
z=int("3") #z将是3
#浮点数
x=float(1) #x将是1.0
y=float(2.5) #y将是2.5
x=float("3") #z将是3.0
w=float("4.6") #w将是4.6
#字符串
x=str("S2") #x将是‘S2’
y=str(3) #y将是‘3’
z=str(4.0) #z将是‘4.0’
'hello'
等同于"hello"
用字符串向变量赋值,a="hello"
多行字符串使用'''
或"""
将多行字符串赋值给字符串
a="""orange is
my favourite food"""
#或者
a='''orange id my
favourite food'''
#换行符插入在代码中的相同位置
python中的字符串是表示unicode字符的字节数组
但是python没有字符数据类型,单个字符就是长度为1的字符串
[]
可用于访问字符串中的元素
#获取位置1处的字符,注意不要越界访问
a='hello,world!'
print(a[1])
可以使用裁切语法返回一定范围的字符
指定开始索引和结束索引,以冒号分隔,以返回字符串的一部分
#获取从位置2到位置6(不包括6)的字符
b="orange and apple"
print(b[2:6])
使用负索引从字符串末尾开始切片
#获取从位置7到位置2的字符,从字符串末尾开始计数
b="orange and apple"
print(b[-7:-3])
获取字符串的长度,使用len()
函数
len()
函数返回字符串的长度
a='orange'
print(len(a))
python有一组可用于字符串的内置方法
strip()方法删除开头和结尾的空白字符
a=' orange '
print(a.strip())
lower()返回小写的字符串
upper()方法返回大写的字符串
replace()用另一段字符串来替换字符串
a="orange and apple"
print(a.replace("apple","peach"))
split()方法在找到分隔符的实例将字符串拆分为子字符串
a="orange,apple,peach"
print(a.split(,))
如果需要检查字符串中是否存在短语或者字符,我们可以使用in或者not in关键字
#检查文本中是否存在短语ang
txt="do you like orange"
x="ang" in txt
print(x)
如果需要串联两个字符串可以直接用+
运算符
a="apple"
b="orange"
c=a+b
print(c)
#在它们之间添加一个空格
d=a+" "+b
print(d)
不能将字符串和数字+
组合在一起
可以使用format()
方法组合字符串和数字
format()
方法接受传递的参数,格式化它们,并将它们放在占位符{}
所在的字符串中
format()
方法接受不限数量的参数
a=3
b=4
c=5
txt="i want {} pieces of item {} for {} dollars."
print(txt.format(a,b,c))
也可以使用索引号{0}
来确保参数被放在正确的占位符中
a=2
b=2.5
txt="i want to pay {1} dollor for {0} apples"
字符串方法
所有字符串方法都返回新值,它们不会更改原始字符