Python Variables

Introduction

Do you remember variables from math class in school? Variables in programming are actually very similar. Variables are just symbols that that represent a value and that value can be changed; thus the name variable. Unlike in math, variables in programming can be much more free form. Variables don’t just have to be a letter. Variables can be a single character, but they are more commonly a word or a short descriptive phrase in lower case with words separated by underscores. It’s actually best to name variables something descriptive so the you and anyone else that you’re working with knows exactly what that variable is, even much later on in the code.

Types of Variables

Python is a dynamic duck typed language. Don’t worry too much about the terminology, but that means that Python doesn’t force you to specify which types variables are when you create them. Oh yeah, there are types of variables. Even though you don’t necessarily have to specify their type when you create them, it’s a good idea to know what type you want them to be. Later on, having the wrong type of variable will invariably get you into big trouble.



There are six major types of variables that you need to know about right now. Four of them are simple, or primitive, variables. The other two are more complex and involve collections of data. There are integers, which are whole numbers. Floats, or floating-point numbers, are numbers with decimals. Text is referred to as a string. There are also booleans, which hold true or false values.

On the more complex side, there are tuples and lists(arrays in other languages), both of which hold collections of information. Tuples hold groups of different types of information and cannot be changed. In programming terms, the inability to change tuples means that they are immutable. Lists, in contrast, hold groups of the same type of date and can be changed at any time.

Assigning Variables

To assign a value to variables and create them, just tell Python what they are equal to. For example, if you wanted to create a variable called say_hello that is a string and holds the phase, “Hello World!” take a look at the example below.

say_hello = "Hello World!"

If you’re working with numbers, it’s nearly the same thing. Just set the variable name equal to whatever number you want.

this_integer = 5
this_float = 5.5

When dealing with numbers, you can even set a variable equal to an equation, and the variable will equal the solution.

the_answer = 6 * 7

Now, check out the values of some variables by printing them out. You can give print the name of a variable, and it will give you back the value.

print(the_answer)

You can change the values of variables just as easily. All you need to do is set them equal to something else. Try it out, and print the values in between.

a = 5
print(a)
a = 10
print(a)

See? It just changes. The same is (sort of) true for strings. They are technically immutable, like tuples, so you’re not actually modifying the existing value. You’re pointing the variable at a new one. That really isn’t some thing to worry about right now, though.

a_string = "Hello!"
print(a_string)
a_string = "Goodbye!"
print(a_string)

It still works the way you’d expect. Later on in more advanced use, you’ll want to keep the immutability of strings in mind.

Comparing Variables

Python also gives you the ability to compare the values of variables and gives you a value of either true or false depending on the comparison. Comparing variables is a very important concept in programming, and will come into play quite a lot later on. Thankfully, Python uses the symbols for comparison from math to handle comparing variables.

The first thing that you’d probably want to check is whether or not two variables are equal to one another. So, you do that with two equal signs next to each other, like this; ==. There are two instead on one to differentiate checking equality for assigning value.

a = 6
b = 2 * 3
print(a == b)

It printed out “True” because three times two does equal six. You can compare strings too. Python will just check that the text in the strings is the same.

a_string = "Hello"
another_string = "Hello"
print(a_string == another_string)

Again, it came back “True” because they were the same. If you change one, the results will be different.

a_string = "Hello"
another_string = "Hello!"
print(a_string == another_string)

Now it’s “False” because another_string has an exclamation point. Again, Python and other programming languages are very literal. They don’t care that it means the same thing because it is not literally exactly the same.

There are other ways to compare variables or values, like; >, <, >=, <=, and !=. You've probably seen all of those before, except maybe the last one. So, the first few are greater than, less than, greater than or equal to, and less than or equal to. The last one that you may not have seen is "not equal." Here are a few examples:

>>> 5 > 7
False
>>> 7 > 5
True
>>> 5 * 2 >= 11
False
>>> 6 * 2 >= 11
True
>>> 6 * 2 >= 12
True
>>> 6 * 2 <= 13
True
>>> "Hello" != "Hello!"
True

The >(greater than) and the <(less than) just compare two values directly. If the two values are equal to one another, the comparison will return "False," since two equal values are neither greater than or less than one another.

The >=(greater than or equal to) and the <=(less than or equal to) do nearly the same thing, but they will evaluate to "True," if the values are equal.

!= is the exact opposite of ==. It will only evaluate to "True" when the two values are not equal to each other.

Closing

There is a lot more that can be done with variables. Play around with what you've done here. There are tons of possibilities with comparison operators. You can see how complex of statements you can make. The upcoming guides will explore each type of variable much greater depth and get you familiar with the capabilities of each.

Exercises

  1. Create a string variable called "name" and set it equal to your name.
  2. Print out your new "name" variable.
  3. Create an integer variable called "an_int."
  4. Create a float variable called "a_float."
  5. Multiply "an_int" and "a_float" and assign the result to a new variable called "the_answer."
  6. Print out "the_answer."
  7. Set "an_int" to a different integer. Set "a_float" to a different float. Divide them and set "the_answer" equal to the result. Print "the_answer."
  8. Use a comparison operator to check that "the_answer" is equal to "an_int" divided by "a_float."
  9. Use print and a comparison operator to see if "3**3 + 3" is greater than 30.
  10. Use a different comparison operator on the statement from the previous question to get the opposite result.
  11. Print the result of "'hello' != 'Hello'." Why is that the result?
  12. Create a set of number variables and arrange them such that the sum of the first two is less than the difference of the third and the first. Print the result.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions


Comments and Discussions
Linux Forum