Chapter 2 - Variables

As stated in the previous chapter, coding is writing commands for a computer to execute. Computers carry out commands by writing data to memory, reading data from memory, and performing calculations on that data. Programmers can tell a computer to write data to memory by defining variables. A variable is a name that is tied to a value. For example, the code temperature = 32 assigns the number value 32 to the variable name temperature. Then you can use the variable name temperature throughout your code and it will be evaluated as the number value 32. There are many types of values that can be assigned to variables. Common types of values include numbers (e.g., 42, 3.14159), strings of text (e.g., "Hello, world!", "Python is fun and easy to learn!"), and booleans (True or False). Python assigns values to variable names using the following syntax:

my_num = 42
my_str = "Hello, world!"
my_bool = True

Use the exercise below to assing values to variable names. Read the comments in the provided code to see what values you should assign to each variable name.

# Change my_num to 3.14159 my_num = 42 # Change my_str to "This is my string" my_str = "Hello, world!" # Change my_bool to False my_bool = True # Print variable values print(my_num) print(my_str) print(my_bool) # Change my_num to 3.14159 my_num = 3.14159 # Change my_str to "This is my string" my_str = "This is my string" # Change my_bool to False my_bool = False # Print variable values print(my_num) print(my_str) print(my_bool) test_object("my_num") test_object("my_str") test_object("my_bool") test_function("print") success_msg("Great job!")
Change the value of my_num to 3.14159, my_str to "This is my string", and my_bool to False

Variable Names

In the first example above, the name of the variable is "my_num" (short for "my number"). The value is the number 42. Variable names in Python must follow the following rules:

  • Variable names must start with a letter or an underscore
  • The rest of the name can contain letters, numbers, and underscores
  • Names are case-sensitive (e.g., "my_Num" and "my_num" are different variables)

Notice that variable names cannot contain spaces. For example, employee age = 42 is not valid. To make variable names that are made of multiple words, programmers can use underscores (e.g., employee_age = 42) or camel case (e.g., employeeAge = 42). Python style guides suggest using underscores to separate chunks of multi-word variable names, but some other languages suggest camel case.

Variable Types

As previously mentioned, common types of variable values include numbers, strings, and booleans. Numbers are divided into different types, the two most common types being int and float. An int is an integer, which is a whole number without a decimal (e.g., -40, 0, 42, 28308732). A float is a decimal floating-point number, which is any number with a decimal (e.g., 3.14159, -43.2, 1.0, 0.0). In Python, passing a variable name into the type function will return the type of the value the variable is currently storing.

var1 = 1 # integer ('int') var2 = 1.0 # float var3 = "1" # string ('str') var4 = True # boolean ('bool') # Print each variable type print(type(var1)) print(type(var2)) print(type(var3)) print(type(var4)) # Print a sentence for each variable value and type # Don't mess with this code or worry about understanding it, it's just here to give you nice output print("var1 has a value of " + str(var1) + " and is of type " + str(type(var1))) print("var2 has a value of " + str(var2) + " and is of type " + str(type(var2))) print("var3 has a value of \"" + str(var3) + "\" and is of type " + str(type(var3))) print("var4 has a value of " + str(var4) + " and is of type " + str(type(var4)))

Numbers, strings, and booleans all behave differently, so it's important to know the difference. For example, the string "42" is different than the integer 42, and both are different than the float 42.0. Similarly, the boolean False is different than the string "False". The following chapters will illustrate how the computer treats different variable types differently and what you as the programmer can do with different variable types.

Review

Define integers using the following syntax: int_name = 10
Define floats using the following syntax: float_name = 10.0
Define strings using the following syntax: str_name = "my string could beat up your string"
Define booleans using the following syntax: bool_name = True

Print variable values using the following syntax: print(variable_name)
Print variable types using the following syntax: print(type(variable_name))

Use the exercise below to put all your newfound knowledge to the test!

# Define an int named x with a value of 4 # Define a float named y with a value of -12.34 # Define a string named name with a value of "Bob" # Define a boolean named is_valid with a value of True # Print each variable value # Print each variable type # Define an int named x with a value of 4 x = 4 # Define a float named y with a value of -12.34 y = -12.34 # Define a string named name with a value of "Bob" name = "Bob" # Define a boolean named is_valid with a value of True is_valid = True # Print each variable value print(x) print(y) print(name) print(is_valid) # Print each variable type print(type(x)) print(type(y)) print(type(name)) print(type(is_valid)) test_object("x") test_object("y") test_object("name") test_object("is_valid") test_output_contains("4", False, "Output should show 4") test_output_contains("-12.34", False, "Output should show 12.34") test_output_contains("Bob", False, "Output should show Bob") test_output_contains("True", False, "Output should show True") test_output_contains("class 'int'", False, "Output should show type of x") test_output_contains("class 'float'", False, "Output should show type of y") test_output_contains("class 'str'", False, "Output should show type of name") test_output_contains("class 'bool'", False, "Output should show type of is_valid") success_msg("Now you know how to define variables and classify their types!")
Print each variable value by using print(name). Print each variable type by using print(type(name)).