IntroductionDo 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 VariablesPython 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
lists(arrays in other languages), both of which hold collections of information.
Tupleshold groups of different types of information and cannot be changed. In programming terms, the inability to change
tuplesmeans that they are immutable.
Lists, in contrast, hold groups of the same type of date and can be changed at any time.
Assigning VariablesTo 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_hellothat is a
stringand 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.5When dealing with numbers, you can even set a variable equal to an equation, and the variable will equal the solution.
the_answer = 6 * 7Now, check out the values of some variables by printing them out. You can give
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
Comparing VariablesPython also gives you the ability to compare the values of variables and gives you a value of either
falsedepending 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
stringstoo. Python will just check that the text in the
stringsis 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_stringhas 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;
!=. 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!" TrueThe
>(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.
>=(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.
ClosingThere 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.
- Create a string variable called "name" and set it equal to your name.
- Print out your new "name" variable.
- Create an integer variable called "an_int."
- Create a float variable called "a_float."
- Multiply "an_int" and "a_float" and assign the result to a new variable called "the_answer."
- Print out "the_answer."
- 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."
- Use a comparison operator to check that "the_answer" is equal to "an_int" divided by "a_float."
- Use print and a comparison operator to see if "3**3 + 3" is greater than 30.
- Use a different comparison operator on the statement from the previous question to get the opposite result.
- Print the result of "'hello' != 'Hello'." Why is that the result?
- 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
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions