defadd(a,b):print("ADDING%d+%d\n"%(a,b))return a+b
defsubtract(a,b):print("SUBSTRACT%d-%d\n"%(a,b))return a-b
defmultiply(a,b):print("MULTIPLY%d*%d\n"%(a,b))return a*b
defdivide(a,b):print("DIVIDE%d/%d\n"%(a,b))return a/b
print("Let's do some math with just functions")
age = add(30,8)
height = subtract(78,4)
weight = multiply(90,5)
iq = divide(80,2)print("age:%d,height:%d,weight:%d,iq:%d"%(age,height,weight,iq))print("Here is a puzzle")
what = add(age,subtract(height,multiply(weight,divide(iq,2))))print("That become:",what,"can you do it by hand?")
Let's do some math with just functions
ADDING30+8
SUBSTRACT78-4
MULTIPLY90*5
DIVIDE80/2
age:38,height:74,weight:450,iq:40
Here is a puzzle
DIVIDE40/2
MULTIPLY450*20
SUBSTRACT74-9000
ADDING38+-8926
That become: -8888.0 can you do it by hand?
print("Let's practice everything.")print('You\'d need to known \'bout escapes with \\ that do \n newline and \t tabs')
poem ="""
\t The 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-6print("This should be five:%s"%five)defsecret_formula(started):
jelly_beans = started*500
jars = jelly_beans/1000
crates = jars/100return jelly_beans,jars,crates
start_point =10000
beans,jars,crates = secret_formula(start_point)print("With a starting point of:%d"%start_point)print("We'd have %d beans,%d jars,%d crates."%(beans,jars,crates))
start_point = start_point/10print("We can also do that this way:")print("We'd have %d beans,%djars,%dcrates."%secret_formula(start_point))
Let's practice everything.
You'd need to known 'bout escapes with \ that do
newline 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 jars,50 crates.
We can also do that this way:
We'd have 500000 beans,500jars,5crates.
defbreak_words(stuff):"""This function will break up words for us."""
words = stuff.split(' ')return words
defsort_words(words):"""Sorts the words."""returnsorted(words)defprint_first_word(words):"""prints the first word after popping it off."""
word = words.pop(0)print(word)defprint_last_word(words):"""prints the last word after popping it off."""
word = words.pop(-1)print(word)defsort_sentence(sentence):"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)return sort_words(words)defprint_first_and_last(sentence):"""prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)defprint_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)
sentence ="All good things come to those who wait."
words1 = break_words(sentence)print(words1)print(sort_words(words1))
print_first_word(words1)
print_last_word(words1)
print_first_and_last(sentence)
print_first_and_last_sorted(sentence)
['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
All
wait.
All
wait.
All
who