Loops and Lists

# Loops and Lists
# In here Dawn, we do some Loops and Lists
# Remember your game
# Cheatad and well the Cheat itself
# Well I guess we can expand it you know
# Thumbs up any way
# Thumbs up to God too 🙂 He’s been amazing so far
# I guess we owe him Big 🙂

# Ok Dawn, we’re going to do some list in here you know
# We are going to store stuffs.
# Do some repeated works and who knows just maybe 🙂
# We are getting closer you know

# Now lets make a list

hairs = [‘brown’, ‘blond’, ‘red’]
eyes = [‘brown’, ‘blue’, ‘green’]
weights = [1, 2, 3, 4]

# Just a reminder Dawn, things will get just maybe tricky
# Interestingly, we have it worked out 🙂
# We now will buld some lists using some loops and print them out

the_count = [1, 2, 3, 4, 5]
fruits = [‘apples’, ‘oranges’, ‘pears’, ‘apricots’]
change = [1, ‘pennies’, 2, ‘dimes’, 3, ‘quarters’]

# this fist kind of for-loop goes through a list
for number in the_count: # the ‘for’ used here is known as a for-loop
print “This is count %d” % number

# same as above
for fruit in fruits:
print “A fruit of type: %s” % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don’t know what’s in it
for i in change:
print “I got %r” % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 20 counts
for i in range(0,7):
print “Adding %d to the list.” % i
# append is a function that lists understand
elements.append(i)
fruits.append(i) # We added this too but trust me guys, we still dont get it clearer

# Dawn own assessment
for i in fruits:
print “Some test you know”

# now we can print them out too
for i in elements:
print “Element was: %d” % i

# Ok, Dawn for the loop. We got loads of things to do
# Trust me yeah, we really need to understand this for loop thing
# Well it seems it prints out some repeated stuffs as dicussed earier on
# We will work with it 🙂

Making decisions

#Making decisions Dawn
# We are making little steps
# And I tell you what, we are getting closer to what
# We have always wanted
# Hurray!!!

print “You enter a dark room with two doors. Do you go through door #1 or door #2?”

door = raw_input(“> “)

if door == “1”:
print “There’s a giant bear here eating a cheese cake. What do you do?”
print “1. Take the cake.”
print “2. Scream at the bear.”

bear = raw_input(“> “)

if bear == “1”:
print “The bear eats your face off. Ooooops sorry pal.”
elif bear == “2”:
print “The bear eats your leg off. Ouch! Not good to hear.”
else: # This info would apear if the user types preferred words than choose #
print “Well, doing %s is probably better. Bear stays cool.” % bear

elif door == “2”:
print “You stare into the endless abyss at Cthuhlu’s retina.”
print “1. Blueberries.”
print “2. Yellow jacket clothespins.”
print “3. Understanding revolvers yelling melodies.”

insanity = raw_input(“> “)

if insanity == “1” or insanity == “2”:
print “You body survives powered by a mind of jello. Good job!”
else:
print “The insanity rots your eyes into a pool of muck. Good job!”

else: # This would print if the user does not choose anything at all or inputs something different
print “You stumble around and fall on a knife and die. Good job!”

Boolean expressions

# Ok, Here we are again Dawn
# What we did previously in 29.py was some boolean expressions
# We introduce Else and If boolean functions
# Or you would prefer to call statements 🙂
# One more thing, Python expects us to indent something after using a colon
# Yes Python wants us to 🙂

people = 30
cars = 40
buses = 15

if buses > cars or people < buses: # We are having some fun Dawn 🙂
print “Good work done Dawn”
elif buses < cars or people > buses: # Yes we are. And well we saw you clapping hands
print “Happy home”
else: print “Bad boy init.”

if cars > people and buses > cars:
print “Lets do some test Dawn”
elif cars < people and buses > cars:
print “Ok Dawn. lets see if it would work out”
else:
print “Hmm, lets see what happens”

if cars > people: # If there are 3 statements going to be used, add ‘elif’
print “We should take the cars.” # So it goes from ‘if’, ‘elif’ and ‘else’
elif cars < people:
print “We should not take the cars.”
else: # Else does not take any argument. ‘elif’ always serves as a middle man.
print “We can’t decide.”

if buses > cars:
print “That’s too many buses.”
elif buses < cars:
print “Maybe we could take the buses.”
else:
print “We still can’t decide.”

if people > buses: # This always take an argument
print “Alright, let’s just take the buses.”
else: # This does not take any argument please. Take note
print “Fine, lets stay home then.”

Boolean logic, more practice

# Just thought I should let you know Dawn
# You have done well in memorizing the boolean logic
# Yeah, you had few wrongs, 4 of them.
# But you did great actually 🙂
# Thumbs up

# Now we move to the next Python script
# Which is the if-statement
# Game on Dawn

people = 20
cats = 30
dogs = 15
cars = 45
houses = 75

 

# if kofi != ama:
# print “So Kofi is a man, Ama is a girl” # Slim and fair actually 🙂
# Der n) Dawn start dey think 🙂
# Bad boooy. I can see yooooooou 🙂

if people < cats: # This will print because the ‘if definition’ is true.
print “Too many cats! The world is doomed!”

if people > cats: # This will not print because the ‘if definition’ is not true
print “Not many cats! The world is saved!”

if people < dogs: # This ill not print because the ‘if definition’ is not true
print “The world is drooled on!”

if people > dogs: # This will print because the ‘if definition’ is true
print “The world is dry!”

dogs += 5 # This is more or less like adding ‘5’ to the already defined ’15’ dogs

if people >= dogs: # This will print the statement. For ‘dogs = 20, people = 20’
print “People are less than equal to dogs.”

if people <= dogs: # Same here
print “People are less than equal to dogs.”

if people == dogs: # Same here
print “People are dogs” # Dont like this statement actually 🙂

# We are trying some observations here. With regards to space indentation
# Hm, not really a difference. Nothing has changed actually
# No error, whether with a single space or 4 indented spaces
# However we would have to admit that with not even a single indentation
# There would be an error. Yeah Indentation block
# Well i guess we ve to maintain the ‘tab’ indentation

if cats > dogs:
print “Yes cats are in a comfortable lead!”

if cars != houses:
print “Hm, some Dawn movement”

if houses >= dogs:
print “Welcome guys”

# From observation, what the if statement does is that
# When the defined format strings is true, it will print
# The statement down it
# But if its not true, it will not print it

Boolean logic in practice

Ok, so we have some exercise on Boolean logic in here
# Lets do it
# Later on, we would open Python and type it in there to see what we got

True and False

False and True

1 == 1 and 2 == 1

“test” == “test”

1 == 1 or 2 != 1

True and 1 == 1

False and 0 != 0

True or 1 == 1

“test” == “testing”

1 != 0 and 2 == 1

“test” != “testing”

“test” == 1

not (True and False)

not (1 == 1 and 0 != 1)

not (1 != 10 or 3 == 4)

not (“testing” == “testing” and “Dawn” == “Coold Guy”)

1 == 1 and not (“testing” == 1 or 1 == 0)

“chuncky” == “bacon” and not (3 == 4 or 3 == 3)

3 == 3 and not (“testing” == “testing” or “Python” == “Fun”

Learning Logic in Python

# In here we learn logic
# We would give you the basics of the formula
# Yes, to make it simple and easy

# What we learn in here is also known as Boolean Logic

# Anything in ‘OR’ is positive which is true

# Anything in ‘AND’ is negative which is false

# Anything ‘NOT OR’ is the same as ‘AND’ except same inputs which negates
# As in ‘True – True’ False——– ‘False – False’ True

# Anything ‘NOT AND’ is the same as ‘OR’ except same input which negates
# As in ‘True – True’ False——– ‘False – False’ True

# And well if its not True, its False
# If its not False, its True

More practices with functions

# We are doing more practices
# This file on normal day should be run
# We are doing something new
# Doing some more practice and well having fun maybe 🙂

 

def break_words(stuff):
“””This function will break up words for us.”””
words = stuff.split(‘ ‘)
return words

def sort_words(words):
“””Sorts the words.”””
return sorted(words)

def print_first_word(words):
“””Prints the first word after popping it up.”””
word = words.pop(0)
print word

def print_last_word(words):
“””Prints the last word after popping it off.”””
word = words.pop(-1)
print word

def sort_sentence(sentence):
“””Takes in a full sentence and returns the sorted words.”””
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
“””Prints the first and the last words of the sentence.”””
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
“””Sorts the words then prints the first and last one.”””
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)

Using Functions and ‘return’

# Functions can return
# Ok, Dawn this is another step forward
# Towards a good success I guess 🙂
# In here, we introduce a new Python word ‘return’

def add(a, b):
print “\nADDING %d + %d” % (a, b)
return a + b

def subtract(a, b):
print “\nSUBTRACTING %d – %d” % (a, b)
return a- b

def multiply(a, b):
print “\nMULTIPLYING %d * %d” % (a, b)
return a * b

def divide(a, b):
print “\nDIVIDING %d / %d” % (a, b)
return a / b
def node(a, b): # This is Dawn at work
print “\nNode %d > %d” % (a, b)
print 87 > 7
return a / b

 

 

print “Let’s do some math with just functions!”

node = node(87, 7)
age = add(30, 5)
height = subtract(78,4)
weight = multiply(90, 2)
iq = divide(100, 2)

print “Noded: %d” % (node)

print “Age: %d, Height: %d, Weight: %d, IQ: %d” % (age, height, weight, iq)

# A puzzle for the extra credit, type it in anyway.
print “Here is a puzzle.”

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print “That becomes: “, what, “Can you do it by hand?”

# Ok, Dawn so here we dissolves the puzzle
what0 = divide(50, 2)
what1 = multiply(180, 25)
what2 = subtract(74, 4500)
what3 = add(35, -4426)
print “That becomes: “, what0 + what1 + what2 + what3, “Puzzled dissolved?”

raw_input (“\n>>> “)

hi = subtract(10, 100)

print “Hi is used and there is some subtraction which is “, hi, “or?”

Dealing with functions and files

# We are dealing with functions and files
# A build on functions, variables and a bit of Maths 🙂
# Always remember with functions def, always add a colon
# We should research online for the more usage of the ‘seek’ module

from sys import argv # We are importing an argument variable

script, input_file = argv # We would need one argument thus a file

def print_all(f): # We decided to use print_all def and ‘f’ as a partnering variable
print f.read() # ‘f’ here is used for the file. Where ‘read’ prints the contents on the screen

def rewind(f): # Same here, we rewind the file
f.seek(0) # Instead the module ‘seek’ is used to do that

def print_a_line(line_count, f): # Another definition though for ‘print_a_line’ with 2 variables
print line_count, f.readline() # Attempts to read line by line using var ‘line_count’ of ‘f’

current_file = open(input_file) # File tends to be opened

print “First lets print the whole file:\n”

print_all(current_file)

print “Now let’s rewind, kind of like a tape.”

rewind(current_file)

print “Let’s print three lines:\n”

current_line = “>>>” # This command prints the first line in the file
print_a_line(current_line, current_file) # ‘current_line’ equals to the first line in the file

current_line = “>>>” # This moves to the second line in the file
print_a_line(current_line, current_file) # ‘current_line’ equals to print second line in the file

current_line = “>>>” # This moves to the third line
print_a_line(current_line, current_file) # ‘current_line’ equals to print third line in the file

More Functions and Variables

# Dawn, sources say that the variables we used in our functions at test18.py
# Are not connected to the variables in our script 😦
# What do you make of it?
# Well this is an exercise to get us thinking

# Dawn do you remember test18.py? Its a similar to the one we ve here
# Here, cheese_crackers is defined. Any word could be used instead
# And now the variables (cheese_count, boxes_of_crackers)
def cheese_and_crackers(cheese_count, boxes_of_crackers): # This is a function too
print “You have %d cheeses!” % cheese_count # This is the variable % cheese_count
print “You have %d boxes of crackers!” % boxes_of_crackers # This too
print “Man thats enough for a party!”
print “Get a blanket.\n” # Remember this? It allows spacing in sentences

print “We can just give the function numbers directly:”
cheese_and_crackers(20, 30) # And well, we can use numbers too
# Same thing as previous.
# The first number goes to ‘You have %d cheeses!’
# The second number applies to ‘You have %d boxes of crackers!”

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) # You would realise that the
# The Main def is always used
# cheese_and_crackers
# It matters most
# Don’t forget please Dawn

print “We can even do math inside too:” # Lets do some Maths. Hope you dont mind 🙂
cheese_and_crackers(10 + 20, 5 + 6) # As additions are used here,
# We can use other symbols as well (-, /, x, +)

print “And we can combine the two, variables and math:” # Variables and Maths
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) # Pure sample

# This is Dawn inputs 🙂
print “Dawn loves coding and hacking. Its in his DNA”
cheese_count = 87
boxes_of_crackers = 250

cheese_and_crackers(cheese_count, boxes_of_crackers) # When using a def, dont use ‘=’

def halo(code, clients, warrior): # Remember to always add the colon: when using def please
print “Boss Dawn, congratulations! You are the richest man on earth!”
print “Ok guys, lets %s” % code
print “Champ Dawn, we heard you got %s” % clients
print “Ok, no problem. By God’s grace we are on course”
print “Cobra7 aka Dawn, real %s are you” % warrior
print “Game on boys, we ain’t stopping yet!\n”

halo(“code”,”clients”,”warrior”) # Over here, we give real values to the variables

def c(c, g, p): # We define a new function ‘c’ which would take 3 arguments (c, g, p)
print “Welcome my people to Dawn’s world of %s!” % c
print “Well its all good %s!” % g
print “This is what he has %s for!” % p
print “No stopping Eagle99”
print “Hurray! We are on course!\n”

c(“coding”,”guys”,”passion”)

print “Hi guys” # This is optional, without it, still 3 statements would be printed
c(“Reg_Sec”,”yeah”,”always killed himself”) # If function ‘c’ has the needed arguments

# Interestingly, we can use as many functions as we want
# All we needed is to to define it and add the variables as many as we want
# Then ‘edit’ the content for other structures if we want repeated ones as in ‘def c’
# Bingo, we should be done and dusted
# Yes Boss Dawn