Python indentation represents a level in a recommended four spaces
Branch and loop
Branch condition is circulating in the most central point, solve the problem scenario is different problems have different processing logic. When the conditions are satisfied, or a single or multiple branching and looping into the condition is not satisfied, the process will be described here on the basis of the same logic Executive dynamic change specific parameters, thereby generating a variety of possibilities, but requires a possibility must perform other wherein possibility need not be performed when using branches.
The core loop is a boundary value, the control execution frequency of boundary value control cycle, the logic loop is repeated until reaching the limit value executed, out of the loop.
In Python not form scope branching and looping, branching, and form a loop in scope as Golang other languages. Python No ++ - increment decrement the operator, and is used for loop iteration data can traverse.
Branched and cyclic specification:
Is not recommended to write a lot of code at a logic branching and looping, or a function package few lines of code to write a small amount of
Branch - if elif
number = 10
if number >= 100:
print("Hai")
elif 50 <= number < 100:
print("Hello")
else:
print("OK")
-For cycle
students = ["QiNiuYun", "BaiDu", "WeiChat", "AliYun"]
for student in students:
print(student)
-While cycle
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
start = 0
while start <= 100:
print(start)
start += 1
Loop flow control
contine this cycle begins, i.e. this line contine back loop are no longer performed, restarting the loop to enter is determined from the condition
start = 0
while start < 100:
start += 1
if start % 2 == 0:
continue
print(start)
break this end of the whole cycle, which is performed from this line after the break, the loop out of the loop execution, the execution cycle behind the logical structure
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
start = 0
while start < 100:
start += 1
if start > 50:
break
print(start)
Usually joint break and contine branches special cases flow control loop
Variable iterables joint in general for loop
The iterables str list tuple set dict range emumerate __ getitem__
Traversal str
name = "beimenchuixue"
for rune in name:
print(rune)
Traversal list
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
names = ["QiNiuYun", "BeiMenChuiXue", "AliYun", "BaiDu"]
for name in names:
print(name)
Traversal tupe
platforms = ("Linux", "MAC", "Windows", "Android", "IOS")
for platform in platforms:
print(platform)
Traversal set
languages = {"zh_CN", "en_US"}
for language in languages:
print(language)
Traversal dictionary dict
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
info = {"name": "BeiMenChuiXue", "age": 18, "sex": "male", "phone_number": "15570910000"}
for key in info:
# 在字典的遍历中,只能获取字典的key值
print(key, info[key])
Traversing range
for number in range(1, 100, 2):
# 从1开始到100,但不包括100,间隔步长为2
print(number)
Traversal emumerate
names = ["QiNiuYun", "BeiMenChuiXue", "AliYun", "BaiDu"]
for name in enumerate(names):
# emumerate 索引从0开始计数,返回一个由索引和值组成的元组
print(name)
Traversal __ gettiem__
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Class(object):
def __init__(self, student_names):
self.student_names = student_names
def __getitem__(self, item):
return self.student_names[item]
python_class = Class(["QiNiuYun", "BeiMenChuiXue", "AliYun", "BaiDu"])
for python_student in python_class:
# for循环会去找 __getitem__魔法方法,每次从 0 开始步长为1向 __getitem__传递 参数
# 如果 __getitem__触发异常,for会处理异常并退出循环
print(python_student)
Use a dictionary to achieve Go language switch
def connect():
print("connect")
def conn():
print("conn")
def default_conn():
print("default_conn")
interface = {"connect": connect, "conn": conn}
interface.get("conn", default_conn)()
interface.get("", default_conn)()