- Python Data Structures and Algorithms
- Benjamin Baka
- 229字
- 2021-07-09 19:44:58
Flow control and iteration
Python programs consist of a sequence of statements. The interpreter executes each statement in order until there are no more statements. This is true if both files run as the main program as well as files that are loaded via import. All statements, including variable assignment, function definitions, class definitions, and module imports, have equal status. There are no special statements that have higher priority than any other and every statement can be placed anywhere in a program. There are two main ways of controlling the flow of program execution, conditional statements and loops.
The if, else, and elif statements control the conditional execution of statements. The general format is a series of if and elif statements followed by a final else statement:
x='one'
if x==0:
print('False')
elif x==1:
print('True')
else: print('Something else')
#prints 'Something else'
Note the use of the == operator to test for the same values. This returns true if the values are equal; it returns false otherwise. Note also that setting x to a string will return something else rather than generate a type error as may happen in languages that are not dynamically typed. Dynamically typed languages such as Python allow flexible assignment of objects with different types.
The other way of controlling program flow is with loops. They are created using the while or for statements, for example:

- Java入門經典(第6版)
- Visual C++串口通信開發入門與編程實踐
- Azure IoT Development Cookbook
- Vue.js前端開發基礎與項目實戰
- Cassandra Data Modeling and Analysis
- Windows Server 2012 Unified Remote Access Planning and Deployment
- Hands-On Enterprise Automation with Python.
- Kinect for Windows SDK Programming Guide
- Scala編程實戰(原書第2版)
- Access 2010數據庫應用技術(第2版)
- 深入分布式緩存:從原理到實踐
- H5+移動營銷設計寶典
- Python硬件編程實戰
- Learning iOS Penetration Testing
- INSTANT EaselJS Starter