python笔记

时间:2023-03-09 17:57:55
python笔记
# coding=utf-8 定义后可使用中文
#var定义输出
i=1
print(i)
#if
if i>0:
print(i)
else:
print("x")
#循环+字符串拼接
for i in range(1,100):
print("hello {0} {1}".format(i,"fei"))
#定义函数
def sayhello():
print("hello world!")
sayhello()
#写个递归吧
def f(n):
if(n==1 or n==0):
return 1
else:
return f(n-1)*n
print(f(0))
#面向对象
class HI:
def __init__(self,name):
self.name=name
def out(self):
print("HI {0}".format(self.name))
hi=HI("XXX")
hi.out()
#继承
class hello(HI):
def __init__(self,name):
HI.__init__(self,name)
def out2(self):
print("Hello {0}".format(self.name))
#引入文件
import macpath
c1="hello \n"*2
print(c1)
#元组和列表:元组可读不可写,列表可读可写,元组使用(),列表使用[]
#集合运算
a=set("abcde")
b=set("ad")
#运算
y=a&b
x=a-b
y=a|b
#消除重复元素
new=set(a)
#字典
k={"name":"xx","sex":"male","age":"20"}
print(k["name"])