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

File ex3.py


Notepad++

print "I will now count my chickens:"

print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4

print "Now I will count the eggs:"


print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh, that's why it's False.."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

Windows PowerShell

PS C:\Users\Tom\mystuff> python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False..
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False

Study Drills #1

Above each line, use the # to write a comment to yourself explaining what the line does.

# Prints I will now count my chickens: ##Statement
print "I will now count my chickens:"

# Prints number of Hens 30 ### 25 + (30/6) = 30
print "Hens", 25 + 30 / 6
# Prints Roosters 97 #### 100 - ((25 * 3)modulo4) = 97
print "Roosters", 100 - 25 * 3 % 4

# Prints Now I will count the eggs ###Statement
print "Now I will count the eggs:"

# Prints answer 7 ### 3 + 2 + 1 - 5 + (4 % 2) - (1 / 4) + 6 = 6.75 round off displays 7
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

# Prints Is it true that 3 + 2 < 5 - 7? ###Statement
print "Is it true that 3 + 2 < 5 - 7?"

# Prints answers false
print 3 + 2 < 5 - 7

# Prints What is 3 + 2? 5 ### 3 + 2 = 5
print "What is 3 + 2?", 3 + 2
# Prints What is 5 - 7? -2 ### 5 - 7 = -2
print "What is 5 - 7?", 5 - 7

# Prints Oh, that's why it's False..
print "Oh, that's why it's False.."

# Prints How about some more.
print "How about some more."

# Prints Is it greater? True
print "Is it greater?", 5 > -2
# Prints Is it greater or equal? True
print "Is it greater or equal?", 5 >= -2
# Prints Is it less or equal? False
print "Is it less or equal?", 5 <= -2

Study Drills #2

Remember in Exercise 0 when you started Python? Start Python this way again and using the above characters and what you know, use Python as a calculator.

PS C:\> 2+2
4

PS C:\> 15-20
-5

PS C:\> 20/4
5

PS C:\> 4/20
0.2

PS C:\> 5*5
25

Study Drills #3

Find something you need to calculate and write a new .py file that does it.

ex3sd3.py

Starting_mileage = 140274
Ending_mileage = 140572
gallons = 20
dif = Ending_mileage - Starting_mileage
print "Starting Mileage::", Starting_mileage
print "Ending Mileage::", Ending_mileage
print "Different::", dif
print "Gallons::", gallons
print "MPG::", dif / gallons

PS C:\mystuff> python ex3sd3.py
Starting Mileage:: 140274
Ending Mileage:: 140572
Different:: 298
Gallons:: 20
MPG:: 14

Study Drills #4

Notice the math seems "wrong"? There are no fractions, only whole numbers. Find out why by researching what a "floating point" number is.

Floating point numbers are numbers which have a decimal component.

Floating Point Python

[Tutor] Floating point exercise 3 from Learn python the hard way

If you tell Python to divide 2 integers (whole numbers) by default it
will use integer arithmetic. So in your calculation on line 5 you get
a result of 7 as Python does not switch to floating point arithmetic.

You can ask Python to use floating point arithmetic a couple of ways.
For example if you want 2 divided by 3 you can tell Python to use
floating point arithmetic by:

--1--
>>> float(2) / 3
0.6666666666666666
>>> float(20) / 7
2.857142857142857
--2--
>>> 2.0 / 3
0.6666666666666666
>>> 20.0 / 7
2.857142857142857

Floating-point cheat sheet for Python

Floating-point cheat sheet for Python
Floating-Point Types

Almost all platforms map Python floats to IEEE 754 double precision.

f = 0.1

Decimal Types

Python has an arbitrary-precision decimal type named Decimal in the decimal module, which also allows to choose the rounding mode.

a = Decimal('0.1')
b = Decimal('0.2')
c = a + b # returns a Decimal representing exactly 0.3

How to Round

To get a string:

"%.2f" % 1.2399 # returns "1.24"
"%.3f" % 1.2399 # returns "1.240"
"%.2f" % 1.2 # returns "1.20"

To print to standard output:

print "%.2f" % 1.2399 # just use print and string formatting

Specific rounding modes and other parameters can be defined in a Context object:

getcontext().prec = 7

Study Drill #5

Rewrite ex3.py to use floating point numbers so it's more accurate (hint: 20.0 is floating point).

print "I will now count my chickens:"

print "Hens", 25.0 + 30.0 / 6
print "Roosters", 100 - 25.0 * 3.0 % 4.0

print "Now I will count the eggs:"


print 3.0 + 2 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0

print "Is it true that 3 + 2 < 5 - 7?"

print 3.0 + 2.0 < 5.0 - 7.0

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh, that's why it's False.."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

PS C:\mystuff> python ex3sd5.py
I will now count my chickens:
Hens 30.0
Roosters 97.0
Now I will count the eggs:
6.75
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False..
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False

Math symbols.


  • + plus
  • - minus
  • / slash
  • * asterisk
  • % percent
  • < less-than
  • > greater-than
  • <= less-than-equal
  • >= greater-than-equal

Python Programming/Basic Math

Common Student Questions

Learn Python The Hard Way

Why is the % character a "modulus" and not a "percent"?
Mostly that's just how the designers chose to use that symbol. In normal writing you are correct to read it as a "percent", as in
"100%" is "one hundred percent". In programming this calculation is typically done with simple division and the / operator.
The % modulus is a different operation that just happens to use the % symbol.

How does % work?
Another way to say it is, "X divided by Y with J remaining." As in, "100 divided by 16 with 4 remaining."
The result of % is the J part, or the remaining part.

What is the order of operations?
In the US we use an acronym called PEMDAS which stands for Parentheses Exponents Multiplication Division Addition Subtraction.
That's the order Python follows as well.

Why does / (divide) round down?
It's not really rounding down, it's just dropping the fractional part after the decimal.
Try doing 7.0 / 4.0 and compare it to 7 / 4 and you'll see the difference.