2017-02-02 HW

posted Feb 2, 2017, 7:23 AM by Samuel Konstantinovich   [ updated Feb 2, 2017, 10:14 AM ]
 Goal: Solve basic python problems

Variables in python:
There is no distinction between create/change variables in Python (like  set / let in Netlogo)

To create or change a variable in python we use the assignment operator the equals sign:

x = 3 #create a variable x make it store the value 3

name = "bob" 

pi = 3.14159

x = x + 1 #change x to: the old value of x plus 1

pi = 3 #change the value of pi

Booleans in python:

Python booleans are written as True and False. Capitalization matters.

The logical operators are:  and , or (they only work on booleans!)
The comparative operators:   
    < , >   
    <= , >=   
    == (equals)   
    !=  (not equals)   

QUESTION: Notice equals is written with two equal signs, why is this? 


Functions in python :
There is no distinction between functions/procedures in python (like to / to-report)

def <function_name> ( <comma separated parameters> ) :
    return <value or expression>

e.g.

#this is a simple function
def  foo(a,b):
    return a + b

#this shows you can have multiple statements in a function
def bar(nom):
    x = 3 * nom
    return  x == 12 #this is how you return a boolean

#this does not return a value explicitly. The function call has a special value when this happens.
def bah(name):
    print "Hello " + name

Classwork:

Boolean functions:  Tend to be tricky to students at first. They are not so bad after you practice a while.



Write a function between(number,a,b) that returns true when the number is between a and b. Assume a < b. 
Do not use   a<b<c syntax, use and/or.
Modify this to work when a is not always less than b.


Homework: (codingbat homework is not numbered)
1. Sign up for an account on codingbat.com

2. Change your NAME in the preferences. Use the format   period,lastname,firstname
(copy paste, and change the value below)
  "01,Goldman,Kenji" 
( no spaces, if your name has a space or quote, remove it.)


2. Always log in before you work! 

3. Complete these problems on the PYTHON half of coding bat:
Warmup-1
make10 ,   sleep_in,   monkey_trouble
Comments