1.6. Python Syntax

1.6.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'
Name = 'Jan Twardowski'

1.6.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'
Listing 1.6. Variables vs. constants - Names are case sensitive
name = 'Mark Watney'
NAME = 'Jan Twardowski'
Name = 'José Jiménez'

1.6.3. 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('My name... {name}')
# My name... {name}

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

1.6.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?

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

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

1.6.7. Assignments

1.6.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/about_syntax.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)

Polish
  1. Stwórz skrypt Python

  2. Na górze pliku dodaj wieloliniowy komentarz z opisem programu (punkty do wykonania 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()