1.1. Basic Syntax

1.1.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')
        print('This is false')

1.1.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?')

1.1.3. Comments 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') Inline comments

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

    print('My name... José Jiménez')  # José Jiménez says hello 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.
    """ 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

    def apollo_dsky(noun, verb):
        This is the Apollo Display Keyboard
        It takes noun and verb
        print(f'Program selected. Noun: {noun}, verb: {verb}')
  • Used for doctest

    def add(a, b):
        Sums two numbers.
        >>> add(1, 2)
        >>> add(-1, 1)
        return a + b Commented out code

  • Never!

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

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

1.1.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' Variable declaration

  • Lowercase letters for variable names

    name = 'José Jiménez'
  • Underscore _ is used for multi-word names

    first_name = 'José'
    last_name = 'Jiménez' Constant declaration

  • Uppercase letters for constants names

    PATH = '/etc/passwd'
  • Underscore _ is used for multi-word names

    FILE_NAME = '/etc/shadow' Variables vs. constants

  • Names are case sensitive

    name = 'José Jiménez'
    NAME = 'Иван Иванович'
    Name = 'Jan Twardowski'
  • 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 = 'Иван Иванович'

1.1.5. print()

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

  • Prints on the screen

  • Variable substitution

  • Special characters

  • More in Print Formatting

    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

1.1.6. Type Annotations

Types are not required, and never will be -- Guido van Rossum, Python BDFL

  • Since Python 3.5

  • SyntaxError in Python before 3.5

  • Sometimes called "type hints"

  • Good IDE will give you hints

  • Types are used extensively in system libraries

  • More and more books and documentations use types

  • To type check use: mypy or pyre-check (more in CI/CD Tools) Basic types

name: str = 'Jan Twardowski'
age: int = 30
is_adult: bool = True Types do not enforce checking

  • This code will run without any problems

  • Although mypy or pyre-check will throw error

name: int = 'Jan Twardowski'
age: float = 30
is_adult: int = True Why?

  • Good IDE will highlight, incorrect types

def sum_numbers(a: int, b: float) -> int:
    return int(a + b)

sumuj_liczby(1, 2.5)
sumuj_liczby('a', 'b') More advanced topics


The topic will be continued in chapter: Type Annotation

1.1.7. Assignments 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

  • print()