2.1. Syntax Identifiers

2.1.1. Summary

  • Identifier is a formal name for variable

  • Variable can change it's value during the program

  • In Python there are no constants

  • NameError when using not declared variable

  • AttributeError when cannot assign to variables

Python variable names should use lowercase letters, underscores, and digits, with underscores for multi-word names. Camel and Pascal case are not used in Python, and numbers cannot be the first character in a variable name.

2.1.2. Variables

  • Variable can change it's value during the program

  • By convention you should use use Latin characters and English names

  • Use underscore _ for multi-word variable names

  • Names should use only lowercase letters, underscores and/or digits

  • Variable names are case sensitive

  • Variable names can have digits in it, but not as a first character

Identifiers are case sensitive. Use lowercase letters for variable names:

>>> name = 'Mark Watney'

By convention you should use use Latin characters and English names (Non-ASCII characters in an identifier):

>>> imie = 'Mark'
>>> imię = 'Mark'

Note, that word "imie" means first name in Polish language.

Underscore _ is used for multi-word names

>>> first_name = 'Mark'
>>> last_name = 'Watney'

You can also join words.

>>> firstname = 'Mark'
>>> lastname = 'Watney'

Although it works for two words, it could be hard to read for three or more:

You should always use lowercase letters:

>>> name = 'Mark Watney'
>>> Name = 'Mark Watney'

Capital letters by convention has different meaning. The code will run without errors or warnings, but you can mislead others. Remember code is read by 80% of a time, and written in 20%.

Not ok by convention :

>>> firstName = 'Mark'  # Camel Case - not used in Python
>>> Firstname = 'Mark'  # Pascal Case - reserved for class names
>>> FirstName = 'Mark'  # Pascal Case - reserved for class names

Camel Case convention is not used in Python. It is common in other programming language such as C / C++ / C# / Java / JavaScript.

You can put numbers in variables:

>>> name1 = 'Mark'
>>> name2 = 'Mark'

But the number cannot be the first character (otherwise will produce SyntaxError):

>>> 1name = 'Mark'
Traceback (most recent call last):
SyntaxError: invalid decimal literal

2.1.3. Constants

  • In Python there are no constants

  • Convention: identifiers with uppercase names should should not change value

Identifiers (variable/constant names) are case sensitive. Uppercase letters are used for constants (by convention):

>>> FILE = 'myfile.txt'
>>> FILENAME = 'myfile.txt'

Underscore _ is used for multi-word names:

>>> FILE_NAME = 'myfile.txt'

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 = 'Mark Watney'
>>> NAME = 'Melissa Lewis'

2.1.4. Constant vs Variable

Definition of second, minute or hour does not change based on location or country (those values should be constants).

>>> SECOND = 1
>>> MINUTE = 60 * SECOND
>>> HOUR = 60 * MINUTE

The definition of a workday, workweek, and workmonth differs based on location, and each country can have different work times (those values should be variables).

>>> workday = 8 * HOUR
>>> workweek = 40 * HOUR

2.1.5. Convention

  • Identifier names are case sensitive

  • Physical units should use case similar to their notation

Identifier names are case sensitive (three different variables):

>>> name = 'Mark Watney'  # convention for variables
>>> NAME = 'Mark Watney'  # convention for constants
>>> Name = 'Mark Watney'  # convention for class names

Note, that the only change was in variable names. As you can see, this could have a huge impact on describing the intent of what you want to achieve with the code.

Physical units should use case similar to their notation (Pa - Pascal, a unit of pressure in SI system):

>>> Pa = 1              # Pascal - 1 Pa = 1 N/m^2
>>> hPa = 100 * Pa      # Hecto Pascal - 1 hPa = 100 Pa
>>> kPa = 1000 * Pa     # Kilo Pascal - 1 kPa = 1,000 Pa
>>> MPa = 1000000 * Pa  # Mega Pascal - 1 MPa = 1,000,000 Pa

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.

2.1.6. Recap

  • Variable can change it's value during the program

  • "Constants" should not change during program

  • Python do not distinguish between variables and constants (it's only a convention)

  • Use underscore _ for multi-word variable names

  • Identifier names are case sensitive

  • By convention you should use use Latin characters and English names

>>> first_name = 'Mark'
>>> firstname = 'Mark'
>>> FILE = 'myfile.txt'
>>> FILENAME = 'myfile.txt'
>>> FILE_NAME = 'myfile.txt'

2.1.7. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Syntax Variables Variable
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define variable `result` with value 1
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wartością 1
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is int, \
'Variable `result` has invalid type, should be int'

>>> assert result == 1, \
'Variable `result` has invalid value, should be 1'

>>> from pprint import pprint
>>> pprint(result)
1
"""

# Define variable `result` with value 1
# type: int
result = ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Syntax Variables Constant
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Define "constant" `RESULT` with value 1
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj "stałą" `RESULT` z wartością 1
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert RESULT is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(RESULT) is int, \
'Variable `result` has invalid type, should be int'

>>> assert RESULT == 1, \
'Variable `result` has invalid value, should be 1'

>>> from pprint import pprint
>>> pprint(RESULT)
1
"""

# Define "constant" `RESULT` with value 1
# type: int
RESULT = ...