2.1. Python Syntax

2.1.1. Variables

  • Names are case sensitive

  • Lowercase letters for variable names

  • Underscore _ is used for multi-word names

  • NameError when using not declared variable

  • AttributeError when cannot assign to variables

Listing 1. Variable declaration
name = 'Mark Watney'
first_name = 'Mark'
last_name = 'Watney'
Listing 2. Variables vs. constants - Names are case sensitive
name = 'Mark Watney'
Name = 'Jan Twardowski'

2.1.2. Constants

  • Names are case sensitive

  • Underscore _ is used for multi-word names

  • Uppercase letters for "constant" names

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

Listing 3. "Constant" declaration
PATH = '/etc/passwd'
FILE_NAME = '/etc/shadow'
Listing 4. Python allows you to change "constants" but it's a bad practice (good IDE will tell you)
NAME = 'Mark Watney'
NAME = 'Jan Twardowski'
Listing 5. Variables vs. constants - Names are case sensitive
name = 'Mark Watney'
NAME = 'Jan Twardowski'
Name = 'José Jiménez'

2.1.3. Printing Values

  • Prints on the screen

  • f-string formatting for variable substitution

  • \n - Escape character for newline

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

  • 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.4. 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.5. Comments

  • PEP 8 for line comments: Hash (#), space and then comment

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

  • Commented out code:

    • Never!

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

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

Listing 6. Line comments
# Mark thinks he is...
print('Mark Watney: Space Pirate')
Listing 7. Inline comments
print('Mark Watney: Space Pirate')  # This is who Mark Watney is

2.1.6. Indentation

  • Python uses indentation instead of braces

  • Code indented on the same level belongs to block

  • PEP 8: 4 spaces indentation, no tabs

  • Python throws IndentationError exception on problem

if True:
    print('True statement, first line')
    print('True statement, second line')
else:
    print('Else statement, first line')
    print('Else statement, second line')
if True:
    print('Outer block, true statement, first line')
    print('Outer block, true statement, second line')

    if True:
        print('Inner block, true statement, first line')
        print('Inner block, true statement, second line')
    else:
        print('Inner block, else statement, fist line')
        print('Inner block, else statement, second line')

else:
    print('Outer block, else statement, first line')
    print('Outer block, else statement, second line')

2.1.7. Assignments

2.1.7.1. Pyton Syntax

  • Complexity level: easy

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

  • Estimated time of completion: 5 min

  • Solution: solution/syntax_python.py

English
  1. Create Python script

  2. At at top, add a multiline comment with program description (todo from this assignments)

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

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

  5. Print "Hello World NAME", where NAME is your name (variable name)

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

Polish
  1. Stwórz skrypt Python

  2. Na górze pliku dodaj wielolinijkowy komentarz z opisem programu (punkty do wykonia z tego zadania)

  3. Zadeklaruj zmienną name i ustaw jej wartość na Twoje imię

  4. Dodaj komentarz "inline" do zmiennej o treści: "This is my name"

  5. Wypisz "Hello World NAME", gdzie NAME to Twoje imię (zmienna name)

The whys and wherefores
  • Tworzenie skryptów Python

  • Deklaracja zmiennych

  • Komentowanie kodu

  • Wyświetlanie wartości zmiennych

Hint
  • print()