LPTHW – Exercise 4: Variables Names

LEARN PYTHON THE HARD WAY  Study Drills:

What is a Variable?

According to Wikipedia, In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents.

Python is completely object-oriented, and not “statically typed”. You do not need to declare variables before using them or declare their type. Every variable in Python is an object.

What we learnt

In this exercise, we will learn about variables and how to assign a value to perform mathematical operations on variables.

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars -drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
OutPut
print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."

LPTHW – Exercise 4: Variables Names

1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it’s just 4?

# By using 4.0, it allows the answer to have a decimal out to the tenths place. If the float was not used a float would not be permitted in the answer.

2. Remember that 4.0 is a “floating point” number. Find out what that means.

Check exercise 3 for floating points

3. Write comments above each of the variable assignments.

Check below code with comments for reference

# Sets variable 'car's to '100'
cars = 100
# Sets variable 'space_in_a_car' to '4.0' (this being a floating point number)
space_in_a_car = 4.0
# Sets variable 'drivers' to '30'
drivers = 30
# Sets variable 'passengers' to '90'
passengers = 90
# Sets variable 'cars_not_driven' to the variable 'cars' minus the variable
# 'drivers' = 100 - 30
cars_not_driven = cars - drivers
# Sets variable 'cars_driven' to variable 'drivers' = 30
cars_driven = drivers
# Sets variable 'carpool_capacity' to variable 'drivers' multiplied by 
# variable 'space_in_a_car' = 30 * 4.0
carpool_capacity = drivers * space_in_a_car
# Sets variable average_passengers_per_car' to variable 'passengers' divided
# by the variable 'cars_driven' = 90 / 30
average_passengers_per_car = passengers / cars_driven


print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty cars today.")
print("We can transport", carpool_capacity, "people today.")
print("We have", passengers, "to carpool today.")
print("We need to put about", average_passengers_per_car, "in each car.")

4. Make sure you know what = is called (equals) and that it’s making names for things.

Ok

5. Remember that _ is an underscore character.

Yes

6. Try running Python as a calculator like you did before and use variable names to do your
calculations. Popular variable names are also i, x, and j.

i = 15
x = 2
j = x*i
print "The value of j is", j

Output
The value of j is 30