Variables for Absolute Beginners: Free Python Tutorial

What are Python variables?

Let’s learn all about them! In this tutorial, you’ll learn how to use Python variables to code.

Want to code more? Enroll in our Python course – on sale today.

Variables are a way of storing information or data that you may want to keep for later, or for storing information you may not know yet.

For instance: suppose you’re writing code that will ask for user input. At the beginning of the code, you won’t know the user input, so you will store the user input in a variable.

Variables are convenient because they let you save information and use it later. Let’s look at an example of a variable below. Note that we’re using the development environment Spyder (Python 3.5) to test our code.

Suppose you’re writing code and want to store the number 1. You can declare (create) a variable to do so.

To declare a variable, write the variable name in the Editor, followed by an assignment operator, which is represented by an equals sign.

one = 

The preceding code declares a variable with the name one. one will be assigned the value on the other side of the operator. Let’s make the value 1 with the following code.

one = 1

You can use the same format to make variables with the values 2 and 3.

one = 1
two = 2
three = 3

With the preceding code, we created the variable two with the value 2. We also created the variable three with the value 3.

Thus we have 3 different variables that we can call. We can do different things with them, and they do not affect one another.

Printing Variables

To prove that we’ve created the variables successfully, we can use the print function. This function lets you print output to the screen so that you can see it. You can learn more about functions in our Python course. Right now, we’ll use one function so that we can see our variables in action.

Type the word print, which is the keyword for the print function. The development environment will color the word in purple.

one = 1
two = 2
three = 3

print

In parentheses after the variable name, you put the parameters of the function. In this case, the parameter will be the name of the variable you want to print. Let’s print one first using the following code.

one = 1
two = 2
three = 3

print(one)

On subsequent lines, use the same format to print two and three.

one = 1
two = 2
three = 3

print(one)
print(two)
print(three)

If you run the code by pressing the Run button, the console on the right side will print the values of the variables that we assigned earlier.

You can reuse variables. For example, the following screenshot shows that you can print the variables backwards.

What happens if you overwrite variables?

You can overwrite a variable (change its value) by assigning it a new value. For instance, the following code changes the value of two to 4.

one = 1
two = 2
three = 3

print(one)
print(two)
print(three)
two = 4
print(two)
print(one)

You will see in the console that before you overwrite the variable, the print function will print the variable’s initially assigned value. After the overwriting line, print will print the variable’s new value because you’ve overwritten the data.

You’ve changed the value of the data inside the variable. When you call the print function, your computer looks at the last known value and prints it to you. When you modify the value, the value stored inside the computer gets changed.

Types of Variables

We’ve created variables (one, two, three) of a certain type: integers. They are integers because their values (1, 2, 3, 4) are integers (whole numbers). You can create other types of integers.

For instance, the following code creates a variable that is a decimal. Note that the name of the variable can be whatever you wish, but it is logical to name it something that relates to its value.

one = 

The preceding code declares a variable with the name one. one will be assigned the value on the other side of the operator. Let’s make the value 1 with the following code.

Decimal = 1.1

Another type of variable you can make is a string. Use the following format to create a new variable named StringVar (to stand for string variable.)

StringVar

Notice that we capitalized the first letter of each word in this variable’s name. Although not required in Python, it is a naming convention and makes code easier to read.

A string is a series of characters. To declare a variable as a string, you can assign it a value in quotation marks. The following code gives StringVar the value "Hello".

StringVar = "Hello"

You can print this variable as well, using the same format for the print function.

StringVar = "Hello"
print(StringVar)

In Python, you don’t have to tell the computer what type you want a variable to be. Python automatically assigns a variable a type depending on the variable’s value. This differs from some other programming languages. You can simply assign a value, and the computer will store it in memory to be used later.

There are some rules, however. For instance, if you want to add a value to a variable, you can do so with the + operator. However, each variable can only be of one type. Let’s look at an example.

Suppose you want to add “1” to the end of StringVar. If you simply write + 1 to where you declared StringVar, you will get an error message. The console will print the TypeError “Can’t convert ‘int’ object to str implicitly.”

This error occurs because "Hello" is a string, whereas 1 is an integer. If you put quotation marks around 1, although 1 is still a number, the computer will interpret it as a string. You will no longer get an error message.

StringVar = "Hello" + "1"

All the variables we created are called global variables because they are not contained in any function. The variables are contained in the core of the code and aren’t in a subsection. As such, you can access them from anywhere.

An example of a local variable is a variable inside a function. For instance, the following code outlines how to set up a function. We provide more information on functions in our Python course.

def FunctionName(Input):
Action
return Output

You can declare a variable inside the function like so:

def FunctionName():
newVar = "World"
return

As such, newVar is a local variable. It is local to a function. You cannot access newVar from outside the function. You can call the print function on newVar in the function. The console will print the value of the variable: World.

However, if you try to print newVar outside FunctionName, the code will crash. Because newVar is locally defined, it doesn’t exist outside the function. Your computer will give you the NameError “name ‘newVar’ is not defined”.

def FunctionName():
newVar = "World"
return

FunctionName()
print(newVar)

On the flip side, you can use global variables inside functions. For instance, you can call the one variable by simply typing its name in FunctionName. However, to let other developers know or to remind yourself that one is global, you can write the keyword global before the variable name.

def FunctionName():
global one
newVar = "World"
return

Comment out the line print(newVar) that is outside FunctionName so that we don’t get any errors. To comment out a line, add a hashtag to the beginning. The console will skip over any commented lines.

def FunctionName():
global one
newVar = "World"
return

FunctionName()
#print(newVar)

Call print on one inside FunctionName. The console will print 1, the value of one that we defined earlier.

def FunctionName():
global one
print one
newVar = "World"
return

What else can you do with variables? There is another way to define variables that is a shorthand.

An alternate way to declare our variables one, two and three is with the following format:

one, two, three = 1, 2, 3

This code assigns the value 1 to one, 2 to twoand 3 to three.

What else can you do with variables?

You can create a new variable that is the sum of 2 values. For instance, the following code declares a variable five and assigns it the value 3+2. If you print five, the console will print the sum: 5.

five = 3+2

What can you not do?

You can’t use variables that are not yet defined. For instance, the following line would return an error because we have yet to declare a variable six.

five + six = 5

You also cannot declare a variable value-first. For instance, the following code would give the SyntaxError “can’t assign to literal”.

5 = five

This occurs because code to the left of the assignment operator (equals sign) is what you’re giving the value to, and code to the right of the sign is what the value is.

What are counting variables?

You will use counting variables a lot when programming. You can use counting variables to keep track of the number of times a certain event occurs. Let’s look at an example. Create a variable count, and give it the initial value 0.

count = 0

Call print on the count variable. Comment out all the other print lines so that you can clearly see what this example prints in the console. The console will print 0.

count = 0
print(count)

Let’s make the value of count 1. There are several ways we can do this. One way we’ve already seen: to overwrite the value with the following code.

count = 0
print(count)
count = 1
print(count)

Another way is to add a value to the variable with the following code.

count = 0
print(count)
count = count + 1
print(count)

The preceding code assigns the value of count to be count’s current value plus 1. With a print function, the console would print 1.

You can increment count‘s value in this way because it will always add onto the latest value. For instance, assign count the value count + 1 again, and print it. The console will print 0 1 2.

count = 0
print(count)
count = count + 1
print(count)
count = count + 1
print(count)

A shorthand notation to add a value to the current value of count is the following. This code will add to and redefine count.

count += 1

Similarly, you can multiply the value of count and reassign its value.

count = count * 3
print(count)

There is shorthand notation for this, too:

count *= 3
print(count)

The console will print 3 at this line.

You can divide the value of count and reassign its value.

count = count / 3
print(count)

There is shorthand notation for this, too:

count /= 3
print(count)

The shorthand is helpful because as you write more code, you will find that the more ways you can write less, the more efficient you will be as a coder.

Want to code more? Enroll in our Python course – on sale today.

In this course, you will learn how to code in the Python 3.5 programming language. Whether you have or have not coded before, you can learn how to use Python. Python is a popular programming language that is useful to know because of its versatility. Python is easy to understand and can be used for many different environments. Cross-platform apps and 3D environments are often made in Python.

We will cover basic programming concepts for people who have never programmed before. This course will cover key topics in Python and coding in general, including variables, loops, and classes. Moreover, you will learn how to handle input, output, and errors.

To learn how to use Python, you will create a functioning Blackjack game! In this game, you will receive cards, submit bets, and keep track of your score. By the end of this course, you will be able to use the coding knowledge you gained to make your own apps and environments in Python.

What students have said about our courses:

“He explains everything.. great course, great teacher.”

“The instructor tells us why we do things and breaks down everything so you will understand better.”

“Pace is excellent, iterates points without being redundant. Builds on previous knowledge in a very organic way.”

“Great course. Clear voice, good screen material. I liked it.”

“I absolutely love this course. I’m only part way through so far but felt compelled to leave a review. This is such a comprehensive course that was well worth the money I spent and a lot more!. Well done. Will definitely be looking at more Mammoth Interactive courses when I finish this.”

This course is project-based, so you will not be learning a bunch of useless coding practices. At the end of this course, you will have real-world apps to use in your portfolio. We feel that project-based training content is the best way to get from A to B. Taking this course means you learn practical, employable skills immediately.

Learning how to code is a great way to jump in a new career or enhance your current career. Coding is the new math and learning how to code will propel you forward for any situation. Learn it today and get a head start for tomorrow. People who can master technology will rule the future.

Get the course here!

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com