python的类型_python数据类型

数据类型String字符串

Number数字类型

int整型

long长整型

float浮点型

List列表

Tuple元组

Dictionary字典

Boolean布尔值

Set集合

String 字符串say = "Hello"

类型转换,将数字类型转换成字符串'''

str:将数字类型转换成字符串类型

type:可以查看一个变量的类型:代表是整型:代表是字符串

'''

a = 1

print (type(a)) #输出结果:a = str(a)

print (type(a)) #输出结果:#这里我们可以看出来,我们把一个整型转换成了字符串

int 整型就是没有小数点say = 1 #这里可以是1 3 5 55...

long 长整型比整型长度更长整数没有小数点say = 1000000000000000000

float 浮点型我们的钱分元角分厘一般和钱有关的存在小数的居多say = 168.68 #这里假如是钱那就是168元6角8分

List 列表say = ["hello", 1, 2, "hi", "you"]

print (say) #输出结果:['hello', 1, 2, 'hi', 'you']

print (say[0]) #输出结果:hello

print (say[1]) #输出结果:1

print (say[1:]) #输出结果:[1, 2, 'hi', 'you']

print (say[1:3]) #输出结果:[1, 2]

print (len(say)); #输出结果:5

用enumerate同时遍历列表索引和索引对应的值hello = ["nice", 1, "you"]

for i,v in enumerate(hello):

print(i, v)

6371070302972511426354195.jpg

用zip同时遍历多个列表hello = ["nice", 1, "you"]

hi = ["to", 2, "meet"]

for item1, item2 in zip(hello, hi): #这里可是多个列表,自已可以尝试,zip函数接收参数相当于可变参数

print(item1, item2)

6371070375887040573798313.jpg

用reversed反向遍历列表hello = ["nice", 1, "you"]

for item1 in reversed(hello):

print(item1)

6371070414932602968579123.jpg

用sorted排序遍历列表hello = [7, 5, 1, 3, 9]

for item1 in sorted(hello): #这里需注意使用sorted需要表列数据类型要一致才行,要么是int,要么是str

print(item1)

6371070448460942542115023.jpg

Tuple 数组say = ("hello", 1, 2, "hi", "you")

print (say) #输出结果:('hello', 1, 2, 'hi', 'you')

print (say[0]) #输出结果:hello

print (say[1]) #输出结果:1

print (say[1:]) #输出结果:(1, 2, 'hi', 'you')

print (say[1:3]) #输出结果:(1, 2)

print (len(say)); #输出结果:5

List列表和Tuple元组区别say =["hello", 1, 2, "hi", "you"]

say[1]="abc";

print (say) #输出结果:['hello', 'abc', 2, 'hi', 'you']

say = ("hello", 1, 2, "hi", "you")

say[1]="abc";

print (say) #输出结果:报错了如下

Traceback (most recent call last):

File "xxx\xxx.py", line 2, insay[1]="abc";

TypeError: 'tuple' object does not support item assignment

Dictionary 字典say = {}

say["hello"] = "how are you!"

say[0] = 1

say[1] = "hi"

print (say[0]) #输出结果:1

print (say["hello"]) #输出结果:how are you!

say = {"hello": "how are you!", 0: 1, 1:"hi" }

print (say[0]) #输出结果:1

print (say["hello"]) #输出结果:how are you!

以下也为代码示例:'''

'''

hello = {"hello":1, "are":2, "you":"you"}

print("hello =", hello)

print("hello['are'] =", hello["are"]) #打印键为are的值

hello["are"] = 888 #给键are赋值新值为888

print("hello['are'] =", hello["are"]) #打印键为are的值

print("hello =", hello)

hi = list(hello.keys()) #获取所有的键列表

print("hi =", hi)

how = sorted(hello.keys()) #获取所有的键列表并排序

print("how =", how)

del hello["are"] #del删除键为are的项(元素)

print("hello =", hello)

flag = "test" in hello #判断test键是否在hello字典中

print("flag =", flag)

flag = "you" in hello #判断test键是否在hello字典中,结果中有True/False

print("flag =", flag) #判断you键是否在hello字典中,结果中有True/False

flag = "test" not in hello #判断test键是否不在hello字典中,结果中有True/False

print("flag =", flag)

6371046697138430822545513.jpg

for遍历字典hello = {"hello": "h1", "how": "h2", "are": 123, "you": 2, "nice": "n1"}

print(hello)

for key,val in hello.items():

print(key, val)

6371047603162871181149636.jpg

字典中get()用法name = {"hello":"hi", "hi":"hello"}

print('name["hi"] =', name["hi"])

print('name.get("hi") =', name.get("hi"))

print('name.get("hsi") =', name.get("hsi"))

6371184648467105985075464.jpg

Boolean 布尔值flag=True;

if flag:

print ("True")

print("hello")

else:

print ("False")

print ("hi")

'''

输出结果:

True

hello

'''

Set集合how = { "nice", "how", "are", "you", "nice"} #声明一个集合并赋值

print("how =", how) #打印输出发现已去除重复项

are =set() #声明一个空集合

print("are =", are)

are.add("you") #空集号添加一个值

print("are =", are)

print("--------------------------------以下参考一下就行")

hi = set("abcdeabfcd")

hello = set("abcdega")

print("hi =", hi)

print("hello =", hello)

print("hi - hello =", hi - hello) #获取hi集合中,在hello集合中不存在的项

print("hello - hi =", hello - hi) #获取hello集合中,在hi集合中不存在的项

print("hi | hello =", hi | hello) #获取hi和hello的合集,去除重复

print("hi & hello =", hi & hello) #获取即在hi集合中,也在hello集合中的项

print("hi ^ hello", hi ^ hello) #获取只在一个hi集合或hello集号中出现的项,也就是这个元素(项),不同时存在hi和hello集合

运行结果图片:

6371046343184019024436036.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值