Structuring Python Programs



Python Statements In general, the interpreter reads and executes statements line by line, i.e. consistently. However, there are some operators that can change this behavior, like conditional operators.
Basically, python statements are written in such a format that one statement is written on only one line. The interpreter treats a newline character as a terminator for one instruction. But it is also possible to write multiple statements per line, which you can find below.
Examples:

# Example 1

  

print ( `Welcome to Geeks for Geeks`

Output:

 Welcome to Geeks for Geeks 

# Example 2

 

x = [ 1 , 2 , 3 , 4 ]

  
# x [1: 3] means start at index
# 1 and go to index 2

print (x [ 1 : 3 ]) 

 
& quot; & quot; & quot; In the above format, the first
index is included, but the last index is not
. "" "

Exit:

 [2, 3] 

Several operators per line We can also write multiple operators per line, but this is not a good practice as it reduces the readability of the code. Try not to write multiple statements on one line. But still, you can write multiple lines by completing one statement with & # 39 ;; & # 39 ;. & # 39 ;; & # 39; in this case it is used as a terminator of one operator.
For example, consider the following code.

Exit :

 10 20 30 

Continue line to avoid scrolling left and right
Some statements can get very long and can cause you to frequently scroll left and right. You can place your code in such a way that you don`t have to loop through it here and there. Python allows you to write one statement on multiple lines, also called line continuation. Line continuation also improves readability.

 # 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 & amp; & amp; no_of_female_students == 30 & amp; & amp; no_of_male_students == 20 & amp; & ) == 30): print (`The course is valid`) 

Types of line continuation
In general, there are two types of line continuation

  • Implicit line continuation
    This is the simplest method of writing a statement that spans multiple lines.
    Any statement containing opening brackets (& # 39; (& # 39;), braces (& # 39; [& # 39;), or curly braces (& # 39; {& # 39; )) is incomplete until all matching parentheses, square brackets, and curly braces are found. the statement can be implicitly continued through the lines without generating an error.
    Examples:

# Example

  

a = 10 ; b = 20 ; c = b + a

  

print (a); print (b); print (c)

# Example 1

 
# The following code is valid

a = [

[ 1 , 2 , 3 ],

[ 3 , 4 , 5 ],

[ 5 , 6 , 7 ]

]

 

print (a)

Exit:

 [[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 & gt; = 18 and

person_2 & gt; = 18 and

person_3 & lt; 18

):

print ( `2 Persons should have ID Cards` )

Exit :

 2 Persons should have ID Cards 
  • Explicit line continuation
    Explicit line concatenation is mainly used when implicit line concatenation is not applicable. In this method, you must use a character to help the interpreter understand that a particular statement spans more than one line.
    The backslash (/) is used to indicate that a statement spans more than one line. It should be noted that “must be the last character on this line, not even a space is allowed.
    See the following example for clarification

    # Example

     

    x =

    1 + 2

    + 5 + 6

    + 10

     

    print (x)

    Exit:

     24 

    Comments in Python
    Writing comments in your code is very important, they help in the readability of the code, and they also tell you more about the code. This will help you write details versus a statement or piece of code. The interpreter ignores comments and does not count them in commands. In this section, we will learn how to write comments in Python.
    Characters used to write comments include hash (#) or triple double quotes (""). The hash is used when writing single line comments that do not span multiple lines. Triple quotes are used to write multi-line comments. Three triple quotes to start a comment and three more quotes to end a comment.
    Consider the following examples:

    # Example 1

     
    ####### This example will print Hello World ####### print (& # 39; Hello World & # 39;) # This comment

    # Example 2

     
    & quot; & quot; & quot; This example will demonstrate

    multiple comments & quot; & quot; & quot;

     
    & quot; & quot; & quot; Next

    variable contains

    String & # 39; How old are you? & # 39;

    “ »»

    a = `How old are you? `

      
    & quot; & quot; & quot; The following statement prints

    what`s inside the variable a

    "" "

    print (a)

    Note. Note that the hash (#) inside a line does not make it a comment. Consider the following example for demonstration.

    Spaces
    The most common spaces are:

    # Example

     
    & quot; & quot; & quot; The following statement prints the saved string

    in the variable & quot; & quot; & quot;

     

    a = `This is # not a comment #`

    print (a) # Prints the string stored in

    Character ASCII Code Literal Expression
    Space 32 (0x20) ` `
    tab 9 (0x9)
    newline 10 (0xA)

    * You can always refer to the ASCII Table by clicking here .

    Whitespace is mostly ignored and, for the most part, is not required by the Python interpreter. When it becomes clear where one token ends and the next begins, the space can be omitted. This is usually the case when special non-alphanumeric characters are used.
    Examples:

    # Example 1

      
    # That`s correct, but whitespace can improve readability

     

    a = 1 - 2   # The best way is = 1 - 2

      

    print (a)

    # Example 2

     
    # That`s right
    # Try they can improve readability here.

    x = 10

    flag = (x = = 10 ) and (x & lt; 12 )

    print (flag)

     
    "" "The readable form may be as follows
    x = 10
    flag = (x == 10) and (x & lt; 12)
    print (flag)
    "" "

      
    # Try more readable code yourself

    Spaces are required to separate keywords from variables or other keywords. Consider the following example.

    # Example

     

    x = [ 1 , 2 , 3 ]

    y = 2

     
    & quot; & quot; & quot; Following is incorrect and will result in a syntax error
    a = yin x
    "" "

     
    # The revised version is written as

    a = y in x

    print (a)

    Spaces as indentation
    Python Syntax pretty simple, but still you have to be careful when writing your code. Indentation is used when writing Python code.
    The spaces before the operator are important and are used in indentation. The spaces before the statement can have a different meaning. Let`s try an example.

    # Example

     

    print ( `foo` ) # Correct

     

    print ( `foo` ) # This will generate an error

     
    # The error will be somewhat "unexpected indentation"

    Leading spaces are used to define grouping of statements such as loops or control structures, etc.
    Example:

    # Example

     

    x = 10

     

    while (x! = 0 ): 

    if (x & gt; 5 ):  # Line 1

    print ( `x & gt; 5` # Line 2

      else # Line 3

      print ( `x & lt; 5` ) # Line 4

    x - = 2   # Line 5

     
    "" "
    Lines 1, 3, 5 are at the same level
    Line 2 will only be executed if the condition is true.
    Line 4 will only be executed if the condition becomes false.
    "" "

    Exit :

     x & gt; 5 x & gt; 5 x & gt; 5 x & lt; 5 x & lt; 5