Range

Calvin (Deutschbein)

W3Wed: 11 Sep


Announcements

  • By MONDAY 11:59 PM: "Problem Set 2: Checkers" assignment.
    • One Karel problem (probably) using "if" and "while"
    • One Python problem using arithmetic and (probably) "for"
    • One writing problem.
  • Sections reminder: Wed & Thurs PM.
    • Problems: email jjrembold@, you may cc ckdeutschbein@
  • PS1: Crushed. Mission: All A's is a go! 🚀

Today


    range()
    and / or / not
    def / return

range()

The Anapurna range of the Himalayas (that mountain is ~5 miles tall)

range()

  • Remember this: def turn_right(): for i in range(3): turn_left()
  • Like the Anapurna range (look it was just a cool picture okay) of the Himalayas...
  • A range of some integer n is part of the number line (from neg to pos infinity)
  • In Karel we didn't worry about the numbers, but we are Arithmetic Appreciators™ now

range()

  • The easiest way to examine range() is with print() >>> print(range(10)) range(0, 10)
  • Um... what?
  • Okay fun fact - range just starts at zero by default, but we can change that... >>> print(range(5,10)) range(5, 10)
  • Variety of uses for this - usually reading a file and cutting off headings for me.

range()

  • The easiest way to examine range() is with print()...
  • But we shouldn't just print the range itself!
  • We often use 'range()' with 'for' >>> for i in range(3): # smaller so its easy to work with ... print(i) ... 0 1 2
  • Recall: we say "range(3)" but we get "range(0,3)"
  • So ranges include the first number and stop right before the second.
  • Why include zero?
    • What is the first hour of the day? (12hr or 24hr clock?)
    • What is the fewest number of apples you can have?

range()

  • The easiest way to examine range() is with print()... >>> for i in range(4,6): ... print(i) ... 4 5
  • We can also do non-zero starts.

range()

  • Often we use for and range when we want to check for something, like multiples of 5. >>> for i in range(25,50): ... if i % 5 == 0: ... print(i) ... 25 30 35 40 45
  • One can imagine how this may be useful
    • Check every 50th entry to see if it seems valid.
    • Divide up some tasks among n people.

range()

  • There is a forbidden (it's not forbidden) third value that can be supplied to a range, that we usually call the step size: >>> for i in range(25,50,10): ... print(i) ... 25 35 45
  • I use this sometimes, but usually inner if is fine, unless...

range()

  • By the way you can make any of these values negative. >>> for i in range(15,10,-2): ... print(i) ... 15 13 11
  • For whatever reason, I'm always reversing orders:
    • Review the homeworks that were turned in last, first
    • Use reverse alphabetical order to offset years of alphabetization
    • In a big file: start reading from the end (latest entries)

range()

  • I like range... for i in range(15,10,-2): print(i)
  • ... but also like while: i = 15 while i > 10: print(i) i = i - 2
  • These are identical. Use what you like.
  • You will need* to use on PS2 Problem 2.
    • There is a very challenging way to use only 'if'
    • I don't have it working yet but it looks possible. Office hours.

Today


    ✓: range()
    and / or / not
    def / return

Booleans

  • There's these things called booleans. >>> type(1) <class 'int'> >>> type(1.0) <class 'float'> >>> type(1==1.0) <class 'bool'> >>> type(False) <class 'bool'>
  • 'True' and 'False' are booleans and nothing else is a boolean.

Booleans

  • The is such thing as boolean arithmetic
  • Easy ones:
    a and b True if 'a' and 'b' are True
    a or b True if 'a' or 'b' be True
    not aTrue if 'a' is not True ('a' is False)
>>> a = True >>> b = False >>> a and b False >>> a or b True >>> not a False

In general: if you aren't sure, just check.

Truthiness

  • If Python expects a boolean, like in an 'if', it turns to turn whatever it gets into a boolean.
  • You can test by writing 'if' statements, or by turning things into booleans with 'bool()'
  • This is sometimes called "truthiness" - I don't love it (do you?)
  • Here's some "falsy" values
    0Non-zero values are treated like True
    0.0Non-zero values are treated like True
    ""Imagine if we want to print nothing.
    NoneNone is the return value of a function with no return (???)
>>> if 0: ... print('hi') ... >>> if "": ... print('hi') ... >>> if print(): ... print('hi') ... >>> if .1: ... print('hi') ... hi >>> bool(0) False

In general: if you aren't sure, just check.

Truthiness

  • While goofy, it allows for some shortcuts.
  • Let's find some multiples of 3.
    val % divIs non-zero if 'val' divided 'div' has some remainder
    val % div == 0Is True if 'val' divided 'div' has no remainder
    bool(val % div)Is True if 'val' divided 'div' has some remainder
    not val % divIs True if 'val' divided 'div' has no remainder
>>> for i in range(10,20): ... if i % 3 == 0: ... print(i) ... if not i % 3: ... print(i) ... 12 12 15 15 18 18 >>>

All together

    >>> for i in range(0,50,5): ... if not i % 3: ... print(i) ... 0 15 30 45 >>>
  • Same as: >>> for i in range(50): ... if i % 3 == 0 and i % 5 == 0: ... print(i)

I took a whole class in undergrad on "counting two ways" (it was fun!)

Disambiguate

  • If you get confused looking at something: >>> for i in range(50): ... if i % 3 == 0 and i % 5 == 0: ... print(i)
  • Add parenthesis: >>> for i in range(50): ... if ((i % 3) == 0) and ((i % 5) == 0): ... print(i)
  • Or use variables. >>> for i in range(50): ... rem3 = (i % 3) rem5 = (i % 5) if not rem3 and not rem5: # 'not' any remainder, ... print(i)

Today


    ✓: range()
    ✓: and / or / not
    def / return

Type hints

  • You basically know what types are now:
    • int
    • float
    • bool
    • Whatever "hello world" is - we'll get there
    >>> type('hi') # to be continued...
  • The ONLY way I wrapped my head around functions was by remembering this: def add(x,y): return x + y
  • Was a function that takes two ints and returns one int
  • Call this a "function type"

Type hints

  • PS2 Problem 2 asks for a function that takes two ints and returns one int.
  • And it ALSO prints a whole bunch of stuff (ints, more or less - we'll get there)
  • In Python we can state what the function type is with "type hints":
    • This are basically a special kind of comment - they don't affect code.
    def divide(x, y): # no hints return x / y def intdiv(x:int, y:int) -> int: # yes hints return x // y
  • You are not required to use type hints in this class...
  • But my brain requires me to use function types to teach this class.

Type hints

  • We term the things that go into function "parameters" or "arguments"
    • Sometimes we say "inputs" but that can get confusing - stay tuned.
  • We term the things that come out of functions "return values"
  • A function with no return value, like 'print', returns "None" - which is a special value and a special type. >>> type(None) <class 'NoneType'> >>> type(print('hi')) hi <class 'NoneType'>
  • Why do I bring this up?

Type hints

  • You should only ever omit return values *on purpose* (realistically: never until you are an expert) >>> def print_sum_of(x:int,y:int) -> None: ... print(x + y) ... >>> type(print_sum_of(3,4)) 7 <class 'NoneType'> >>>
  • Code like this will be easier to fix if you try to, say: >>> x = print_sum_of(3,4) 7 >>> x >>>
  • Wait - I said print_sum should return None! Why am I saving that in x!

Exercise

  • Do "Problem Set 2: Checkers"
    • One Karel problem (probably) using "if" and "while" def check_move(): if no_beepers_present(): move() put_beeper() else: move()
    • One Python problem using arithmetic and (probably) "for" def divisible_by_six_or_seven(x:int, y:int) -> int: # there must be the possibility of something printing in here return # must return an int
    • One writing problem

Announcements

  • By MONDAY 11:59 PM: "Problem Set 2: Checkers" assignment.
    • One Karel problem (probably) using "if" and "while"
    • One Python problem using arithmetic and (probably) "for"
    • One writing problem.
  • Sections reminder: Wed & Thurs PM.
    • Problems: email jjrembold@, you may cc ckdeutschbein@
  • PS1: Crushed. Mission: All A's is a go! 🚀