As mentioned in our previous post, Python is an amazing language that allows you to get things done quickly with a lot less overhead.

However, to get started, you need to understand some of the basics. Let’s start with variables.

 

Variables

Variables are storage spaces for values in the Python programming language. You can store strings, numbers, functions, and more. Variables allow you to give names to important values in your program. Now, how to do you declare them? Well, take a look below!

Variable Example

two:int = 2
my_number:int = 2
final_value:int = two + my_number
print(two) # 2
print(my_number) # 2
print(final_value) # 4

As you can see, we gave the value “2” the name “two” and also the name “my_number”. Finally, we add them together to get the value 4, which we store in “final_value”. 

Notice that each variable have the type after them; in this case,  the type is an (int)eger. The newest version of Python has types, which allows us to get some safety when writing code. This can help you reason about what something really represents; in most Python programs, you can only rely on the name to figure out very complex structures.

 Now, those final lines that start with print, allow us to see the value of the variables.

 

Cool, But What Can I do With This?

You might be thinking this isn’t useful, but it’s the basis for many programs and the game code you’ll write; variables are some of the smallest building blocks. 

So, to show you the usefulness, fix this code so that it properly prints out the right value for the variables “three_times_five”.

my_num:int = 22
my_num_two:int = 23
three_times_five:int = my_num * my_num_two
print(three_times_five) # 506, but should be 15

Tune in next time, and we’ll show you how to do this in an easier way that can bee used for any calculation you want.

 

%d bloggers like this: