设为首页 加入收藏

TOP

python对象的基本操作代码(一)
2017-09-30 17:21:17 】 浏览:7454
Tags:python 对象 基本操作 代码

基础:

#对象.方法()

# 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))


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



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

集合:

#集合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.intersectio
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇object and namespace 下一篇tkinter学习-Lable&Button

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目