2.1. Syntax Identifiers

2.1.1. Summary

  • Identifier is case-sensitive name of a variable or a constant

  • Lowercase identifiers (variables) can change during the program

  • Uppercase identifiers (constants) should not change during program

  • In Python there are no constants - it's only a convention

  • NameError is raised when accessing undeclared identifier

  • AttributeError is raised when cannot assign to variables

  • Identifier can contain numbers, but not as a first character

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

  • In Python for multi-word identifiers use joined words or snake case

  • Camel case is not used in Python at all

  • Physical units should use case similar to their notation (kPa - kilo Pascal)

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 during the program

  • Variables should have lowercase names

Variables are used to store data in memory. You can change the value of a variable during the program. In Python, all variables should have lowercase names:

name = 'Alice'

2.1.3. Constants

  • Constants should not change during program

  • Constants should have uppercase names

  • In Python there are no constants - it's only a convention

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

FILE = 'myfile.txt'

2.1.4. Constant vs. Variable

  • Lowercase identifiers (variables) can change during the program

  • Uppercase identifiers (constants) should not change during program

  • In Python there are no constants - it's only a convention

Definition of second, minute or hour does not change based on location or country, therefore 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. Hence those values should be variables:

workday = 8 * HOUR
workweek = 40 * HOUR

2.1.5. Case Sensitivity

  • Identifier names are case sensitive

  • Lowercase words by convention are reserved for variables

  • Uppercase words by convention are reserved for constants

  • Capitalized words by convention are reserved for class names

  • Physical units should use case similar to their notation (kPa - kilo Pascal)

Identifier names are case sensitive:

name = 'Alice'  # convention for variables
NAME = 'Alice'  # convention for constants
Name = 'Alice'  # 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. Multi-word Identifiers

  • Conventions: joined words, snake case, camel case, pascal case

  • In Python for multi-word identifiers use joined words or snake case

  • Camel case is not used in Python at all

  • Pascal case is used for class names

In programming there are several conventions:

firstname = 'Alice'   # joined words
first_name = 'Alice'  # snake case
firstName = 'Alice'   # camel case
FirstName = 'Alice'   # pascal case

In Python we use either snake case or joined words.

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

Pascal case has different meaning in Python. It is used for class names. The name of this convention comes from the Pascal programming language, where it was used for all identifiers.

2.1.7. Identifiers with Numbers

  • Identifier can contain numbers

  • Identifier cannot start with a number

Identifier can contain numbers:

name1 = 'Alice'

Identifier cannot start with a number. In such case the SyntaxError exception will be raised:

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

2.1.8. Non-Ascii Characters

  • Identifier can contain non-ASCII characters (such as national letters and accents)

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

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

name = 'Alice'  # best
imie = 'Alice'  # ok
imię = 'Alice'  # bad

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

2.1.9. Recap

  • Identifier is case-sensitive name of a variable or a constant

  • Lowercase identifiers (variables) can change during the program

  • Uppercase identifiers (constants) should not change during program

  • In Python there are no constants - it's only a convention

  • NameError is raised when accessing undeclared identifier

  • AttributeError is raised when cannot assign to variables

  • Identifier can contain numbers, but not as a first character

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

  • In Python for multi-word identifiers use joined words or snake case

  • Camel case is not used in Python at all

  • Physical units should use case similar to their notation (kPa - kilo Pascal)

name = 'Alice'
FILE = 'myfile.txt'
first_name = 'Alice'
firstname = 'Alice'

2.1.10. Assignments

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

# %% 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

# %% 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ść

# %% Doctests
"""
>>> 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
"""

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

# %% Imports

# %% Types
result: int

# %% Data

# %% Result

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

# %% 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

# %% 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ść

# %% Doctests
"""
>>> 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
"""

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

# %% Imports

# %% Types
result: int

# %% Data

# %% Result