python基础学习 day1:基本数据类型
-
Number(数字)
-
String(字符串)
-
List(列表)
-
Tuple(元组)
-
Dictionary字典)
-
Set(集合)
其中:
-
可变数据(3个):列表、字典和集合
-
不可变数据(3个):数字、字符串、元组
1. Number(数字)
1.1 基本语法
-
Python3支持
int
、float
、bool
、complex
(复数)四种数字类型; -
Python3中整数类型只有
int
,表示为长整型,舍弃了Python2中的long
; -
使用内置的
type()
和isintance()
函数可以查询变量所指的对象类型。
1 class A(object):
2 pass
3 class B(A):
4 pass
5
6
7 a, b, c, d = 20, 5.5, True, 4+3j
8 # type
9 print(type(a), type(b), type(c), type(d))
10 # <class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
11 # isintance
12 print(isinstance(a, int))
13 # True
14 # type和isinstance区别
15 print(isinstance(A(), A)) # True
16 print(type(A()) == A) # True
17 print(isinstance(B(), A)) # True
18 print(type(B()) == A) # False
type()
不会认为子类是一种父类类型isinstance()
认为子类是一种父类类型
1.2 魔方方法
-
int
:将数字或者字符串按照指定进制转换成对应的整型数字
1 a = '123'
2 b = int(a)
3 print(type(a), a)
4 print(type(b), b)
1 bin = '11' 2 print(int(bin, base=2)) 3 print(int(bin, base=10))
-
bit_length
:指定数字的最小二进制表示位数
1 num = 10
2 print(num, '->', bin(num))
3 print(num, '->', num.bit_length())
2. String(字符串)
2.1 基本语法
-
以单引号或者双引号或者三引号括起来,特殊字符使用反斜杠
\
进行转义,或者使用r'I'm panda'
进行强制转换; -
支持切片等截取操作,索引从0开始,-1代表最后一个字符;
-
+
代表字符串连接符,*
代码赋值当前字符串; -
python中字符串不可被修改,向某个索引位赋值,如
str1[2] = 'a'
会报错。
2.2 魔法方法
-
str.capitalize()
:首字母大写 -
str.casefold()
:字符小写化 -
str.lower()
:英文字符小写化 -
str.center(len, char)
:将字符串以指定的一个字符填充至指定长度 -
str.count(str1)
:统计字符串str
中子序列str1
出现的次数 -
str.endswith(str1)
:判断字符串是否以子序列结束 -
str.startswith(str1)
:判断字符串是否以子序列开始
-
str.find(str1)
:统计子序列首次出现的位置,未找到时返回-1
-
str.format()
:格式化,将字符串中的占位符替换为指定值,多用于print
语句中 -
str.isalnum()
:判断字符串是否为数字或者字母 -
str.isalpha()
:判断字符串是否是字母 -
str.isdecimal
():判断字符串是否是十进制数字 -
str.isdigital()
:判断字符串是否是数字,不支持中文数字 -
str.isnumeric()
:判断字符串是否是数字,支持中文数字 -
str.expandtabs()
:将字符串中的制表符扩展到指定长度,多用于表格显示 -
str.join()
:将字符串以指定字符进行分割拼接 -
str.strip()
:移除字符串中指定字符及其子序列
1 print('hello'.capitalize())
2 print('HeLlo'.casefold())
3 print('α À Ⅰ Ⅱ Ⅲ Ⅵ'.casefold())
4 print('HelLo'.lower())
5 print('Hello'.center(10, '*'))
6 print('HellLo'.count('el'))
7 print('Hello'.startswith('Hel'))
8 print('Helloworld'.find('l'))
9 print('I am {name}, my age is {age}'.format(name='cup', age=99))
10 print('I am {0}, my age is {1}'.format('cup', 100))
11 print('Helloworld'.isalnum())
12 print('Hello world'.isalnum())
13 print("123".isdecimal(), "123".isdigit(), "123".isnumeric())
14 print("②".isdecimal(), "②".isdigit(), "②".isnumeric())
15 print("二".isdecimal(), "二".isdigit(), "二".isnumeric())
16 inf = "usrname\temail\tpassword\nliu\tliu@qq.com\t123\nliu\tliu@qq.com\t123\nliu\tliu@qq.com\t123"
17 print(inf.expandtabs(20))
18 print("_".join("hello world"))
19 print("xaexlexxa".rstrip("91exlexxa"))
20 '''
21 Hello
22 hello
23 α à ⅰ ⅱ ⅲ ⅵ
24 hello
25 **Hello***
26 1
27 True
28 2
29 I am cup, my age is 99
30 I am cup, my age is 100
31 True
32 False
33 True True True
34 False True True
35 False False True
36 usrname email password
37 liu liu@qq.com 123
38 liu liu@qq.com 123
39 liu liu@qq.com 123
40 h_e_l_l_o_ _w_o_r_l_d
41 '''
3. List(列表)
-
定义:列表是一个与位置有关联的任意类型的对象的有序集合。
-
语法:创建时以方括号
[]
进行定义,使用逗号,
分割不同元素,访问时使用[index]进行访问; -
支持切片操作;
-
+
代表连接符,*
代码赋值当前列表; -
python中列表元素可被修改
4. Tuple(元组)
-
与列表类似,但元组元素不可被修改;
-
使用圆括号
()
进行定义,使用逗号,
分割元素; -
a = (1,)
和b = (1)
:前者是tuple,后者是int型; -
支持切片操作;
注:字符串、列表和元组在Python中均属于序列(sequence),因此都支持索引和切片操作,以及对应的连接+
和赋值*
操作。
5. Set(集合)
-
语法:使用大括号
{}
或者set()
函数定义,创建空集合必须使用a = set()
; -
代表无序不重复元素序列;
-
支持差集
a-b
、并集a|b
、交集a&b
等运算;
1 a = {1, 2, 3, 3, 2, }
2 b = set((2, 4, 6, 3, 8, 6, 9, 9))
3 print(a, b)
4 print('a差b:', a - b) # a和b的差集
5 print('b差a:', b - a)
6 print('a并b:', a | b) # a和b的并集
7 print('a交b:', a & b) # a和b的交集
8 print(a ^ b) # a和b中不同时存在的元素
9 # 输出
10 '''
11 {1, 2, 3} {2, 3, 4, 6, 8, 9}
12 a差b: {1}
13 b差a: {8, 9, 4, 6}
14 a并b: {1, 2, 3, 4, 6, 8, 9}
15 a交b: {2, 3}
16 {1, 4, 6, 8, 9}
17 '''
6. Dictionary(字典)
-
无序对象集合,使用键来存储;
-
是一种映射类型,使用
{}
进行定义,是无序的键(key):值(value)
对集合 -
同一个字典中,键(key)必须是唯一的;
-
字典的三种创建方式:
1 # 1.直接创建 2 dict1 = {'name': 'panda', 'age': 13} 3 # 2.调用dict()函数,以表达式方式创建 4 dict2 = dict(school='cup', age=99) # 或者dict2 = dict([('school', 'cup'), ('age', 99)]) 5 # 3.使用zip合并列表创建 6 key = ['a', 'b', 'c'] 7 value = [1, 2, 3] 8 dict3 = dict(zip(key, value)) 9 print(dict1['name']) 10 print(dict2['school']) 11 print(dict3['a'])
7. 数据类型转换
函数 | 描述 |
---|---|
[int(x [,base])] | 将x转换为一个整数 |
[float(x)] | 将x转换到一个浮点数 |
[complex(real [,imag])] | 创建一个复数 |
[str(x)] | 将对象 x 转换为字符串 |
[repr(x)] | 将对象 x 转换为表达式字符串 |
[eval(str)] | 用来计算在字符串中的有效Python表达式,并返回一个对象 |
[tuple(s)] | 将序列 s 转换为一个元组 |
[list(s)] | 将序列 s 转换为一个列表 |
[set(s)] | 转换为可变集合 |
[dict(d)] | 创建一个字典。d 必须是一个序列 (key,value)元组。 |
[frozenset(s)] | 转换为不可变集合 |
[chr(x)] | 将一个整数转换为一个字符 |
[ord(x)] | 将一个字符转换为它的整数值 |
[hex(x)] | 将一个整数转换为一个十六进制字符串 |
[oct(x)] | 将一个整数转换为一个八进制字符串 |