基本数据类型(一)

基本数据类型

数字   int(所有功能都放在int里)# 数字比作猎人

    - 将字符串转换为数字:

1 # 将字符串转换为数字
2 a = "123"
3 print(type(a), a)
4 b = int(a)
5 print(type(b), b)
6 # >>>执行结果:
7 # <class 'str'> 123
8 # <class 'int'> 123
将字符串转换为数字
1 num = "001a"  # a的十六进制为10
2 v = int(num, base=16)  # 转换为十六进制
3 print(v)
4 # >>>执行结果:
5 # 26
转换为十六进制数

    - bit_lenght:前数字的二进制,至少用n位表示

 

 1 age = 5
 2 # 1 --1
 3 # 2 --10
 4 # 3 --11
 5 # 4 --100
 6 # 5 --101
 7 # ...
 8 r = age.bit_length()
 9 print(r)
10 # >>>执行结果:
11 # 3
bit_length()方法:数字的二进制,至少用n位表示

字符串  str # 字符串比作女巫

  一、基本功能:

 *******7个最重要:

 1 # 1,join()方法:将字符串中的每一个元素按照指定分隔符进行拼接***
 2 test1 = "字符串比作女巫"
 3 print(test1)
 4 t = " "
 5 v1 = t.join(test1)
 6 v2 = " ".join(test1)
 7 v3 = "_".join(test1)  # 其他数据类型也会用到的功能
 8 print(v1)
 9 print(v2)
10 print(v3)
11 # >>>执行结果:
12 # 字符串比作女巫
13 # 字 符 串 比 作 女 巫
14 # 字 符 串 比 作 女 巫
15 # 字_符_串_比_作_女_巫
join()方法***:将元素按照指定分隔符拼接
 1 # 2.partition()方法、split()方法、splitlines()方法:分割字符串***
 2 test1 = "understandingunder"
 3 # partition()方法:包含分割的元素,规定分成3部分
 4 v1 = test1.partition('d')
 5 print(v1)
 6 v2 = test1.rpartition('d')  # 从右边开始分割
 7 print(v2)
 8 # >>>执行结果:
 9 # ('un', 'd', 'erstandingunder')
10 # ('understandingun', 'd', 'er')
11 # split()方法:不包含分割的元素,可以设定分为任意部分
12 v3 = test1.split('d', 3)
13 print(v3)
14 v4 = test1.rsplit('d', 3)  # 从右边开始分割
15 print(v4)
16 # >>>执行结果:
17 # ['un', 'erstan', 'ingun', 'er']
18 # ['un', 'erstan', 'ingun', 'er']
19 # =========================================
20 # 正则表达式:是否想要分割的元素
21 # =========================================
22 # splitlines()方法:分割,只能根据换行符,true,false:是否保留换行符
23 test = "sdfijhd\nbidhihci\ndhcbujhc"
24 v1 = test.splitlines(False)  # False:不保留\n
25 v2 = test.splitlines(True)  # True:保留\n
26 print(v1)
27 print(v2)
28 # >>>执行结果:
29 # ['sdfijhd', 'bidhihci', 'dhcbujhc']
30 # ['sdfijhd\n', 'bidhihci\n', 'dhcbujhc']
partition()方法、split()方法***、splitlines()方法:分割字符串
 1 # 3.find()方法、index()方法:从开始往后找,找到第一个之后,获取其位置***
 2 # find()方法
 3 test = "abcdeabfcd"
 4 v1 = test.find("cd", 2, 8)
 5 print(v1)
 6 # 没有找到返回-1,(为开区间)
 7 v2 = test.find("fc", 3, 8)
 8 print(v2)
 9 # >>>执行结果:
10 # 2
11 # -1
12 # ==========================
13 # index()方法:找不到会报错
14 test = "python"
15 v1 = test.index('p')
16 v2 = test.index('o')
17 print(v1)
18 print(v2)
19 # >>>执行结果:
20 # 0
21 # 4
find()方法***、index()方法:获取字符串位置
 1 # 4.strip()方法:移除指定字符串,有限最多匹配***
 2 test1 = "Python"
 3 # lstrip()方法:移除从左边匹配的字符串
 4 v1 = test1.lstrip('Py')
 5 # rstrip()方法:移除从右边匹配的字符串
 6 v2 = test1.rstrip('3ayahaone')
 7 # strip()方法:移除左或右匹配的字符串
 8 v3 = test1.strip('Py')
 9 print(v1)
10 print(v2)
11 print(v3)
12 # 移除符合
13 test2 = "\tpython\n"
14 v4 = test2.lstrip()
15 v5 = test2.rstrip()
16 print(v4)
17 print(v5)
18 # >>>执行结果:
19 # thon
20 # Pyt
21 # thon
22 # python
23 #
24 #     python
strip()方法***:移除指定字符串
 1 # 5.islower()方法、lower()方法、isupper()方法、upper()方法:判断是否全部为大小写和转换为大小写***
 2 test = "Python"
 3 # islower()方法:判断是否全部为小写
 4 v1 = test.islower()
 5 # lower()方法:转换为小写
 6 v2 = test.lower()
 7 print(v1, v2)
 8 # isupper()方法:判断是否全部为大写
 9 v3 = test.isupper()
10 # upper()方法:转换为大写
11 v4 = test.upper()
12 print(v3, v4)
13 # >>>执行结果:
14 # False python
15 # False PYTHON
lower()方法***upper()***方法:判断、转换为大小写
1 # 6.replace()方法:替换***
2 test = "pythonpythonpython"
3 v1 = test.replace("on", "abab")
4 print(v1)
5 v2 = test.replace("on", "abab", 2)
6 print(v2)
7 # >>>执行结果:
8 # pythababpythababpythabab
9 # pythababpythababpython
replace()方法***:替换

>>>

1 # 7.capitalize()方法:首字母大写
2 test = "python"
3 v = test.capitalize()
4 print(v)
5 # >>>执行结果:
6 # Python
capitalize()方法:首字母大写
1 # 8.swapcase()方法:大小写转换
2 test = "Python"
3 v = test.swapcase()
4 print(v)
5 # >>>执行结果:
6 # pYTHON
swapcase()方法:大小写转换
 1 # 9.casefold()方法与lower()方法:让所有变小写
 2 # 区别:casefold()还可以使很多未知的字符相应变小写
 3 test = "PYTHON"
 4 v1 = test.casefold()
 5 print(v1)
 6 v2 = test.lower()
 7 print(v2)
 8 # >>>执行结果:
 9 # python
10 # python
casefold()方法与lower()方法:让所有变小写
 1 # 10.center()方法、ljust()方法、rjust()方法、zfill()方法:设置宽度,并将内容放在指定位置
 2 # 20 代指总长度
 3 # * 空白未知填充,一个字符,可有可无
 4 # center()方法:设置宽度,并将内容居中
 5 test = "abc"
 6 v1 = test.center(20, "*")
 7 v2 = test.center(10, "")
 8 v3 = test.center(10)
 9 print(v1)
10 print(v2)
11 print(v3)
12 # >>>执行结果:
13 # ********abc*********
14 # 中中中abc中中中中
15 #    abc
16 # =================================
17 # ljust()方法:设置宽度,并将内容居左
18 test = "python"
19 v1 = test.ljust(10, "*")
20 print(v1)
21 # rjust()方法:设置宽度,并将内容居右
22 v2 = test.rjust(10, "*")
23 print(v2)
24 # zfill()方法:设置宽度,并将空白部分填充为0
25 v3 = test.zfill(10)
26 print(v3)
27 # >>>执行结果:
28 # python****
29 # ****python
30 # 0000python
center()方法、ljust()方法、rjust()方法、zfill()方法:设置宽度,位置
1 # 11.count()方法:在字符串中寻找子序列出现的次数
2 test = "abcdefacdbde"
3 v1 = test.count("cd")
4 print(v1)
5 v2 = test.count("cd", 5, 10)
6 print(v2)
7 # >>>执行结果:
8 # 2
9 # 1
count()方法:在字符串中寻找子序列出现的次数
1 # 12.endswith()方法与startswith()方法:以什么结尾,以什么开始
2 test = "python"
3 v1 = test.endswith("on")
4 v2 = test.startswith("pt")
5 print(v1)
6 print(v2)
7 # >>>执行结果:
8 # True
9 # False
endswith()方法与startswith()方法:以什么结尾,以什么开始
 1 # 13.expandtabs()方法:把字符串中的tab符号('\t')转为空格,默认为空格数是8,断句为括号里的数
 2 test = "12345678\t9"
 3 v1 = test.expandtabs()  # 默认为8,\t前有8位,则\t为8位(空格)
 4 v2 = test.expandtabs(10)  # 给定为10,\t前有8位,则\t为2位(空格)
 5 print(test, len(test))
 6 print(v1, len(v1))
 7 print(v2, len(v2))
 8 # >>>执行结果:
 9 # 12345678    9 10
10 # 12345678        9 17
11 # 12345678  9 11
12 # ===============================
13 # 可用于制表
14 test2 = "username\temail\tpassword\npython\tpy@qq.com\t123\npython\tpy@qq.com\t123\npython\tpy@qq.com\t123\n"
15 v2 = test2.expandtabs(20)
16 print(v2)
17 # >>>执行结果:
18 # username            email               password
19 # python              py@qq.com           123
20 # python              py@qq.com           123
21 # python              py@qq.com           123
expandtabs()方法:把字符串中的tab符号('\t')转为空格
 1 # 14.format()方法、format_map()方法:格式化,将一个字符串中的占位符替换为指定的值
 2 # format()方法
 3 test1 = 'i am {name}, age {a}'
 4 print(test1)
 5 v1 = test1.format(name='python', a=19)
 6 print(v1)
 7 test2 = 'i am {0}, age {1}'
 8 print(test2)
 9 v2 = test2.format('python', 19)
10 print(v2)
11 # >>>执行结果:
12 # i am {name}, age {a}
13 # i am python, age 19
14 # i am {0}, age {1}
15 # i am python, age 19
16 # format_map()方法:传入的值{'name':'python','a':19}
17 test = 'i am {name}, age {a}'
18 v = test.format_map({"name": "python", "a": 19})
19 print(v)
20 # >>>执行结果:
21 # i am python, age 19
format()方法、format_map()方法:格式化,将一个字符串中的占位符替换为指定的值
 1 # 15.isalnum()方法:判断字符串中是否只包含字母和数字
 2 test1 = "Abc123"
 3 v1 = test1.isalnum()
 4 print(v1)
 5 test2 = "-12ab"
 6 v2 = test2.isalnum()
 7 print(v2)
 8 # >>>执行结果:
 9 # True
10 # False
isalnum()方法:判断字符串中是否只包含字母和数字
1 # 16.isalpha()方法:判断是否是字母、汉字
2 test1 = "学习Python"
3 test2 = "python123_"
4 v1 = test1.isalpha()
5 v2 = test2.isalpha()
6 print(v1, v2)
7 # >>>执行结果:
8 # True False
isalpha()方法:判断是否是字母、汉字
 1 # 17.isdecimal()方法、isdigit()方法、isnumeric()方法:判断当前输入是否为数字
 2 test1 = "123"
 3 test2 = ""
 4 test3 = ""
 5 v1 = test1.isdecimal()  # 十进制数
 6 v2 = test1.isdigit()  # 特殊数字
 7 v3 = test1.isnumeric()  # 汉字数字
 8 v4 = test2.isdecimal()
 9 v5 = test2.isdigit()
10 v6 = test2.isnumeric()
11 v7 = test3.isdecimal()
12 v8 = test3.isdigit()
13 v9 = test3.isnumeric()
14 print(v1, v2, v3)
15 print(v4, v5, v6)
16 print(v7, v8, v9)
17 # >>>执行结果:
18 # True True True
19 # False True True
20 # False False True
isdecimal()方法、isdigit()方法、isnumeric()方法:判断当前输入是否为数字
 1 # 18.isidentifier()方法:判断是否存在字母,数字,下划线,标识符:def、class
 2 test1 = "def_abc123"
 3 test2 = "abc"
 4 test3 = "123"
 5 v1 = test1.isidentifier()
 6 v2 = test2.isidentifier()
 7 v3 = test3.isidentifier()  # 单独只有数字为false
 8 print(v1, v2, v3)
 9 # >>>执行结果:
10 # True True False
isidentifier()方法:判断是否存在字母,数字,下划线,标识符
 1 # 19.isprintable()方法:判断是否存在不可显示的字符
 2 # \t制表符
 3 # \n换行符
 4 test1 = "python\t123"
 5 test2 = "python123"
 6 v1 = test1.isprintable()
 7 v2 = test2.isprintable()
 8 print(v1, v2)
 9 # 执行结果:
10 # False True
isprintable()方法:判断是否存在不可显示的字符
1 # 20.isspace()方法:判断是否全部是空格
2 test1 = ""  # 空字符
3 test2 = "   "
4 v1 = test1.isspace()
5 v2 = test2.isspace()
6 print(v1, v2)
7 # >>>执行结果:
8 # False True
isspace()方法:判断是否全部是空格
 1 # 21.istitle()方法、title()方法:判断是否是标题(首字母都大写)和转换为标题
 2 test1 = "Python is good"
 3 v1 = test1.istitle()
 4 print(v1)  # 判断是否是标题
 5 v2 = test1.title()  # 转换为标题
 6 print(v2)
 7 v3 = v2.istitle()
 8 print(v3)
 9 # >>>执行结果:
10 # False
11 # Python Is Good
12 # True
istitle()方法、title()方法:判断是否是标题和转换为标题
1 # 22.maketrans()方法、translate()方法:转换
2 test = "fuhwojwixih;dsxugsx;mkasxishub;ahuehijc;432"
3 # maketrans()方法:对应关系
4 v = str.maketrans("aeiou", "12345")
5 # translate()方法:转换
6 new_test = test.translate(v)
7 print(new_test)
8 # >>>执行结果:
9 # f5hw4jw3x3h;dsx5gsx;mk1sx3sh5b;1h52h3jc;432
maketrans()方法、translate()方法:转换

 

  二、其他数据类型也会用到的功能

    1.len()

    2.for循环

      for 变量名 in 字符串:

        变量名

 

    3.索引,下标,获取字符串中的某一个字符

    4.切片

    (5.'_'.join('...'))

 

 1 test = "我爱学习"
 2 # 1.len():Python3中len()获取当前字符串中有几个字符组成
 3 index = 0
 4 while index < len(test):
 5     v = test[index]
 6     print(v)
 7     index += 1
 8 print("========")
 9 # 2.for循环
10 for a in test:
11     print(a)
12 # >>>执行结果:
13 #
14 #
15 #
16 #
17 # ========
18 #
19 #
20 #
21 #
22 
23 # 3.索引,下标,获取字符串中的某一个字符
24 v1 = test[3]
25 print(v1)
26 # 4.切片
27 v2 = test[0:2]  # 0=< <1 范围
28 print(v2)
29 v3 = test[0:-1]  # 到最后
30 print(v3)
31 # >>>执行结果:
32 #
33 # 我爱
34 # 我爱学
其他数据类型也会用到的功能

    6.range()

 1 # 帮助创建连续的数字,通过设置步长来指定不连续
 2 test = range(0, 100, 20)
 3 for item in test:
 4     print(item)
 5 # 执行结果:
 6 # 0
 7 # 20
 8 # 40
 9 # 60
10 # 80
帮助创建数字
 1 # 将文字对应的索引打印出来:
 2 test = input()
 3 print(test)
 4 l = len(test)
 5 print(l)
 6 r = range(0, l)
 7 for item in r:
 8     print(item, test[item])
 9 # >>>执行结果:
10 # abc
11 # abc
12 # 3
13 # 0 a
14 # 1 b
15 # 2 c
16 
17 # ========================
18 
19 test = input(">>>")  # 输入abc
20 for item in range(0, len(test)):
21     print(item, test[item])
22 # >>>执行结果:
23 # >>>abc
24 # 0 a
25 # 1 b
26 # 2 c
将文字对应的索引打印出来

  

  三、字符串一旦创建,不可修改

  一旦修改或拼接,都会造成重新生成字符串

1 name = "abc"
2 age = "18"
3 info = name + age
4 print(info)
5 # >>>执行结果:
6 # abc18 新生成的
新生成的字符串

 


 

 

列表  list(类)

  一、调用类中的功能

    1.列表格式

      用中括号括起来,“ , ”用于分割每个元素

    2.列表中可以嵌套任何类型

      列表中的元素可以是数字,字符串,列表,布尔值......所有的都能放进去

      “集合”,内部放置任何东西

    3.索引取值

    4.切片,切片结果也是列表

 1 li = [1, 12, 9, "age", ["小明", ["a", 10], "小红"], "python", True]
 2 # 元素个数
 3 print(len(li))
 4 # 索引取值
 5 print(li[3])
 6 # 切片取值
 7 print(li[3:5])
 8 print(li[3:-1])
 9 # >>>执行结果:
10 # 7
11 # age
12 # ['age', ['小明', ['a', 10], '小红']]
13 # ['age', ['小明', ['a', 10], '小红'], 'python']
元素个数、索引、切片

    5.for循环、while循环

 1 # for循环
 2 li = [1, 12, 9, "age", ["小明", ["a", 10], "小红"], "python", True]
 3 for item in li:
 4     print(item)
 5 # >>>执行结果:
 6 # 1
 7 # 12
 8 # 9
 9 # age
10 # ['小明', ['a', 10], '小红']
11 # python
12 # True
for循环

    6.列表元素可以通过索引被修改和删除

 1 # 通过索引修改和删除列表元素
 2 li = [1, 12, 9, "age", ["小明", ["a", 10], "小红"], "python", True]
 3 li[1] = 120  #修改
 4 print(li)
 5 li[2] = [11, 22, 33, 44]
 6 print(li)
 7 del li[2]  # 删除
 8 print(li)
 9 # >>>执行结果:
10 # [1, 120, 9, 'age', ['小明', ['a', 10], '小红'], 'python', True]
11 # [1, 120, [11, 22, 33, 44], 'age', ['小明', ['a', 10], '小红'], 'python', True]
12 # [1, 120, 'age', ['小明', ['a', 10], '小红'], 'python', True]
通过索引修改和删除列表元素

    7.列表元素可以通过切片被修改和删除

1 # 通过切片修改和删除列表元素
2 li = [1, 12, 9, "age", ["小明", ["a", 10], "小红"], "python", True]
3 li[1:3] = [120, 90]  # 修改
4 print(li)
5 del li[2:6]  # 删除
6 print(li)
7 # >>>执行结果:
8 # [1, 120, 90, 'age', ['小明', ['a', 10], '小红'], 'python', True]
9 # [1, 120, True]
通过切片修改和删除列表元素

    8.in 操作(列表中的元素)

 1 # in 操作
 2 li = [1, 12, 9, "age", ["小明", ["a", 10], "小红"], "python", True]
 3 v1 = 12 in li
 4 print(v1)
 5 v2 = 120 in li
 6 print(v2)
 7 v3 = "小明" in li  # 不是在列表中
 8 print(v3)
 9 # >>>执行结果:
10 # True
11 # False
12 # False
in 操作

     9.嵌套索引取值

1 # 嵌套索引取值
2 li = [1, 12, 9, "age", ["小明", ["19", 10], "小红"], "python", True]
3 print(li[4][1][0])
4 print(li[4][1][0][1])
5 # >>>执行结果:
6 # 19
7 # 9
嵌套索引取值

    10.转换

 1 # 字符串转换列表  li = list("..."),内部使用for循环
 2 s = "hbcosjdbcushdb"
 3 # a = "123"
 4 # print(int(a))  # 字符串转数字
 5 # a = 123
 6 # print(str(a))  # 数字转字符串
 7 new_li = list(s)
 8 print(new_li)
 9 # >>>执行结果:
10 # ['h', 'b', 'c', 'o', 's', 'j', 'd', 'b', 'c', 'u', 's', 'h', 'd', 'b']
字符串转换列表
 1 # 列表转换成字符串,需要自己写for循环一个一个处理:既有数字又有字符串
 2 li = [11, 22, 33, "123", "alex"]
 3 r = str(li)  # '[11,22,33,"123","alex"]'
 4 print(r)
 5 
 6 s = " "  # 空字符
 7 for i in li:
 8     # s = s + i  # 数字和字符串不能相加(报错)
 9     s = s + str(i)
10 print(s)
11 # >>>执行结果:
12 # [11, 22, 33, '123', 'alex']
13 #  112233123alex
14 
15 # 列表中的元素只有字符串:直接使用字符串join方法
16 li = ["123", "alex"]
17 v = "_".join(li)
18 print(v)
19 # >>>执行结果:
20 # 123_alex
列表转换成字符串

 

  二、list类中提供的方法(eg:li对象调用append方法)

    li = [1, 12, 9, "age", ["小明", ["19", 10], "小红"], "python", True]

    list 类:li为list类的一个对象

     1.在原来值最后追加

 1 # 1.在原来值最后追加
 2 li = [11, 22, 33, 44]
 3 
 4 # v = li.append(55)
 5 # print(v)
 6 # print(li)
 7 # >>>执行结果:
 8 # None  # v为空值
 9 # [11, 22, 33, 44, 55]
10 
11 # 可以不用一个值接收,在原值后面直接追加,如下
12 li.append(55)  # li.append("python")、li.append([2,3])
13 print(li)
14 # >>>执行结果:
15 # [11, 22, 33, 44, 55]
append()方法:在原来值最后追加

     2.清空列表

posted @ 2021-09-01 20:08  沐銘梓  阅读(83)  评论(0)    收藏  举报