{article Learn Python The Hard Way}{text}{/article}

Notepad++

# ex4.py

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

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."

Window PowerShell

PS C:\\mystuff> python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

Study Drill 1

  • I used 4.0 for space_in_a_car, but is that necessary? NO.
  • What happens if it's just 4? You would get a whole number as the answer.

Study Drill 2

Remember that 4.0 is a "floating point" number. Find out what that means.

  • Floating point numbers are numbers which have a decimal component.

Study Drill 3

Write comments above each of the variable assignments.

# ex4.py

cars = 100 # Number of car available.
space_in_a_car = 4.0 # Number of space in a car
drivers = 30 # Number of Drivers
passengers = 90 # How many passengers
cars_not_driven = cars - drivers # How many cars not driven
cars_driven = drivers # How many cars you have and drivers needed
carpool_capacity = cars_driven * space_in_a_car # How many you can transport.
average_passengers_per_car = passengers / cars_driven # How many passengers in a car.

Study Drill 4

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

  • Assigns the value on the right to a variable on the left.

Study Drill 5

Remember that _ is an underscore character.

  • We use this character a lot to put an imaginary space between words in variable names.

Study Drill 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 = 4
x = 8
j = 2

j = i + x

print "Total", j

>>>
Total 12

Names to Avoid

Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.
In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use 'l', use 'L' instead.

The equal sign ('=') is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:

width = 20
height = 5*9
width * height
print width * height
900


A value can be assigned to several variables simultaneously:

x = y = z = 0 # Zero x, y and z
print x
0
print y
0
print z

0



Variables must be “defined” (assigned a value) before they can be used, or an error will occur:

python : Traceback (most recent call last):
At line:1 char:1
+ python test4.py
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Traceback (most recent call last)::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

File "test4.py", line 1, in <module>
n
NameError: name 'n' is not defined



There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:

PS > 3 * 3.75 / 1.5
7.5

PS > 7.0 / 2
3.5