1.2.1.6 PCEP-30-02 Practice Test Compendium – Variables, assignments, comments

Variables

01) A variable is a named container able to store data.

02) A variable's name can consist of:

  • letters (including non-Latin ones)

  • digits

  • underscores (_)

and must start with a letter (note: underscores count as letters). Upper- and lower-case letters are treated as different.

03) Variable names which start with underscores play a specific role in Python – don't use them unless you know what you're doing.

04) Variable names are not limited in length.

05) The name of the variable must not be any of Python's keywords (also known as reserved words). The complete list of Python 3.8 keywords looks as follows:

'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'

* Note: Python 3.9 introduced a new keyword, __peg_parser__, which is an easter egg related to the rollout of a new PEG parser for CPython. You can read more about this in PEP 617. The keyword will most probably be removed in Python 3.10.

06) These are some legal, but not necessarily the best, Python variable names:

  • i

  • counter_2

  • _

  • Tax22

The assignment operator

07) The assignment operator = is designed to assign a value to a variable:

variable = expression

For example:

counter = 0
pi2 = 3.1415 ** 2

or – when more than one variable is assigned with the same value:

variable_1 = variable_2 = ... = expression

For example:

counter = stages = 0

08) A variable must be assigned (defined) before its first use – using a variable without prior definition (assignment) raises the NameError exception.

09) Python is a dynamically typed language, which means that a variable can freely change its type according to the last assignment it took part in.

10) There are some short-cut (compound) operators which simplify certain kinds of assignments.

Last updated