|
0.变量和赋值:x=5name=“John”1.数据类型:整数(int)浮点数(float)字符串(str)布尔值(bool)2.注释:#这是单行注释"""这是多行注释"""123453.算术运算:a+b#加法a-b#减法a*b#乘法a/b#除法a%b#取余a**b#幂运算1234564.比较运算:a==b#等于a!=b#不等于a>b#大于a=b#大于等于astr:returnparam2*param11238.枚举:fromenumimportEnumclassColor(Enum):RED=1GREEN=2BLUE=312345639.迭代器:classMyIterator:def__init__(self,start,end):self.current=startself.end=enddef__iter__(self):returnselfdef__next__(self):ifself.current>=self.end:raiseStopIterationelse:self.current+=1returnself.current-1123456789101112131440.JSON解析:importjsonjson_str='{"name":"John","age":30}'data=json.loads(json_str)123441.日期和时间:fromdatetimeimportdatetimenow=datetime.now()1242.随机数生成:importrandomrandom_number=random.randint(1,10)12343.数学运算:importmathmath.sqrt(16)12344.模块和包:#my_module.pydefmy_function():return"Hello"#main.pyimportmy_modulemy_module.my_function()123456745.命名空间:global_var=5defmy_function():local_var=10globalglobal_varglobal_var=2012345646.继承和多态:classAnimal:defspeak(self):passclassDog(Animal):defspeak(self):return"Woof"classCat(Animal):defspeak(self):return"Meow"123456789101147.操作系统交互:importosos.getcwd()os.listdir(".")123448.命令行参数:importsysforarginsys.argv:print(arg)123449.正则表达式:importrepattern=r"\d+"re.findall(pattern,"Thereare2applesand5bananas.")1234
|
|