4. Variables & Comments

In this lesson you will learn how to store information in variables and the importance of comments.

Variables

In programming you can think of variables as boxes that store data. In Python, variables are created the moment you first assign a value to it. To call a variable just call the name of the variable. There are many types of variables but for now we will go over four very common ones.

Why use variables?

Imagine you want to write a story about someone named Bob. It might go something like this:

Bob loves dogs and cats. Bob also likes to go to the beach. One of Bob's favorite things to do is to code.

If we wanted to all of a sudden change the name Bob to Andrew, we would have to manually go through the paragraph and change every time it says Bob. However, if we had made a name variable that stored the name Bob, we could just change the variable to be Andrew and every time the name variable was used, it would of changed the value to be Andrew for us. Because of this, variables are an essential and powerful tool to us programmers.

Strings

Strings are basically any words or sentences. In other words, strings contain ASCII characters (yes, that means you can store emojis). Strings can be created with either single or double quotation marks. Below is an example of assigning a string to a variable named a_cool_name:

a_cool_name = "Andrew"
a_cool_name = 'Andrew'

Note how the name of variables are all lowercase and use underscores (_) as spaces. It is not required for all variable names to follow this naming convention but is highly recommended in order to improve readability.

If you remember from before, the print function used a string and printed the string back through the console. Escape characters work the exact same as they do as stated in the last lesson.

Here is an example of using a string with a print function:

sentence = "I like dogs"
print(sentence)

Integers

Integers are any whole numbers. This includes all negative and positive numbers and zero. Below is an example of assigning an integer to a variable named my_fav_num:

my_fav_num = 3

Floats

Floats are another way to store numbers but can also store decimals. This also includes all negative and positive numbers and zero. Below is an example of assigning a float to a variable named very_small_num:

very_small_num = -932.35

Booleans

Booleans can have two values, true or false. Think of a boolean as storing a yes or no value, aka binary. Below is an example of assigning a boolean to a variable named i_like_to_code:

i_like_to_code = True

Make sure when assigning a boolean that the values True and False are capitalized.

Comments

Sometimes when you are programming you want to make a note or a comment on some part of code. To do this we can use a number sign/hash sign for a single line or triple apostrophes for multi-line comments as shown below:

# Knock, knock.
# Who's there?
# Tank.
# Tank who?
# You're welcome.
'''This
is
a
multi-line
comment'''

Instead of writing knock knock jokes in your code, you should utilize comments to explain how your code works or the purpose of certain parts. Comments are an essential way to recording work and when other programmers read your code, a useful comment will go a long way.

Comments are also useful to comment out segments of code when debugging.

User Input and Casting

Now that we know how to make variables lets let the user assign their own value to a variable. To get the user's input we have to use the input function shown below:

name = input("What's your name: ")
print("hello " + name)

Notice how we can use apostrophes inside double quotation marks.

So let's break down this code and see what is happening. First we created a variable named "name" and then assigned it to equal a built in function called input. The input function contains a string that is sent into the console to prompt the user. In this case it was "What's your name: ". After the user inputs their name into the console and clicks enter, the string the user typed is stored as "name". Then the next line prints back hello and "name".

Inside the print function the string was "added" to the variable to combine it into one string. In the next lesson we will go over more in-depth on how this works.

But now let's suppose you want to make a simple calculator that adds two numbers from the user but the input function returns a string so how can we convert or "cast" the string into a float? To do this, we would have to cast the string into a float. Below is an example of casting with a simple adding calculator:

first_float = float(input("First float: "))
second_float = float(input("Second float: "))
print("The sum of the two floats is: " + str(first_float + second_float))

In addition to the float cast there are casts for several different variables as shown below:

# Integer cast:
a = int(1)   # becomes 1
b = int(1.9) # becomes 1
c = int("1") # becomes 1

# Float cast:
a = float(1)     # becomes 1.0
b = float(1.9)   # becomes 1.9
c = float("1")   # becomes 1.0
d = float("1.9") # becomes 1.9

# String cast:
a = str("a1") # becomes "a1"
b = str(1)    # becomes "1"
c = str(1.0)  # becomes "1.0"
❮ PreviousNext ❯