Skip to content
Learnearn.uk » Home » Constants & Variables

Constants & Variables

Introduction

Constants & Variables

When we want to write computer programs we often need to store simple pieces of data temporarily in the program for further use. We do this using a combination of constants and variables, using the format name = value.

Constants

Whenever we store data that doesn’t change we store it in a constant, usually designated in Python as all capitals.

For example if we want to store the value of Pi, we can create a PI constant.

PI = 3.142

Variables

If we want to store the value of some data that might change, we use a similar system, but instead we use lowercase letters.

player_name = "Bob"

It is important to note that Python and many other programming languages don’t recognise the difference between variables and constants and so they will let you change a constant, even if you shouldn’t!

Naming Conventions

Python Naming Conventions

nI Python, following naming conventions is important for writing clean and readable code. Here are some commonly accepted naming conventions in Python:

Variable Names

Use lowercase letters and separate words with underscores to improve readability. For example:

my_variable = 10
user_name = "John"

Function Names

Use lowercase letters and separate words with underscores for function names as well. For example:

def calculate_average(numbers_list):
    pass

Constant Names

Use uppercase letters and separate words with underscores for constants, which are values that do not change. For example:

MAX_VALUE = 100
PI = 3.14

Class Names

Use CamelCase, starting with an uppercase letter for class names. For example:

class MotorCar:
    pass

Module Names

Use lowercase letters and separate words with underscores for module names. For example, if you have a module that performs mathematical operations, you could name it math_operations.py.

Private Names

Use a single leading underscore to indicate that a variable or function is intended to be private, meaning it should not be accessed or modified from outside the class or module. For example:

_private_variable = 10
def _private_function():
    pass

Remember that these conventions are not strict rules enforced by the Python language itself, but they are widely followed in the Python community. Consistently applying these naming conventions will make your code more readable and make it easier for others to understand and collaborate on your code.

Reserved Words

Python Reserved Words

Reserved words, also known as keywords, are words that are predefined and reserved by the programming language for specific purposes. In Python, you cannot use these reserved words as variable names, function names, or any other identifiers because they have a special meaning in the language. Here are the reserved words in Python:

 

Python Reserved Words
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

 

Resources