1.5. Python Syntax

1.5.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.2. Variable declaration
name = 'Mark Watney'

first_name = 'Mark'
last_name = 'Watney'

firstname = 'Mark'
lastname = 'Watney'
Listing 1.3. Variables vs. constants - Names are case sensitive
name = 'Mark Watney'        # lower cased names are reserved for variables
Name = 'Jan Twardowski'     # Capitalized names are reserved for classes

1.5.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 1.4. "Constant" declaration
FILE = '/etc/passwd'
FILE_NAME = '/etc/shadow'
FILENAME = '/etc/group'
Listing 1.5. Python allows you to change "constants" but it's a bad practice (good IDE will tell you)
NAME = 'Mark Watney'
NAME = 'Jan Twardowski'

1.5.3. Variables vs. Constants

Listing 1.6. Variables vs. constants - Names are case sensitive
name = 'Mark Watney'        # variable
NAME = 'Jan Twardowski'     # constant
Name = 'José Jiménez'       # class
Listing 1.7. Variables vs. constants - Definition of second, minute or hour does not change based on location or country (those values should be constants). Definition of workday, workweek and workmonth differs based on location - each country can have different work times (those values should be variables).
SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE

workday = 8 * HOUR
workweek = 5 * workday
workmonth = 4 * workweek
Listing 1.8. For physical units it is ok to use proper cased names. It is better to be compliant with well known standard, than to enforce something which will mislead everyone.
Pa = 1
hPa = 100 * Pa
kPa = 1000 * Pa
MPa = 1000 * kPa

1.5.4. Printing Values

  • Prints on the screen

  • f-string formatting for variable substitution

  • More information in Builtin Printing

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

print(name)
# José Jiménez

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

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

1.5.5. End of Lines

  • No semicolon (;) at the end of lines

  • PEP 8: Use \n for newline

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

1.5.6. Line Length

  • Most controversial rule

  • PEP 8: 79 for line with code

  • PEP 8: 72 for line with comment

  • black: 90-ish

  • django: 120 for code, 300 for models

1.5.7. 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 1.9. Line comments
# Mark thinks he is...
print('Mark Watney: Space Pirate')
Listing 1.10. Inline comments
print('Mark Watney: Space Pirate')  # This is who Mark Watney is

1.5.8. 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')

1.5.9. Good practices

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

1.5.10. Assignments

1.5.10.1. About Print

  • Complexity level: easy

  • Lines of code to write: 1 lines

  • Estimated time of completion: 3 min

  • Solution: solution/about_print.py

English
  1. Print "Hello World" on the screen

  2. "Hello" must be in a first line

  3. "Wold" must be in a second line

  4. Use only one print() function call

Polish
  1. Wypisz "Hello World" na ekranie

  2. "Hello" ma być w pierwszej linii

  3. "World" ma być w drugiej linii

  4. Użyj tylko jednego wywołania funkcji print()

The whys and wherefores
  • Wyświetlanie na ekranie

1.5.10.2. About Syntax

  • Complexity level: easy

  • Lines of code to write: 8 lines

  • Estimated time of completion: 3 min

  • Solution: solution/about_syntax.py

English
  1. Write a multiline comment with program description (todo from this assignments)

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

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

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

  5. Use f-string

Polish
  1. Napisz wieloliniowy komentarz z opisem programu (punkty do wykonania z tego zadania)

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

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

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

  5. Zastosuj f-string

The whys and wherefores
  • Tworzenie skryptów Python

  • Deklaracja zmiennych

  • Komentowanie kodu

  • Wyświetlanie wartości zmiennych