python的标准数据类型简介

python的标准数据类型有5种:

数字-numbers

字符串-str

列表-list

元组-tuple

字典-dict

下面来个概览先大概了解一下,后面在细说

1.数字-numbers

  数字类型就是我们日常生活中所说的数字,python中的数字不分是整数还是小数,正数还输负数,超长数,python中的int足够强大到可以表示任意的数

num1 = 10
num2 = -10
num3 = 1.1
num4 = -1.1
num5 = 222222222222222222222222222222222222222222222222222222
num6 = 3.14j
print(type(num1), num1)
print(type(num2), num2)
print(type(num3), num3)
print(type(num4), num4)
print(type(num5), num5)
print(type(num6), num6)

运行结果:

<class 'int'> 10
<class 'int'> -10
<class 'float'> 1.1
<class 'float'> -1.1
<class 'int'> 222222222222222222222222222222222222222222222222222222
<class 'complex'> 3.14j

注:python3中只有int、float、complex类型,已经没有Lang类型了,超长数据也是int类型

数据类型没有什么重要的方法,如果硬要说一个相关的就是int(),float(),可以把其他类型转换为int类型、float类型

s = "123"
i = int(s)
f = float(s)
print(type(i),i)
print(type(f),f)

结果:

<class 'int'> 123
<class 'float'> 123.0

2.字符串-str

引号引起来的一串字符,这串字符中可以有数字、字母、符号、汉字都可以

python中既可以用单引号也可以用双引号,不过要成对出现,如果字符串内部有单引号,那整个串就用双引号,反之,串内有双引号,整个串就用双引号

str1 = "123()— = + !@#¥%……*( 哈哈哈"
str2 = '字符串内部有"双引号'
str3 = "字符串内部有'双引号"
print(type(str1), str1)
print(type(str2), str2)
print(type(str3), str3)

打印结果为:

<class 'str'> 123()— = + !@#¥%……*( 哈哈哈
<class 'str'> 字符串内部有"双引号
<class 'str'> 字符串内部有'双引号

注:python3中不用写【# -*- coding: UTF-8 -*-】就可以直接打印中文

str类型的方法很多很多,先给一个list,在后面会专门来一个str方法的讲解

capitalize、casefold、center、count、encode、endswith、expandtabs、find、format、format_map、index、isalnum、isalpha、isascii、isdecimal、isdigit、isidentifier、islower、isnumeric、isprintable、isspace、istitle、isupper、join、ljust、lower、lstrip、maketrans、partition、replace、rfind、rindex、rjust、rpartition、rsplit、rstrip、split、splitlines、startwith、strip、swapcase、title、translate、upper、zfill,还有一些魔法方法,其中重要的必须要知道的:find、format、index、join、replace、split、strip

3.列表-list

由中括号括起来的一串字符,这就是列表,列表可容一切字符

lst = [1, "a", True, (1, 2), [12, "aa"], {"a": 1, "b": "b"}]
print(type(lst))
print(lst)

结果

<class 'list'>
[1, 'a', True, (1, 2), [12, 'aa'], {'a': 1, 'b': 'b'}]

列表的特性:可变、有序、可迭代性

由于【可变】,所以它有append、clear、count、extend、insert、pop、remove、copy方法

由于【有序】,所以它有index、reverse、sort

它还可以使用切片、in、for循环

list上面的这些特性和方法都很重要,要记住~~~

4.元组-tuple

由圆括号括起来的一串字符,这就是元组,元组也可容一切字符(这里是不是隐隐有种见过类似一句话的感觉~~~)

tup = (1, "a", True, (1, 2), [12, "aa"], {"a": 1, "b": "b"}, )
print(type(tup))
print(tup)

结果:

<class 'tuple'>
(1, 'a', True, (1, 2), [12, 'aa'], {'a': 1, 'b': 'b'})

注:

元组的特性:有序、不可修改,切记:元组不可修改,不过是不可修改一级元素,如果一个元素中的元素是可变对象的话,那个元素是可修改的

tup = (1, "a", True, (1, 2), [12, "aa"], {"a": 1, "b": "b"}, )
print(type(tup))
print(tup)
tup[4].append("haha")
print(tup)

结果:

<class 'tuple'>
(1, 'a', True, (1, 2), [12, 'aa'], {'a': 1, 'b': 'b'})
(1, 'a', True, (1, 2), [12, 'aa', 'haha'], {'a': 1, 'b': 'b'})

元组的方法只有两个:count、index

索引、in、for循环都是适合元组的

5.字典-dict

由大括号括起来的一对对的键值对,这就是字典,字典的值也可容一切字符(多么熟悉的一句话啊~~)

科普-什么是键值对:就是【键和值,两个配成一对】,就是【1: "a"】,其中写在前面的是键,后面的是值,中间用冒号隔开,一对对键值对用逗号隔开。键不可重复,值可以重复

ps:键重复以后执行不会报错,但是只显示最后一个,所以这个要注意,不然在你不知道的情况下会漏掉了一些数据

dic = {1: "a", "a": (1, 2), "bb": [12, "aa"], 33: {"a": 1, "b": "b"}}
print(type(dic))
print(dic)

结果:

<class 'dict'>
{1: 'a', 'a': (1, 2), 'bb': [12, 'aa'], 33: {'a': 1, 'b': 'b'}}

dict字典的特性:

猜你喜欢

转载自www.cnblogs.com/lybolg/p/12459740.html