This is my first formal start to learn the language, although it has been more or less touched a bit, but has not been in-depth study and research, so I intend to start from today to begin to learn the language of the following.
Python this language and other languages compared to the biggest feature is simple, is simple, such as the completion of the same function, may be implemented in C + +, you need to write a lot of code, but in Python here just a few lines of code can be done, this is his advantage, of course, his advantage is far more than this, There may be many, but to the present I, may not have found, the other needs me in the future study in depth to understand and understanding!
Use of While
First, a flowchart.
1 #Coding=utf-82myage=563 4Count=15 Print('you might as well guess my age? You have three chances! ')6 whileCount<4:7Age=int (Input ('%d chance: Enter my age in your mind! -'%count))8 ifage==Myage:9 Print("you are so good, you guessed it! ")Ten Break One elifAge>Myage: A Print("I think it's a little bit smaller than you think! ") - elifage<Myage: - Print("Although I know I look very young, but you guessed wrong oh! ") the -Count+=1 - ifCount==4: -Continue_confirm=input ("you've run out of three chances! Are you trying again? y/n") + ifcountine_confirm=='y': -Count=1 + Else: A Print('It seems the game is over! But you seem to have lost! ')
Through the above a simple sample, you can see his basic principle and C + +, but note that in Python he is by the code indentation, to distinguish between the levels of code, so the indentation is very important, to pay attention to the corresponding level, cut can not mix!
":" This symbol can not be lost!
Judging condition: Any null value is False(0, empty string "", empty list [], empty Dictionary {}) non-null not 0 is True
Use of break
In Python, for and while have a corresponding statement to exit this loop he is break, he can use in for and Whil is in the form of the loop condition is still in the state of the exit, the simple is forced to quit the loop body.
For example:
1 sum=02while# dead Loop 3 print(' sum=%d '%sum)4 sum++5 if sum>3:6 break # exits the loop body
This is a simple small example, when the value of sum reaches 3 and exits directly, very well understood.
The use of continue
The role of continue is also the exit loop, but he is different from break, is directly out of the loop, and he just quit the loop and then go to execute the next time, the difference is that one is completely out of the loop, one is only introduced this cycle.
For example:
1 num=02 while True:3 num+=14 if num%2==0:5 continue# jump out of the loop and execute the next 6 Print ('num=', num)
This code will print out all the odd numbers from 0 to n when he encounters an even number and he jumps out when he encounters an odd number.
Python's learning Note (0) The use of loops 1