Overview

  • Hour 1

    • Data Types
    • Decision Structures
  • Hour 2

    • git demo
    • py.test demo
  • Hour 3

    • Graded lab exercise

Data Types

  • Recall that variables are like containers with labels
  • These containers also have "type." The type of a container determines the shape of stuff that can go inside.
  • Many data types in Python. Only need to know about: string, integer, float, and Boolean

Integer

  • Positive or negative whole numbers

Float

  • Short for floating point number
  • Positive or negative number with values to the right of the decimal place

Booleans

  • Named after the mathematician George Boole.
  • Booleans can be True or False. (The capitalization is important.)

In [ ]:
arthur = "king"
lancelot = -23
robin = 1.99
bedevere = True

How does Python know the data type?

  • Strings are delimited by quotation marks
  • Floats have decimals
  • Ints don't have decimals

In [28]:
arthur = "king"
type(arthur)


Out[28]:
str

In [29]:
lancelot = -23
type(lancelot)


Out[29]:
int

In [30]:
robin = 1.99
type(robin)


Out[30]:
float

In [31]:
bedevere = True
type(bedevere)


Out[31]:
bool

Reassigning Variables

  • Variables can change values after initial assignment

In [32]:
galahad = 1
galahad = 57
galahad


Out[32]:
57
  • Variables can also change types after initial assignment

In [34]:
patsy = 2
patsy = "Clip clop"
type(patsy)


Out[34]:
str
  • Value can also be forced to take on a data type

    • int() converts a value to an integer
    • float() converts a value to a float
    • str() converts a value to a string

    But it doesn't always make sense to so


In [40]:
zoot = float(5)
zoot = 5.0
zoot
type(zoot)
zoot = "5"
type(zoot)
zoot = str(5)
zoot
type(zoot)


Out[40]:
str

In [49]:
arthur = "king"
galahad = "5"

print(arthur + galahad)

robin = 4
bedevere = 5.0

print(robin * bedevere)
int(20.7)


king5
20.0
Out[49]:
20

Ranges of Data Types

  • It is possible to have numbers that are too big or too small to be represented by Python.
  • Integers range from -2**63 to 2**63 - 1
  • Floats range from 2.2250738585072014e-308 to 1.7976931348623157e+308

In [ ]:
import sys
print(sys.maxint)
print(sys.float_info)

Decision Structures

So far, we've been writing programs that do the same thing repeatedly. If order for a program to be more reactive, we need a way for it to make decisions. Depending on whether something is the case, it takes one action or another.

Here's the general form of an if statement

if *condition*: statement statement etc.


In [51]:
cold = True

if cold:
    print("Wear a coat.")

Boolean Expressions and Relational Operators


In [56]:
x = 1
y = 2
y != x


Out[56]:
True

Comparing Strings

  • Characters are stored as ASCII codes
    • Uppercase A-Z are numbers 65-90
    • Lowercase a-z are 97-122
    • Digits 0-9 are 48-57
  • String comparisons use these codes

In [62]:
name1 = "Mary1"
name2 = "Mary"

name1 < name2


Out[62]:
False

Putting Together Concepts

Let's put the if statement together with Boolean expressions


In [64]:
temperature = 10

if (temperature < 12):
    print("Wear a coat.")


Wear a coat.

Alternate Paths


In [69]:
temperature = 39.5

if temperature < 40.0:
    print("A little cold, isn't it?")
    print("Wear a coat")
else:
    print("Nice weather we're having")
    print("Don't wear a coat")

print("Done the if-statement")


A little cold, isn't it?
Wear a coat
Done the if-statement

Nested Decision Structure


In [72]:
salary = 40000
years_on_the_job = 2

if salary >= 30000:
    if years_on_the_job >= 2:
        print("You qualify for the loan")
    else:
        print("You must have been on your current job for at least two years to qualify.")
else:
    print("You must earn at least $30,000 per year to qualify.")


You qualify for the loan

Multiple Nested Decision Structures


In [83]:
score = 45

if score >= 90:
    print("Your grade is A")
elif score >= 80:
    print("Your grade is B")
elif score >= 70:
    print("Your grade is C")
else:
    print ("Restart. Try again.")

print("Done with if-statement")


Restart. Try again.
Done with if-statement

Rule 5

Test your code, often and thoroughly.

py.test

A test framework for Python. (There are many.)

We use this to grade. For subsequent assignments, you'll be writing test cases too.

Lab Exercise

Each student should fork the repo at:

https://github.com/benevolentprof/inf1340-2015-labs

Then clone into PyCharm

Do the exercise, commit and push as you go along.


In [ ]: