悉野小楼

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Python学习笔记2 常用类型list tuple dict set

复制代码
def double(a):
    """两倍
    处理 三个引号可以多行注释, 3个单引号也可以用来多行注释 """
    return a * 2
a = double(5)
print(a)
if isinstance(a, int):    #检测是否是某个类型
    print("a是整数")
print(True + 1)            #True为1
print(False + 1)        #False为0
print(type(None))        #NoneType
print(type(1.1e5))        #float 1.1 * 10^5
print(type(0o31))        #int 0b101  #0b二进制 0x十六进, 0o 八进
print(type("ac"))        #str
print(type(["a","b"]))     #list    list("hello") == ['h', 'e', 'l', 'l', 'o']
print(type(("a","b")))    #tuple
print(type({"a":"b"}))    #dict    dict([[1,2],[3,4]]) dict([(1,2),(3,4)]) 列表可以强转为dict
print(type({2,3,4}))     #set set([1,2,3]) 可以强转为set
tmp = ["a","b", "c"]

if "b" in tmp:
    print("b在tmp列表中")
print(tmp[-1]) #输出c, list可以赋值, 从最后一位数的

tmp = ("a","b")
if "b" in tmp:        #元组也可以计算在不在中间
    print("b在tmp的元组中")
print(tmp[-1]) #输出b, tuple不支持赋值, 从最后一位数的

tmp = {"a":"b", 1:5}        #key不用同一类型, 但查时要"1"与1是不同的
if "a" in tmp:
    print("a在tmp的dict中")    #b作为值不行
    
print("int(-10.6)为",int(-10.6)) #强转丢弃小数部分
print("int(10.6)为",int(10.6))    #强转丢弃小数部分

set = {1,2,3,3}
print("{1,2,3,3}实际为",set, ",长度为", len(set)) #输出{1,2,3} 输出长度为3

if 1 in set:    #检测是否在集合中
    print("set", set, "包含1")
else:
    print("set", set, "不包含1")
    
'''导入其它文件, 拿变量
import tmp
tmp.t2 = 333
print(tmp.t2) #导入另一个文件
'''

print("dict([[1,2],[3,4]]) == dict([(1,2),(3,4)]", dict([[1,2],[3,4]]) == dict([(1,2),(3,4)])) #输出True, 只判断值相等就是true, 不用判断地址的

if True == 1 and False == 0:
    print("True==1 and False == 0是对的")
    
if None != False:
    print("None!=False是对的")

a, b, c = 5, 3.2, "Hello"
x = y = z = "same"

复制代码

 

posted on   悉野  阅读(9)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2011-09-27 更新表 使用表中一列等于另一张中列的内容
点击右上角即可分享
微信分享提示