Python Variables
A Python variable is a symbolic name that refers to a value stored in the computer’s memory. You can think of it like a labeled container that holds information, similar to a box with a name on it.
Variables are used to store data, which can be of various types, such as numbers, strings, lists, dictionaries, and more.
Creating Variables
You create a variable by assigning a value to it using the assignment operator =
.
For example:
number = 10
In this example,
number
is the variable name.10
is the value assigned tonumber
.
Dynamic Typing
Python is dynamically typed, meaning you don’t need to declare the type of the variable. The type is inferred when a value is assigned.
For example:
x = 10 # x is an integer
y = "hello" # y is a string
Variable Naming Conventions and Rules
When naming variables in Python, it’s essential to follow specific rules and conventions. Violating these rules can lead to syntax errors while following the conventions makes the code easier to read and maintain.
Here’s a breakdown of those rules and conventions:
(1) Naming Rules (Syntax Rules)
- Variable names must start with a letter (a-z, A-Z) or an underscore (_) but not with a number. For example,
person1
is a valid variable name, while1person
is not. - Variable names can contain only letters (a-z, A-Z), numbers(0-9), and underscores(_). For example,
person
,person1
,person_name
- Variable names are case-sensitive. For example,
age
,Age
, andAGE
are three different variables. - Spaces are not allowed in variable names but underscores can be used to separate words. For example,
person_name
is a valid variable name, whereasperson name
will result in an error. - Reserved words (keywords) cannot be used as variable names. For example, you cannot name a variable
class
,for
, orprint
, since they are reserved.
(2) Naming Conventions (Best Practices)
- Use descriptive names that reflect the purpose of the variable. For example,
user_age
is better thanx
. - Use an underscore to separate words in variable names (
snake_case
), following the PEP 8 convention. For example,user_age
instead ofuserAge
(Camelcase is not preferred in Python). - Private variables can be indicated by starting the variable name with a single underscore (_). For example,
_user_address
- Variables meant to be “strongly private” (internal use only) can be prefixed with a double underscore (__). For example,
__db_url
- Constants are typically written in all uppercase letters. For example,
PI
,MAX_SIZE
Variable Reassignments
You can reassign variables in Python freely, even changing the type of the value.
For example:
x = 10 # x is an integer
x = "ten" # x is now a string
Multiple Variable Assignments
In Python, you can assign values to multiple variables in a single line.
For example:
a, b, c = 1, 2, 3
You can also assign the same value to multiple variables in a single line.
For example:
a = b = c = 5
Type Checking of a Variable
You can get the data type of a variable using the type()
function.
For example:
x = 10
y = "James"
print(type(x))
print(type(y))
Output:
<class 'int'>
<class 'str'>
Constants in Python
In Python, constants are variables whose values are intended to remain unchanged throughout the execution of the program. Unlike some other languages, Python doesn’t have a built-in constant type. However, by convention, constants are typically written in all uppercase letters with underscores separating words.
For example:
PI = 3.14159
GRAVITY = 9.81
MAX_CONNECTIONS = 100
Python doesn’t enforce immutability, meaning you can technically change the value of a constant, but by convention, you should avoid doing so.