Variables, what they are, and how to use them




In almost every single Python program you write, you will have variables. Variables act as placeholders for data. They can aid in short hand, as well as with logic, as variables can change, hence their name.

Variables help programs become much more dynamic, and allow a program to always reference a value in one spot, rather than the programmer needing to repeatedly type it out, and, worse, change it if they decide to use a different definition for it.

Variables can be called just about whatever you want. You wouldn't want them to with function names, and they also cannot start with a number.

You want to be careful what you name variables, classes (discussed later), and functions (discussed later), so that they do not have the same names as eachother.

For example, you have leared about the print function. What if you go and define a variable named print?

Say, for example, you do:

print = print("Uh oh!")

Now you have a variable and a function named print, which can cause trouble down the road!

exampleVar = 55
print(exampleVar)
		

In this case, we will have a 55 printed out to console. So, in this case, we were able to store an integer to our variable.

cannotDo = Hey!
		

Hey! is not a valid datatype, and this will throw an error. You would need to throw quotes around the string.

canDo = 'Hey!'
print(canDo)
		

This is acceptable.

canContainOperations = 5/4
print(canContainOperations) 
		

Here, we can see that we were even able to store the result of a calculation to our variable.

We can even store a variable to our variable, or an operation with our variables to a variable. Something like var3 = (var2/var1) would work. You can store other things, like functions, as well to variables. More on that later!

Try playing with variables in the console provided above, or via your own Python installation. Once you feel comfortable with them, proceed to the next tutorial.


There exists 2 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:






  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial