top of page

Python Variables

  • Writer: SimpleSyntax
    SimpleSyntax
  • Feb 22, 2023
  • 4 min read

Just like in mathematics, programming also uses variables. Variables create a space in the computers memory to hold a piece of data that we can use in our programs. In keeping with the theme of Simple Syntax, an easier way to think of a variable is a box which you can keep a piece of information in.

A variable could be a number, a name, address, contact details, data for a game or many other things.

To keep it basic, we will use some datatypes we learned from the last post to use with some variables.


STRINGS

A string was covered in the last post as letters, characters or a sentence. So we can store a specific string in our variable, or "box".

(Note on naming variables. You cannot use spaces. You can use numbers, letters, or underscores ( _ ). Also, some words are reserved by Python, such as the word print, try to avoid using these reserved words. You can look them up in the Python documentation.


my_string = "this is a string variable"

Above we have created a variable and assigned some data. The variable is named my_string, and the data is of the string datatype as it is surrounded by quotation marks, which is "this is a string variable"


We can print this string using the print function

print(my_string)



We can manipulate the string variable using f-strings in Python, allowing for some formatting.

You need to put an f inside the print function parenthesis () , surrounded my quotation marks, your variable must then be inside two curly brackets {}

We can now manipulate the string to be printed in all capitals

Such as....

print(f"{my_string.title()}")

Some others we can do are printing in all uppercase using the upper() function

print(f"{my_string.upper()}")

and lowercase with the lower() function

print(f"{my_string.lower()}")

Note: Remember that printing f-strings are slightly more complicated than the normal print function. Think of it from the outside in to remember the syntax, ( f " { } " ), parentheses', f (on the left only), quotation marks, then curly brackets. After a few times you should memorize it fairly quickly.


IMPORTANT! f-strings were introduced in Python 3.6. Before this you would have to use the format() function. I strongly recommend updating to version 3.6 if you have not to keep things standard and up to date.


Some functions have been used here which have not been covered yet but will be later. Functions are generally denoted by the (). E.g., .upper() is a function. A block of code that has been predefined to make letters uppercase. Creating your own functions will be covered in a later post.


NUMBERS

We will cover using int and float variables for numbers as these are very common and help keep things simple. When using numbers, it is common that operations (addition, multiplication, subtraction and division) are used. Some of the symbols you are used to are different in programming operations though, below is a list for these four.


+ is for addition

- is for subtraction

* is for multiplication (not x)

/ is for division (not ÷)


Assigning a number variable is like before, but we must not used the quotation marks. If we use quotation marks ( "" ), Python will see it as a string, and math operations cannot be performed on string datatypes. In simple terms, we perform math operations on numbers, not letters (unless algebra, but keep things simple like always)


Here is an example of an int (integer) variable

int_var = 8


Here is an example of a float variable (decimal)

float_var = 4.574


We can also assign multiple values to a variable at once. Remember that the order the variables appear in corresponds to the order of the values you create. Separate each variable with a comma ( , ). So var_1 = 7, var_2, =9, and var_3 = 10


var_1,var_2,var_3 = 7,9,10


we can now perform a math operation, and make the result it's own variable. For example:


sum = var_1 + var_2

(var_1 is 7 and var_2 is 9. So 7+9 equals 16). As we created the variable "sum" and performed the operation to the left of the equal sign, the result is stored in the variable "sum". In simple terms, sum = 16


The = symbol in programming can be confusing at first, but it can make it more simple to understand by using the word "is". Such as, sum is 16, or int_var is 8, or var_1 is 7.


At times, programming used two equals symbols ( == ). This carries an entirely different meaning. The two == is a comparison operation, it is often used to check if two values are the same. You don't need to worry about this right now however.


Below are some examples of using number based variables (int and float) with some operations

This covers some basics on variables. try practice using them yourself. Programming becomes much easier with practice. It builds your memory and understanding. Simply reading and watching will not help you very much. The way in which variables can be used does get more complex, but the point here is to start simple and build up.


Summary


A variable is a "box" to store information in.


The standard 4 operation symbols are + for addition, - for subtraction, * for multiplation, and / for divsion.


You can print variables using print(variable_name)


You can format your variable printing by using f strings


Practice using f strings so you can remember the syntax


( f " { } " ), parentheses', f (on the left only), quotation marks, then curly brackets.

E.g.,

print(f"{my_string.title()}")


There will be a Python file added to the Github repository if you get stuck or get any errors when practicing. It can be found here https://github.com/SimpleSyntax/SimpleSyntax . The Python file is called variables.py









 
 
 

Comments


Subscribe to Simple Syntax

Thanks for subscribing!

bottom of page