2.1. Python Syntax

2.1.1. Indentation

  • Python uses indentation instead of braces

  • PEP 8: 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')

2.1.2. End of Lines

  • No semicolon (;) at the end of lines

  • \r\n and \n works

  • PEP 8: Use \n

>>> print('Hello!\nHow are you?')
Hello!
How are you?

2.1.3. Comments

  • Indent must be on the same level as block indent

  • PEP 8 - Line comments: Hash (#), space and then comment

  • PEP 8 - Inline comments: code, two spaces, hash (#), space and then comment

  • PEP 8 - Multiline comments: If assigned to variable, it serves as multiline str

  • Commented out code:

    • Never!

    • Use Version Control System instead - e.g.: git blame

    • IDE has Local history (modifications) and you can compare file

Listing 1. Line comments
# José Jiménez says hello
print('My name... José Jiménez')
Listing 2. Inline comments
print('My name... José Jiménez')  # José Jiménez says hello
Listing 3. Multiline comments
"""
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.
"""

2.1.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

  • PEP 257 Docstring convention - For multiline always use three double quote (""") characters

Listing 4. 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 5. Docstring used for doctest
def add(a, b):
    """
    Sums two numbers.

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

2.1.5. Variables and Constants

  • NameError when using not declared variable

  • AttributeError when cannot assign to variables

  • 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)

  • Lowercase letters for variable names

  • Uppercase letters for "constant" names

  • Underscore _ is used for multi-word names

Listing 6. Variable declaration
name = 'José Jiménez'
first_name = 'José'
last_name = 'Jiménez'
Listing 7. "Constant" declaration
PATH = '/etc/passwd'
FILE_NAME = '/etc/shadow'
Listing 8. Variables vs. constants - Names are case sensitive
name = 'José Jiménez'
NAME = 'Иван Иванович'
Name = 'Jan Twardowski'
Listing 9. Python allows you to change "constants" but it's a bad practice (good IDE will tell you)
NAME = 'José Jiménez'
NAME = 'Иван Иванович'

2.1.6. Printing Values

  • 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

2.1.7. Assignments

2.1.7.1. Meet Python

  • Complexity level: easy

  • Lines of code to write: 2 lines + 2 lines of comment

  • Estimated time of completion: 5 min

  • Filename: solution/syntax_python.py

  1. Create Python script

  2. Add interpreter declaration

  3. Under interpreter declaration add multiline comment with program description (copy-paste from book)

  4. Declare variable name and set its value to your name

  5. Add inline comment to variable declaration with text: "This is my name"

  6. Print variable

  7. Next line under print statement add line comment, with expected output

  8. Run script

The whys and wherefores
  • Tworzenie skryptów Python

  • Deklaracja zmiennych

  • Komentowanie kodu

  • Wyświetlanie wartości zmiennych

Hint
  • print()