< previous | Table of Contents | next > |
2.2 CONSTANTS AND VARIABLES
VARIABLES
Variables are structures used to store data. We can define names and attach particular values to them. You can think of variables as containers we can fill with desired data. Variables can be different types and store text, numbers, or data structures. In Python, there is no need to specify the variable type explicitly. Hence, it's called a dynamically typed language. Once a variable is defined, you can use it in the rest of your program. You can adjust the value of a variable in the program, and Python will keep track of it. However, make sure that changing the value is intentional and necessary to what you’re trying to accomplish with your program; otherwise, the outcome might be unintended.
As you can see in the examples below, we don’t have to specify the type ourselves, as Python takes care of that for us regardless of what type of data we're dealing with.
# Assigning the string “Tallinn” to the the variable location location = "Tallinn" # Assigning the truth value of True to the variable raining_outside raining_outside = True # Assigning the integer value of 52 to the variable age age = 52
CONSTANTS
Constants are also variables, but as their name states, these values are supposed to never change throughout the program. Similarly to variables, constants can store different types of data from strings to integers to floating point numbers and more. Constant values are meant to be accessed but not changed. However, it’s important to note that technically, Python does not enforce anything as a constant, so even the constants that you define can be changed.
To avoid errors where constant values are accidentally altered, Python has adopted naming conventions for constants. According to the Python PEP 8 style guide, constant values are meant to be all uppercase and separated with underscores if the intended constant name consists of multiple words.
E = 2.71828 PI = 3.14 LENGTH = 100 WIDTH = 50 LOCAL_MINIMUM = 0.5
VARIABLE NAMING CONVENTIONS
To write clear and widely understandable code, Python has adopted a set of conventions that guide how variables should be named. The Python PEP 8 style guide outlines the naming conventions that the Python community should adhere to.
To make variables easy to understand for everyone who interacts with the code base, including yourself, you should make sure that the names are clear and indicative of the contents or purpose of the variable. That being said, the variable names should not be too long.
As mentioned above, there is no built-in way to declare a variable as a constant. Still, a collectively agreed-upon notion is to store constants with all uppercase variable names and using underscores if the constant name consists of multiple words.
Here are examples of some descriptive and valid variable names.
ORDER_ID = 756 character_health = 96 username = "TulnukasValdis"
Also, here are all the ways you should not name your variables, as these would be considered incorrect and using them would result in a SyntaxError
.
# Words should be separated by underscores, not spaces user name = "TulnukasValdis" # Variable names cannot start with numbers 1username = "PeeterPaan123" # Words should be separated by underscores, not hyphens user-name = "Shrek"
To illustrate what would happen if you used an incorrect variable name, here's a screenshot of Thonny.
When variable names consist of multiple words, readability can become challenging. Several naming convention cases that are commonly used to enhance clarity have been developed. There are three naming conventions for variables in Python: the Snake Case
, Pascal Case
, and Camel Case
. For variables and functions, Python's style guide recommends using the Snake Case
, meaning all words in lowercase and separated by underscores, for optimal readability.
One more important thing to mention is that Python strongly advises against using the characters l
(lowercase letter L), O
(uppercase letter o), and I
(uppercase letter i) as for naming since they can be indistinguishable from each other or numerals one and zero.
CAMEL CASE
The words are joined together without spaces between them; the first word is all lowercase, and all the first letters of all subsequent words are in uppercase.
birthDate = "24th February"
PASCAL CASE
Similarly, the words are joined together without spaces between them. However, now every first letter of every word is capitalized, including the first word.
BirthDate = "15th March"
SNAKE CASE
By contrast to the first two, in the snake case, all the words are in lowercase, and different words are joined together by an underscore. This is the preferred naming convention in Python.
birth_date = "1st January"
KEY THINGS TO REMEMBER
- Python is a dynamically typed language, meaning that you don't have to explicitly declare variable types.
- Variable names should be descriptive enough to clarify the variable's purpose.
- Constant names should be all uppercase, signaling that these values must not be changed.
- Snake Case, where words are all lowercase and separated by underscores, is Python's preferred naming convention for functions and variables.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next > |