(资料图片)
今天给大家总结了 33 个 Python 关键字的含义, 每一个关键字列出一个例子,供大家参考学习和记忆。
x1 = (5 >3 and 5 < 10)x1x2 = (5 >3 or 5 >10)x2x3 = Falsenot x3
def func(x): if x < 18: print("未成年") elif x < 30: print("青年") else: print("中老年")func(25)
def func1(x): try: 100//x except: print("ZeroDivisionError: division by zero(除数不能是0)") else: print(f"程序计算结果是{str(100//x)}")func1(10)func1(0)
name_list = ["张三","李四","王五"]for name in name_list: print(name)
x = 0while x<9: print(x) x += 1
print(9 >6)print(6 in [11,6,33])print(5 is 5)print(5 == 5)print(5 == 5 and 7 == 7)print(5 == 5 or 6 == 7)print(not(5 == 7))
print(9 < 6)print(6 in [11,88,33])print(15 is 5)print(5 != 5)print(5 < 5 and 7 == 7)print(5 <= 3 or 6 >= 7)print(not(5 == 5))
for i in range(10): if i <= 5: continue print(i)
x = 0while x < 10: if x == 5: break x += 1 print(x)
def func(x): try: 100 // x except: print("ZeroDivisionError: division by zero(除数不能是0)") else: print(f"结果是:{str(100 // x)}") finally: print("无论如何,都会执行!") func(10)func(0)
x = 15if x < 18: raise Exception("不好意思,你的年龄小于18岁,未成年")
import openpyxlimport pandas as pdfrom openpyxl import load_workbook()
def func1(): print("关注") func1()
def func2(x,y): return x + yfunc2(x=2,y=8)
class Person: name = "张三" age = 18 p = Person()p.name,p.age
x = lambda a: a + 8x(2)y = lambda a,b: a + by(1,1)z = lambda a,b,c: a * c + bz(2,5,5)
x = 1del xprint(x)
x = ["张三","李四","王五"]del x[0]print(x)
# 定义一个函数:def func(): global x x = "函数中的变量"# 执行函数:func()# x定义在函数中,按说这里打印x会报错,我们看看print(x)
x = ["张三","李四","王五"]"张三" in x# -------------------------for i in range(3): print(i)
x = 2.0y = 2.0x is yx == y
x = Noneprint(x)if x: print("嘻嘻")else: print("哈哈")
x = 666assert x == 666assert x == 888,"x应该等于666,你的输入有误!"
with open("哈哈.txt","r") as f: print(f.read())
def f(): yield 5f()next(f())
- EOF -
重磅!程序员交流群已成立
公众号运营至今,离不开小伙伴们的支持。
为了给小伙伴们提供一个互相交流的平台,特地开通了程序员交流群
群里有不少技术大神,不时会分享一些技术要点,更有一些资源收藏爱好者不时分享一些优质的学习资料。(群完全免费,不广告不卖课!)
需要进群的朋友,可长按扫描下方二维码。
▲长按扫码