📜  构建Python程序

📅  最后修改于: 2022-05-13 01:55:32.030000             🧑  作者: Mango

构建Python程序

在本文中,您将了解如何正确构建和格式化您的Python程序。

Python语句通常,解释器逐行读取和执行语句,即顺序。不过,有些语句可以改变这种行为,比如条件语句。
大多数情况下, Python语句是以这样一种格式编写的,即一条语句只写在一行中。解释器将“字符”视为一条指令的终止符。但是,您也可以在下面找到每行编写多个语句。
例子:

# Example 1
  
print('Welcome to Geeks for Geeks') 
输出:
Welcome to Geeks for Geeks
# Example 2
  
x = [1, 2, 3, 4]
  
# x[1:3] means that start from the index 
# 1 and go upto the index 2
print(x[1:3])  
  
""" In the above mentioned format, the first 
index is included, but the last index is not
included."""
输出:
[2, 3]

每行多个语句 我们也可以每行编写多个语句,但这不是一个好习惯,因为它会降低代码的可读性。尽量避免在一行中编写多个语句。但是,您仍然可以通过在“;”的帮助下终止一个语句来编写多行。 ';'在这种情况下,它被用作一个语句的终止符。
例如,考虑以下代码。

# Example
  
a = 10; b = 20; c = b + a
  
print(a); print(b); print(c)
输出:
10
20
30

行继续以避免左右滚动
有些语句可能会变得很长,并且可能会迫使您频繁地左右滚动屏幕。您可以以无需在这里和那里滚动的方式调整您的代码。 Python允许您在多行中编写单个语句,也称为行继续。续行也增强了可读性。

# Bad Practice as width of this code is too much.
 
#code
x = 10
y = 20
z = 30
no_of_teachers = x
no_of_male_students = y
no_of_female_students = z
 
if (no_of_teachers == 10 and no_of_female_students == 30 and no_of_male_students == 20 and (x + y) == 30):
    print('The course is valid')
 
# This could be done instead:
 
if (no_of_teachers == 10 and no_of_female_students == 30
    and no_of_male_students == 20 and x + y == 30):
    print('The course is valid')

续行的类型
一般来说,有两种类型的续行

  • 隐式续行
    这是编写跨越多行的语句的最直接的技术。
    任何包含左括号 ('(')、方括号 ('[') 或大括号 ('{') 的语句都被认为是不完整的,直到遇到所有匹配的括号、方括号和大括号。在此之前,语句可以隐式地跨行继续而不会引发错误。
    例子:
    # Example 1
      
    # The following code is valid
    a = [
        [1, 2, 3],
        [3, 4, 5],
        [5, 6, 7]
        ]
      
    print(a)
    
    输出:
    [[1, 2, 3], [3, 4, 5], [5, 6, 7]]
    
    # Example 2
    # The following code is also valid
      
    person_1 = 18
    person_2 = 20
    person_3 = 12
      
    if (
       person_1 >= 18 and
       person_2 >= 18 and
       person_3 < 18
       ):
        print('2 Persons should have ID Cards')
    
    输出:
    2 Persons should have ID Cards
    
  • 显式续行
    当隐式线连接不适用时,主要使用显式线连接。在这种方法中,您必须使用一个字符来帮助解释器理解特定语句跨越多行。
    反斜杠 (\) 用于指示语句跨越多行。需要注意的是,“必须是该行中的最后一个字符,即使是空格也是不允许的。
    请参阅以下示例以进行说明
    # Example
      
    x = \
        1 + 2 \
        + 5 + 6 \
        + 10
      
    print(x)
    
    输出:
    24
    

    Python中的注释
    在代码中编写注释非常重要,它们有助于提高代码的可读性,并能提供更多关于代码的信息。它可以帮助您针对语句或代码块编写详细信息。解释器忽略注释并且不在命令中计算它们。在本节中,我们将学习如何在Python中编写注释。
    用于编写注释的符号包括井号 (#) 或三重双引号 (“””)。哈希用于编写不跨越多行的单行注释。三重引号用于编写多行注释。三个三引号开始注释,三个引号结束注释。
    考虑以下示例:

    # Example 1
      
    ####### This example will print Hello World ####### print('Hello World')  # This is a comment
    
    # Example 2
      
    """ This example will demonstrate 
        multiple comments """
      
    """ The following
        a variable contains the 
        string 'How old are you?'
    """
    a = 'How old are you?'
      
    """ The following statement prints
        what's inside the variable a 
    """
    print(a)
    

    注意 请注意,字符串中的哈希 (#) 不会使其成为注释。考虑以下示例进行演示。

    # Example
      
    """ The following statement prints the string stored
        in the variable """
      
    a = 'This is # not a comment #'
    print(a) # Prints the string stored in a
    

    空白
    最常见的空白字符如下:

    CharacterASCII CodeLiteral Expression
    Space32 (0x20)‘ ‘
    tab9 (0x9)‘\t’
    newline10 (0xA)‘\n’

    * 您可以随时点击此处参考 ASCII 表。 Python解释器通常会忽略空格,并且大多数情况下不需要空格。当一个记号结束和下一个记号开始的地方很清楚时,可以省略空格。当涉及特殊的非字母数字字符时,通常会出现这种情况。
    例子:

    # Example 1
      
    # This is correct but whitespace can improve readability
      
    a = 1-2  # Better way is a = 1 - 2
      
    print(a)
    
    # Example 2
      
    # This is correct
    # Whitespace here can improve readability.
    x = 10
    flag =(x == 10)and(x<12)
    print(flag)
      
    """ Readable form could be as follows
    x = 10
    flag = (x == 10) and (x < 12)
    print(flag)
    """
      
    # Try the more readable code yourself
    

    在将关键字与变量或其他关键字分开时,空格是必要的。考虑以下示例。

    # Example
      
    x = [1, 2, 3]
    y = 2
      
    """ Following is incorrect, and will generate syntax error
    a = yin x
    """
      
    # Corrected version is written as
    a = y in x
    print(a)
    

    空格作为缩进
    Python 的语法很简单,但在编写代码时仍然需要小心。缩进用于编写Python代码。
    语句前的空格具有重要作用,用于缩进。语句前的空格可以有不同的含义。让我们尝试一个例子。

    # Example
      
    print('foo') # Correct
      
       print('foo') # This will generate an error
      
    # The error would be somewhat 'unexpected indent'
    

    前导空格用于确定语句的分组,例如循环或控制结构等。
    例子:

    # Example
      
    x = 10
      
    while(x != 0):  
     if(x > 5):   # Line 1
      print('x > 5')  # Line 2
     else:        # Line 3
      print('x < 5') # Line 4
     x -= 2       # Line 5
      
    """
    Lines 1, 3, 5 are on same level
    Line 2 will only be executed if if condition becomes true.
    Line 4 will only be executed if if condition becomes false.
    """
    
    输出:
    x > 5
    x > 5
    x > 5
    x < 5
    x < 5