Variables
Python Basics • Last Updated: 01/12/2025
Python Basics • Last Updated: 01/12/2025
Variables in Python are really important. They can store variables, and can be manipulated as well. Variables in Python can hold multiple values, and these types of variables are called 'tables', or more commonly, 'lists'. Variables are extremely fundamental and useful when coding in Python as a whole.
We use variables in Python to store data in a convenient form. This form allows us to refer to it multiple times, and useful for values that aren't constant. What are constant values? Well, they are values that cannot be changed.
Variables in Python have a strict naming logic and are case-insensitive. That means that variables like abc and ABC are considered different, so that means as long as the caps are different, the original value won't be replaced. This is something to look out for, however, as you can't assume that abc will automatically hold the value of ABC, which it won't, but issues like these can be easily avoided.
When naming variables in Python, look out for the following:
Only A-Z, a-z, 0-9, '. ', and '_' are allowed, i.e. letters, numbers, full-stops, and underscores only.
Caps don't matter. (stated above).
Spaces are not allowed as they will break the variable name.
Variables names cannot start with a symbol or a number, (unless it's an underscore).
Underscores at the start means the variable is temporary. If a variable is temporary, you cannot use it again as Python will see it as undefined if you try to refer to it.
Any symbols apart from underscores (_) or full-stops (.) are not allowed in Python syntax and are therefore invalid.
When you set a value to a variable, you simply just put an equal's sign in between the two parts; the left side is the variable name (rules here), and the right side is the desired value you would like to put in.
When you want to set the value to some text, don't make the mistake of not placing it within quotations. If you set the value to a string without the quotations, then Python will assume that it is the value contained in another variable, but not text.
Booleans are just a fancy way of saying 'true' or 'false' and then assigning that to a variable (for example). They are also how if-statements work, more on that covered here. Syntax on Booleans are very specific, requiring them to be either True or False as their value. This is specific as it has to be exactly that and starting with a capital letter.
This is an important aspect of Python string manipulation. F' Strings are essentially used everywhere strings can be involved, as they make it extremely easy to embed variable data into your string. The way you'd structure it is like this:
print(f"Hello, {name}!")
The logic of that simply implies that there's a defined variable named "name", most likely set by an input() command. Then the F" String outputs "Hello,", followed by whatever is set by the "name" variable. For example, if the variable held the string "Alice", then it would output: "Hello, Alice!". If it was set to "Bob", then it would be: "Hello, Bob!". But those are just examples of how f"strings work. You can use them in many ways.
To add variable data to your string, the easiest solution is to wrap the variable name in some curly brackets {}. Python will look at it and grab the value stored in the variable and add it to your string in place of the variable name and brackets.
Variables in Python are a very simple concept. Naming them is not case-sensitive, however you can only use letters, numbers, full-stops, and underscores. Variables can hold number values, string values, and booleans, and can be manipulated using operations.
If you have any questions, please head to the comments section below!
Share your opinions or ask questions.
-