引入:变量的是用来反映状态以及状态变化的,毫无疑问针对不同的状态就应该用不同类型的数据去标识。
1. 数字类型
- python中你可以对数字就行下表中的计算
运算符 | 描述 | 示例 | 运算结果 |
+ | 加法 | 5 + 5 | 10 |
- | 减法 | 5 - 1 | 4 |
* | 乘法 | 4 * 3 | 12 |
/ | 除法(浮点除法) | 9 / 3 | 3.0 |
// | 整除 | 9 // 2 | 4 |
** | 幂 | 3 ** 2 | 9 |
% | 模(求余) | 9 // 2 | 1 |
整型 int简单来说就是整数
age = 18 # age=int(18)
print(id(age))
print(type(age))
print(age)
4530100848
<class 'int'>
18
浮点型 float
简单来说就是小数
salary = 2.1 # salary=float(2.1)
print(id(salary))
print(type(salary))
print(salary)
4569240656
<class 'float'>
2.1
优先级
-
a + b * 3 会产生什么结果?
这时候我们就需要参照优先级表了,但实际应用上我们一般不会查表,因为我们可以用()解决。 比如:
a + ( b * 3 )
基数
在python中默认以十进制数为基数,我们可以在数字前加前缀,显示指定使用其他基数。
- 0b 或 0B 表示二进制
- 0o 或 0O 表示8进制
- 0x 或 0X 表示16进制
>>> 0b10
2
>>> 0o10
8
>>> 0x10
16
类型转换
我们可以用int( )将其他数据类型转换为整型,并舍去小数点
# 布尔型
>>>int(True)
1
>>>int(False)
0
# 浮点型
>>>int(2.6)
2
# 仅包含数字和正负号的字符串
>>>int('10')
10
>>>int('-234')
-234
# 注意 int() 无法接受包含小数点 或 指数的字符串
>>> int('23.3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '23.3'
一个int型有多大
Python2 中 一个int型包含32位,long型 64位
Python3 中int型 无穷大 ,long不复存在
2. 字符串 str
- 作用:表示名字、爱好
- 定义:字符串相当于一根羊肉串。而字符串就是一串被串起来的字符,在单引号、双引号或三引号内包裹的一串字符。需要注意的是:三引号内的字符可以换行,而单双引号内的字符不行
创建字符串很简单,只要为变量分配一个值即可。例如:
var1 = 'Hello World!'
var2 = "Rison"
#那单引号、双引号、多引号有什么区别呢? 让我大声告诉你,单双引号木有任何区别,只有下面这种情况 你需要考虑单双的配合
msg = "My name is Rison , I'm 18 years old!"
#多引号什么作用呢?作用就是多行字符串必须用多引号
msg = '''
床前明月光,
疑是地上霜。
'''
- View Code
使用str()进行类型转换
>>> str(34)
'34'
>>> str(True)
'True'
使用 \ 转义
换行符 \n
>>> print('ma\nn')
ma
n
转义符(制表符) \t
>>> print('ma\tn')
ma n
用 \' 和 \" 表示单双引号 尤其是当该字符串由相同类型的引号包裹时
>>> print('I\'m a man')
I'm a man
同理如果需要输出\ 输入\\即可
>>> print('man\\')
man\
使用 + 拼接
>>> a = 'Hello'
>>> b = 'World'
>>> print(a + b)
HelloWorld
使用 * 复制
>>> a = 'helloworld'
>>> print(a * 10)
helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworld
# 美观一点 在后面加个空格
>>> a = 'helloworld '
>>> print(a * 10)
helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld
使用 [ ] 提取字符
>>> a = 'helloworld'
# 在字符串后面用 [ ] 中括号内加偏移量即可取到对应的值
>>> a[0] # 第一个字符的偏移量为0 第二个为1 以此类推
'h'
>>> a[1]
'e'
>>> a[-1] # 倒数第一个从-1算起
'd'
>>> a[-2]
'l'
>>> a[10] # 超过报错 因为从0开始,最大是9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
使用 [ start : end : step ] 分片
分片操作(slic)可以从一个字符串中抽取子字符串(字符串的一部分)。我们使用一对方括号、起始偏移量 start、终止偏移量end以及可选的步长step来定义一个分片。其中些可以省略。分片得到的子串包含从 start开始到end之前的全部字符。
[:] 提取从开头到结尾的整个字符串
[start:] 从 start提取到结尾
[:end] 从开头提取到end
[start:end] 从 start提取到end-1
[start:end:step】] 从 start提取到end-1,每step个字符提取一个
与之前一样,偏移量从左至右从0开始,依次增加;从右至左从-1开始,依次减小。如果省略 start,分片会默认使用偏移量0(开头);如果省略end,分片会默认使用偏移量-1(结尾)。
>>> a = 'helloworld'
>>> a [:]
'helloworld'
>>> a[1:]
'elloworld'
>>> a[:-1]
'helloworl'
>>> a[0:-1] # 左闭右开
'helloworl'
>>> a[1:-2:2]
'elwr'
>>> a[-1:-4]
''
>>> a[-3:-1]
'rl'
>>> a[::-2]
'drwle'
使用 len( ) 获取长度
len可以计算字符串包含的字符数
>>> a = 'helloworld'
>>> len(a)
10
使用 split 分割
# split() 括号内什么都不写默认以空白字符分割 返回一个列表
>>> a = 'hello world hello Rison'
>>> a.split()
['hello', 'world', 'hello', 'Rison']
# split('l') 如你所视
>>> a.split('l')
['he', '', 'o wor', 'd he', '', 'o Rison']
使用 join 合并
与split相反 join将列表分解再组合成字符串
用法为 string.join(list)
>>> a = ['hello', 'world', 'hello', 'Rison']
>>> b = ','.join(a) # 调用方式很奇怪 表示用 , 将列表a中每个元素拼接起来
>>> b
'hello,world,hello,Rison'
大小写与对齐方式
strip() # 将字符串 . 结尾的全部删除
>>> msg = 'a duck goes into a bars...'
>>> msg.strip('.')
'a duck goes into a bars'
capitalize() # 让字符串首字母大写
>>> msg.capitalize()
'A duck goes into a bars...'
title() # 字符串中每个单词首字母大写
>>> msg.title()
'A Duck Goes Into A Bars...'
upper() # 所有字母大写
>>> msg.upper()
'A DUCK GOES INTO A BARS...'
lower() # 所有字母小写
>>> msg.lower()
'a duck goes into a bars...'
swapcase() # 所有字母大小写交换
>>> msg.swapcase()
'A DUCK GOES INTO A BARS...'
center() # 居中
>>> msg.center(34) # 在34个字符中居中 以空格填充
' a duck goes into a bars... '
ljust() # 左对齐 # 在34个字符中左对齐
>>> msg.ljust(34)
'a duck goes into a bars... '
rjust() # 右对齐 # 在34个字符中右对齐
>>> msg.rjust(34)
' a duck goes into a bars...'
使用replace()替换
replace() # c.replace(a,b) 就是把 c中出现的 a替换成 b
>>> msg = 'a duck goes into a bars...'
>>> msg.replace('a ','a famous')
'a famousduck goes into a famousbars...'
记得加空格!!!!!
>>> msg.replace('a', 'a famous')
'a famous duck goes into a famous ba famousrs...'
熟悉字符串
>>> poem = '''
... Whenever you need me, I'll be here。
...
... Whenever you're in trouble, I'm always near。
...
... Whenever you feel alone, and you think everyone has given up。。。
...
... Reach out for me, and I will give you my everlasting love。
... '''
>>> len(poem)
209
>>> poem[:23]
'\nWhenever you need me, '
startswith() # 以什么开头
>>> poem.startswith('whenever')
False
>>> poem.startswith('\n')
True
>>> poem.startswith('\nWhenever')
True
endswith()# 以什么结尾
endswith()
>>> poem.endswith('\n')
True
find() # 第一次出现xx的偏移量
>>> poem.find('in')
55
rfind() # 最后一次出现xx的偏移量
>>> poem.rfind('in')
199
count() # xx出现的次数
>>> poem.count('in')
3
3 布尔值 bool
- 作用:用于判断条件结果
- 如何定义:True、False,通常情况不会直接引用,需要使用逻辑运算得到结果。需要注意的是:Python中所有数据类型的值自带布尔值。如此多的数据类型中只需要记住只有0、None、空、False的布尔值为False,其余的为True。
#所有数据类型都自带布尔值
1、None,0,空(空字符串,空列表,空字典等)三种情况下布尔值为False
2、其余均为真
>>> a=3
>>> b=5
>>>
>>> a > b #不成立就是False,即假
False
>>>
>>> a < b #成立就是True, 即真
True