官术网_书友最值得收藏!

Python basic syntax and block structure

This section primarily provides a basic understanding of the Python language constructs. If you feel you already have a solid grasp of Python, feel free to skip ahead.

Let's get down to the nuts and bolts.

A Python program is written as source code in one or more .py files and consists of statements and expressions as shown in the following screenshot:

Both statements and expressions tell Python to do something. The difference is that expressions can be combined to form more complex expressions, while statements can be combined with expressions, but not with other statements.

For example, a statement looks like this:

if 2 > 1: 

An expression looks like this:

print ("One is the loneliest number") 

Python source code files are executed from top to bottom as soon as they're loaded by the Python runtime. This means that for simple programs, we could just write a series of statements in a .py file and then tell Python to run them. In the preceding example, the if and else parts are statements or a single statement with two parts, if you prefer to think of it that way. Everything else is an expression. For more complex programs, we need a more structured approach.

Like most programming languages, Python lets us create functions and classes in order to organize our code.

If you don't know what functions or classes are, you could think of functions as miniature programs that can be used as building blocks for larger programs and classes as combinations of functions and data to create new kinds of data.
主站蜘蛛池模板: 大姚县| 临潭县| 淮阳县| 金山区| 乌审旗| 苗栗县| 东丽区| 浦县| 卢氏县| 通化县| 渝北区| 布尔津县| 奉节县| 清徐县| 饶阳县| 万山特区| 甘泉县| 正镶白旗| 深泽县| 伊吾县| 邢台市| 遂昌县| 安达市| 临漳县| 云龙县| 冕宁县| 建昌县| 南涧| 囊谦县| 汉川市| 阿合奇县| 长顺县| 库尔勒市| 丰宁| 嵩明县| 娄底市| 杂多县| 仁布县| 聊城市| 巨鹿县| 蒙阴县|