python对象的基本操作代码

时间:2023-03-08 20:33:31

基础:

#对象.方法()

# a=1
# b=a
# a=2
#
# _a=2423
#
# print(a)
# print(b)
#
# print(False and False)
# print(False or False)
#
# print(0 and 2)
# print(1 and 2)
# print(1 and 0)
#
#
# print(2 in [1,2,3])
#
# #字符串
#
# #转义符号
# s='Let\'s go'
#
# print(r"\fsdghlfjdk.")

#查找:[:]

# s1="hello world"
#
# print(s1[1:4])
# print(s1[1:4:2])
# print(s1[-1])
# print(s1[:])
# print(s1[:8])
# print(s1[1:-1])
# print(s1[1:])
# print(s1[-3:-1])

#strip():把字符串开头和结尾的空格以及\n

#s="   hello\nworld".strip()
# s="   hello\nworld\n"
# s1="   hello\nworld\n".strip()
# s1="**hello\nworld\n***".strip("*")
# print(s)
# print(s1)

#拼接方法

# s="hello"+"world"+"I"+"am"+"python"
# print(s)
#
# print(" ".join(["I","am","world!"]))

#分割方法
# s="hello world".split("l",1)      # ["he","lo world"]
# print(s)

#查找字符
# print("hello world".find("a",4))
# # print("hello world".rfind("l"))
#
# #
# print("hello world".index("q"))

#替换方法

# s="hello world"
# print(s.replace("world","Python"))
# print(s)

#居中显示
# print("hello world".center(50,"*"))
# print("hello world".ljust(50,"*"))

#字符串的格式化输出
#%s:字符串  %d:整型  %f:浮点型

# print("hello %s,%s"%("sb","egon"))
# print("hello %s, his age is %d"%("sb",35))
# print("hello %s, his age is %.4f"%("sb",35.53452345))

# print("hello {0}, his age is {1}".format("alex",34))
# print("hello {0}, his age is {1}".format(34,"alex"))
#
# print("hello {name}, his age is {age}".format(age=30,name="wusir"))
#
#
# print("hello {name}, his age is {age}".format_map({"name":"egon","age":1000}))

# print("一".isdecimal())
# print("一".isdigit())
# print("壹".isnumeric())

# print("hello world".capitalize())
# print("hello world".title())
#
# print("HELLO world".casefold())
# print("HELLO world".lower())

# print("HELLO\tworld")
# print("HELLO world".expandtabs())

# "HELLO world".rsplit()
# print("HELLO\n wor\nld\n".splitlines())
# print("HELLO\n wor\nld\n".split("\n"))

#print("HELLo world".zfill(10))

print(type(None))

# []
#
# ()
#
# {}
#
#

# print(bool(-1))
# print(bool([1,]))
# print(bool(None))
#
# if None:
#     print("ok")

集合:

#集合set  两个功能:1 去重 2关系测试

# s=set([1,3,"hello"])
# s2={1,2,3}
#
# print(s)
# print(type(s2))

#去重:
# l=[1,2,2,34,45]
# s="hello"
#
# print(set(l))
# print(set(s))

# print({{1,2}:"hello"})#  set是可变数据类型
# print({[]:"hello"})

# s3={1,2,[1,2]}     #  set集合内元素一定是不可变数据类型
# print(s3)

#关系测试

s1={"hello",1,2,3}

s2={1,2,("a","b")}

# 求并集

print(s1.union(s2))
print(s1|s2)

# 求交集

print(s1.intersection(s2))
print(s1&s2)

# 求差集
print(s1.difference(s2))

set

列表:

#创建形式    可迭代对象:能够进行for循环

l=[1,"hello",[4,5],{"name":"egon"}]
l2=list([1,23,3])
l3=[1,23,["hello",334],656,77]
# print(l2)
# print(type(l2))
#
# #查:切片[:]
# print(l3[-2:])

#增加

# l3.append("yuan")
# l3.append(7)
# print(l3)
# l3.insert(2,"jjj")
# print(l3)

#
# l3.extend([7,8])
# print(l3)

# ret=l3.pop(1)
# print(ret)
# print(l3)

# l3.remove(1)
# print(l3)

# del l3[2]
# print(l3)
# del l3
# print(l3)

#改  赋值操作

# print(id(l3))
#
# l3[2][0]="yuan"

# l4=[12,3]
#
# # l4.clear()
# # print(l4)
#
# l4=[]   #推荐这种方式

# [1,222,33].count(33)

# len

# print(len(l3))
# #l5=[3,1,56,34]
# l5=["A","a","B"]
#
# l5.sort(reverse=True)

# print(l5)
# sorted(l5)
#
# [1,2,333,4].reverse()

# count=0
# for i in [11,22,33]:
#     print("%s---%s"%(count,i))
#
#     count+=1

# l=[1,2,333,4]
# for i in l:
#     print(l.index(i),i)

# l=[1,2,333,4]
# for i ,v in enumerate(l,1):
#     print(i,v)

list

循环:

# for i in seq:   # seq可以是字符串,列表,元组,字典
#     pass
#
# print("ok")

# 两个问题:
# 1 循环次数有序列的一级元素的个数决定

#item是什么?

for item in ["hello",123,[2,3,4]]:
    if item==123:
        continue  #结束的本次循环
        #break      # 结束的整个for循环
    #print("ok")
    print(item)

else:
    print("ending for")

print("ending")

# print(range(10))
#
# for i in range(10):
#     print("ok")

# while 条件表达式:
#     执行代码

#
# while 2>1:
#     print("ok")

# 打印1-100

# i=1
# while i<101:
#     print(i)
#     break
#     i+=1
#
# else:
#     print("ok")

# for i in range(1,101):
#     print(i)

loop

字典:

#d={[1,2,3]:"yuan","name":"egon"}# 键唯一且为不可变数据类型
#d={1:"yuan","name":"egon"}# 键唯一且为不可变数据类型

#查
# print(d["name"])
#
# v=d.get("names",None)  #推荐
# print(v)
#
# if not v:
#     pass

#遍历

# for i in "hello":
#     print(i)

#d={1:"yuan","name":"egon"}

# for i in d:
#     #print(i,"----->",d[i])
#     print("%s---->%s======"%(i,d[i]))

# print(list(d.keys()))
# print(d.values())
# print(d.items())

# 1---->"yuan"
# "name"---->"egon"

#增
d={1:"yuan","name":"egon"}

# d["age"]=123
# print(d)
#
#
# #修改
# d[1]=666
#
# print(d)

#删除

# ret=d.pop(1)
# print(ret)
# print(d)

# d2={"height":123,"sex":"male","name":"alex"}
#
# d.update(d2)
# print(d)

对经常使用的几种类型的常用方法在学习中测试留下的代码,留作纪念。