
Variables
Go back: The Basics
Variables are a major part in Python, as they transport information around from one place to the other.
Let's call our variable "Var1":
Var1
and let's add our determinator:
Var1 =
Now that we have a variable created we can add any data to it:
Text ex: Var1 = "Hello World" ( Any Text ) {‘ or “}
List ex: Var1 = [’One’, ‘Two’, ‘Three’] ( Any Text ) {‘ or “}
Number ex: Var1 = 536 ( Any number )
Float ex: Var1 = 342.58 ( Any number )
Boolean ex: Var1 = True ( True or False )
There are more types of variables, but these are good for the basics.
When we use variables, we don’t always use them alone. Connecting variables to make more variables is a crucial part too! Let’s learn…
Here are some examples
var1 = 'Hello,'
var2 = ' World!'
print(var1 + var2) # Method 1, using the '+'
print(var1, var2) # Method 2, using the ','Method 1 and 2 seem very similar, but they actually are very different!
Method 1, simply explained, adds all the variables next to it, then prints it.
Whereas, Method 2, prints each item individually, enabling us to print two different types of variables at once! Like this:
var1 = 'Hello,'
var2 = 53
print(var1, var2)
When adding variables in-between text, we can easily clutter up our code by doing this:
var1 = 173
var2 = 896
print('Our first number is ', var1, '. And our second number is ', var2)
This does give us our desired outcome of:
Our first number is 173. And our second number is 896
But another technique can be used to speed up and de-clutter our code!
var1 = 173
var2 = 896
print(f'Our first number is {var1}. And our second number is {var2}')Adding an “f” before the first quotation mark will tell the computer that it will see variables surrounded with “{}” in the text, and to print them out as variables. Our first print command was 72 characters long, whereas our new one is only 69 characters long! (Nice)
Heres something you’ll use a billion times in your python journey:
var == varThis returns True if both variables are equal (returns False if they aren’t).
var != varThis does the opposite of ==, if the variables aren’t equal the return will be True, and if they are equal, it will return False.
number1 > number2Checks if number1 is larger than number2 (returns True), if that isn’t larger, then it returns False
number1 < number2Checks if number2 is smaller than number1 (returns True), if that isn’t smaller, then it returns False
These are great for the basics, but you’ll learn more the further you move on; and of course, you can guess, it works a lot of the time!