Learn Python the Hard way


18to39
In [2]:
#exeercise 18
def print_two(*args):
    arg1, arg2 = args
    print ("arg1: ", arg1, "arg2: ", arg2)

# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
    print ("arg1: ", arg1, "arg2: ", arg2)

# this just takes one argument
def print_one(arg1):
    print ("arg1: ", arg1)

# this one takes no arguments
def print_none():
    print ("I got nothin'.")


print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
arg1:  Zed arg2:  Shaw
arg1:  Zed arg2:  Shaw
arg1:  First!
I got nothin'.
In [3]:
#exercise 19

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print ("You have ",cheese_count, "cheeses!")
    print ("You have ", boxes_of_crackers,"  boxes of crackers")
    print ("Man that's enough for a party!")
    print ("Get a blanket.\n")


print ("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)


print ("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)


print ("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)


print ("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
We can just give the function numbers directly:
You have  20 cheeses!
You have  30   boxes of crackers
Man that's enough for a party!
Get a blanket.

OR, we can use variables from our script:
You have  10 cheeses!
You have  50   boxes of crackers
Man that's enough for a party!
Get a blanket.

We can even do math inside too:
You have  30 cheeses!
You have  11   boxes of crackers
Man that's enough for a party!
Get a blanket.

And we can combine the two, variables and math:
You have  110 cheeses!
You have  1050   boxes of crackers
Man that's enough for a party!
Get a blanket.

In [14]:
#exercise 20

input_file= "junk.txt"


#reading a file
def to_print_a_file(f):
    print (f.read())
    
def rewind(f):
    f.seek(0)
    
def print_a_line(line_count,f):
    print(line_count, f.readline())
    
current_file= open(input_file)  
print ("First let's print the whole file:\n")
to_print_a_file(current_file)

print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print ("Let's print three lines:\n")

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

    
First let's print the whole file:

hello how are you
my name is Prarthana
I study in Praxis

Now let's rewind, kind of like a tape.
Let's print three lines:

1 hello how are you

2 my name is Prarthana

3 I study in Praxis

In [18]:
#exercise 21

def add(a,b):
    print("ADDING",a,b)
    return a+b

def subtract(a, b):
    print ("Subtracting ", a, b)
    return a - b

def multiply(a, b):
    print ("Multiplying ", a, b)
    return a * b

def divide(a, b):
    print ("Dividing ", a, b)
    return a / b



print("Let's do some math with just functions")

age=add(30,5)
height= subtract(78,4)
weight=multiply(90,2)
q=divide(100,2)

print ("Age: Height: Weight: IQ: " , age, height, weight, q)

#a puzzle for extra credit,type it anyway
print(" here is a puzzle")

what=add(age,subtract(height,multiply(weight, divide(q, 2))))
print ("That becomes: ", what, "Can you do it by hand?")
Let's do some math with just functions
ADDING 30 5
Subtracting  78 4
Multiplying  90 2
Dividing  100 2
Age: Height: Weight: IQ:  35 74 180 50.0
 here is a puzzle
Dividing  50.0 2
Multiplying  180 25.0
Subtracting  74 4500.0
ADDING 35 -4426.0
That becomes:  -4391.0 Can you do it by hand?
In [31]:
#exercise 24

print ("Let's practice everything.")
print ('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""

print("......")
print(poem)
print("......")

five= 10-2+3-6
print("This should be five:", five)


def secret_formula(started):
    jelly_beans=started*500
    jars=jelly_beans/1000
    crates=jars/100
    return jelly_beans,jars,crates

beans,jars,crates=secret_formula(10000)

print ("With a starting point of: ",10000)
print ("We'd have ",beans," beans,", jars, " jars and ", crates, " crates.")


print("We can also do this way")
print("We have %d beans, %d jars, %d crates." % secret_formula(1000))
Let's practice everything.
You'd need to know 'bout escapes with \ that do 
 newlines and   tabs.
......

 The lovely world
with logic so firmly planted
cannot discern 
 the needs of love
nor comprehend passion from intuition
and requires an explanation

  where there is none.

......
This should be five: 5
With a starting point of:  10000
We'd have  5000000  beans, 5000.0  jars and  50.0  crates.
We can also do this way
We have 500000 beans, 500 jars, 5 crates.
In [32]:
#exercise 29

people = 20
cats = 30
dogs = 15


if people < cats:
    print ("Too many cats! The world is doomed!")

if people > cats:
    print ("Not many cats! The world is saved!")

if people < dogs:
    print ("The world is drooled on!")

if people > dogs:
    print ("The world is dry!")
    
dogs+=5

if people >= dogs:
    print ("People are greater than or equal to dogs.")

if people <= dogs:
    print ("People are less than or equal to dogs.")


if people == dogs:
    print ("People are dogs.")
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
In [36]:
#exercise 30

people=30
cars=40
trucks=15

if cars > people:
   print ("We should take the cars.")
elif cars < people:
    print ("We should not take the cars.")
else:
    print ("We can't decide.")
    
if trucks > cars:
    print ("That's too many trucks.")
elif trucks < cars:
    print ("Maybe we could take the trucks.")
else:
    print ("We still can't decide.")

if people > trucks:
    print ("Alright, let's just take the trucks.")
else:
    print ("Fine, let's stay home then.")
We should take the cars.
Maybe we could take the trucks.
Alright, let's just take the trucks.

Comments

Popular posts from this blog

Playing with Tableau

Working with Pandas

Assignment 1 - Learn Python the Hard Way