Python3 语法基础思维导图

示例代码 :



import io
import sys
 
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8')
print('元组定义')

tup = ('Google','IE','sougou','360')
for everyOne in tup:
    print(everyOne)

print('元组访问')
print ("tup[0]: ", tup[0])
print ("tup[1:4]: ", tup[1:4])

print('修改元组')
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
# 创建一个新的元组
tup3 = tup1 + tup2
print (tup3)

print('删除元组')
tup4 = ('Google', 'Runoob', 1997, 2000)
 
print (tup4)
del tup4


print('集合创建方式一:\{\}')
coll ={'one', 'two', 'three'}
print(coll)
print('集合创建方式一:set')
colltwo = set('123456')
print(colltwo)

print('集合添加')
collthree = set(("Google", "IE", "Sougou"))
print('未添加之前:')
print( collthree)
collthree.add('360')
print('添加之后:')
print(collthree)

print('集合删除')
collthree.remove('IE')
print('集合移除之后:')
print(collthree)

print('集合中个数总量计算')
print(len(collthree))

print('集合中的元素清空')
collthree.clear()
print(collthree)

print('判断元素是否在集合中存在')
collfour = set(("Google", "IE", "Sougou"))
if 'IE' in collfour:
    print('包含')
else:
     print('不包含')

print('集合运算符')
pf = set(['邓肯','加内特','马龙'])
pd = set(['邓肯','奥尼尔','姚明'])


print('集合运算符之交集运算:', pf & pd)
print('集合运算符之并集运算:', pf | pd)
print('集合运算符之差集运算:', pf - pd)

print('字典创建')
dict = {'编号': '0001', '姓名': '周晨曦', 'age': '2'}
print(dict)

print('字典访问')
print('dict[\'编号\']',dict['编号'])

print('字典遍历')
for item in dict.items():
    print(item)

print('字典修改')
dict['编号'] = '0002'
print(dict)

print('字典删除')
del dict['编号'] # 删除键 'Name'
print(dict)
dict.clear()     # 清空字典
print(dict)
del dict         # 删除字典

# if...else 简单选择语句
age = 3
if age == 2:
    print('恭喜你符合年龄要求')
else:
    print('提示你年龄不符合要求')

# python3 if...elif...else
score = 99
if score ==100:
    print('满分')
elif score >=90:
    print('优秀')
elif score >=80:
    print('良好')
elif score >=70:
    print('及格')
else:
    print('不及格,请家长')

# python3 if语句嵌套
if score >= 90:
    if score == 100:
        print('A+')
    else:
        print('A')
elif score >=80:
    if score >=85:
        print('B+')
    else:
        print('B')
elif score >=70:
    print('C')
else:
    print('D')

# python3 and 选择语句
age =45
if(age >= 18 and age <=65):
    print('中国司机法定年龄为18~65岁之间,你的年龄是符合要求的')

# python3 or 选择语句
answer ='A'
if(answer == 'A' or answer =='B'):
    print('这道选择题不管是选择A还是B 都是正确的')

# python3 not 语句
a ='1'
b =['A','B','1']
if a not in b:
    print('a 元素不存在于b序列中')
else:
    print('a 元素存在于b序列中')

# python3 for 遍历序列

c =['A','B','C']
for obj in c:
    print('value is', obj)


# python3 for 遍历对象

st = 'ABCDEFG'
for obj in st:
    print('value is', obj)

# python3 while 循环语句

i = 1
while i <=10:
    print('坚持就是胜利',i)
    i =i + 1

# python3 break 跳转语句

st = 'ABCDEFGH'
for obj in st:
    if obj == 'D':
        print('中止循环')
        break
    print('value is', obj)

# python3 continue 跳转语句

st = 'ABCDEFGH'
for obj in st:
    if obj == 'D':
        print('中止当前循环,进入下一个循环')
        continue
    print('value is', obj)

 


版权声明:本文为zhouzhiwengang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。