3.2. Python Syntax

3.2.1. Indentation instead of braces

  • Python uses indentation instead of braces

  • 4 spaces indentation, no tabs

  • Python throws IndentationError exception on problem

  • Code indented on the same level belongs to block:

if True:
    print('First line of the true statement')
    print('Second line of the true statement')
    print('Third line of the true statement')
else:
    print('This is false')

3.2.2. End of lines

  • No semicolon (;) at the end of lines

  • \r\n and \n are good:

print('Hello!\nHow are you?')
print('Hello!\r\nHow are you?')

3.2.3. Comments

3.2.3.1. Line comments

  • Indent must be on the same level as block indent

  • Hash (#), space and then comment:

# José Jiménez says hello
print('My name... José Jiménez')

3.2.3.2. Inline comments

  • Source code, two spaces, hash (#), space and then comment:

print('My name... José Jiménez')  # José Jiménez says hello

3.2.3.3. Multiline comments

  • Triple single quotes '''

  • Triple double quotes """ (more common)

  • Both ''' and """ quotes works the same

  • if assigned to variable, it serves as multiline str

"""
We choose to go to the Moon!
We choose to go to the Moon in this decade and do the other things,
not because they are easy, but because they are hard;
because that goal will serve to organize and measure the best of our energies and skills,
because that challenge is one that we are willing to accept, one we are unwilling to postpone,
and one we intend to win, and the others, too.
"""

3.2.3.4. Docstring

  • Docstring is a first multiline comment in:

    • File / Module

    • Class

    • Method / Function

  • It is accessible in __doc__ property of an object

  • Used for generating help() documentation

  • Used for doctest

Listing 3.92. Docstring used for documentation
def apollo_dsky(noun, verb):
    """
    This is the Apollo Display Keyboard
    It takes noun and verb
    """
    print(f'Program selected. Noun: {noun}, verb: {verb}')
Listing 3.93. Docstring used for doctest
def add(a, b):
    """
    Sums two numbers.

    >>> add(1, 2)
    3
    >>> add(-1, 1)
    0
    """
    return a + b

3.2.4. Variables and constants

  • NameError when using not declared variable

  • AttributeError when cannot assign to variables

  • Names are case sensitive

name = 'José Jiménez'
NAME = 'Иван Иванович'
Name = 'Jan Twardowski'

3.2.4.1. Variable declaration

  • Lowercase letters for variable names

  • Underscore _ is used for multi-word names

name = 'José Jiménez'
first_name = 'José'
last_name = 'Jiménez'

3.2.4.2. Constant declaration

  • Uppercase letters for constants names

  • Underscore _ is used for multi-word names

PATH = '/etc/passwd'
FILE_NAME = '/etc/shadow'

3.2.4.3. Variables vs. constants

  • Names are case sensitive

  • Python do not distinguish between variables and constants

  • Python allows you to change "constants" but it's a bad practice (good IDE will tell you)

name = 'José Jiménez'
NAME = 'Иван Иванович'
Name = 'Jan Twardowski'
NAME = 'José Jiménez'
NAME = 'Иван Иванович'

3.2.5. print()

  • print() adds '\n' at the end

  • Prints on the screen

  • Variable substitution

  • Special characters

  • More in Builtin Printing

print('My name... José Jiménez')
# My name... José Jiménez
name = 'José Jiménez'


print('My name... {name}')
# My name... {name}

print(f'My name... {name}')
# My name... José Jiménez

print(f'My name...\n\t{name}')
# My name...
#     José Jiménez