Content:
Python is a popular programming language, which is widely used in fields such as data science. It eschews many of the more complex concepts used in other languages and is often recommended for new programmers due to the simplicity of the code syntax.
Variables form a fundamental part of a programming language, and Python is no different. Variables allow you store and manipulate data in your application.
This guide will explore the basics of Python variables and show how to use them effectively in your applications.
Creating a Variable
The first step to using a variable is to create it. There are a few rules for variable naming which must be followed:
- Variable names must start with a letter or an underscore
- They can contain letters, numbers, and underscores
- Avoid using reserved keywords as variable names
Unlike some languages, Python is dynamically-typed. This means you do not need to specify a data type when creating a variable. When the variable is created, the data type is managed automatically by the Python interpreter.
Assigning a value to a variable is as simple as
x = 2
No data type is required.
It would be a little silly to have an entire section just for that one line of code, and there is more complexity when working with static variables.
It’s possible to create static variables, by declaring them at the top level of a class. Static variables created this way are different to instance variables, and the same variable name can be used to hold two different values.
class Test:
x = 2
t = Test()
t.x = 5
print(Test.x) # 2
print(t.x) # 5
This is something to be careful of, and it’s therefore recommended to ensure your static functions are named appropriately.
Variable Scope
There are two main types of variable scope in Python. A variable can be defined globally (meaning any part of your code can access it), or locally. Local variables are specific to the function/class where they are defined.
It’s generally recommended to avoid global variables where possible. Using local variables provides greater clarity on the purpose of the variable, while also reducing the risk of conflicts
There are additional rules where functions are nested within other functions, though this is a scenario which you are less likely to encounter.
To learn more about Python variable scope, check out our article on the subject.
Variable Scope In Python
Variable Accessibility
Unlike many other object-oriented programming languages, Python does not apply accessibility rules to variables. It is not possible to create a private variable.
Instead, you will sometimes encounter variable names prefixed with an underscore.
class Test:
_x = 5 # Intended to be private
While this does not alter the function of the code, it is common to use the underscore prefix to denote that a variable should not be altered externally. It’s essentially an unofficial way of warning users that the variable is intended to be private.
It’s a good idea to follow this convention where possible.
Data Types
Data types are not something you’ll need to worry about too much, due to Python using dynamic-typing. That said, it’s useful to understand some of the more complex data types (such as lists), which you will have to define yourself.
The basic data types are as follows:
- Integers (int): Whole numbers without a fractional part.
- Floating-Point Numbers (float): Numbers with a fractional part.
- Strings (str): Textual data enclosed in single or double quotes.
- Booleans (bool): Represents either True or False.
These are pretty self-explanatory, and are similar to the data types used in many other programming languages.
For holding collections of values, the following types are available:
- List (list): Array-like structure containing indexed values
- Dictionary (dict): JSON-like structure of key-value pairs
- Tuple (tuple): Similar to a list, but with immutable values
- Set (set): Similar to a list, but containing only unique values
Lists and dictionaries are the most commonly used data types, with the tuple and set adding additional constraints.
A tuple is totally immutable – no changes can be made once the tuple has been initialised. A set, meanwhile, does not allow value changes, but does allow new values to be added. It also only allows a single copy of a value to exist. A list does not have any of these restrictions.
For a JSON-like data structure, you’ll need to use a dictionary.
There is a special data type, called the NoneType
, which is used to represent no value. It is returned by functions that have no explicit return value.
This covers the main set of data types you’ll encounter when using Python.
Conclusion
Variables are an essential part of any programming language, including Python. They allow you to store and manipulate data, making your code more dynamic and flexible. In this tutorial, we covered the basics of Python variables, including variable assignment, data types, scope, and best practices. With this knowledge, you can now start using variables effectively in your Python programs.