Basic Analysis of Python Process Control
Which language has flow control, that is, if switch while statements.
The principle and function of distance control for each language are similar, but the expressions are different.
Today I will share with you a Python statement, such as conditions and loops.
Here, we will not use braces, but code blocks.
Note the colon after if and else:
If
name = raw_input("What is your name? ")if(name.endswith('Gumby')): print 'Hello, Mr. Gumby'
Else
name = raw_input("What is your name? ")if(name.endswith('Gumby')): print 'Hello, Mr. Gumby'else: print 'Hello, Stranger'
Elif
Note that the else if statement we use in C ++ is directly written in Python: elif
num = input('Enter a number: ')if num >0 : print 'The number is positive'elif num < 0: print 'The number is negative'else: print '0'
While
x = 1while x <= 100 print x x += 1
For Loop
If you can use for, try to avoid using while
words = ['this', 'is', 'an', 'ex', 'parrot']for word in words: print word
For dictionary Traversal
d = {'x':1, 'y':2, 'z':3}for key in d: print key, 'corresponds to', d[key]
Zip and row Iteration
names = ['name', 'beth', 'george', 'damo']ages = [12, 45, 32, 99]for name, age in zip(names, ages): print name, 'is', age, 'years old'
Break bounce cycle
Continue