2018-02-02 HW

posted Feb 2, 2018, 6:25 AM by Konstantinovich Samuel   [ updated Feb 3, 2018, 4:25 PM ]

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>


#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


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 (use your stuy.edu for consistency) 

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

2. Share your work with my account:  konstans@stuy.edu 
This allows me to see if you complete assignments!

3. Always log in before you work! 

4. Complete these problems on the PYTHON half of coding bat:
make10 ,   sleep_in,   monkey_trouble