W4 Tutorial

Coming Up

  1. Booleans, Relational and Logic Operators, and Conditionals (if statements)
  2. Sequences, Indexing and Slicing
  3. Functions

1 Booleans, Relational and Logic Operators, and Conditionals

Discussion 1: Booleans

D1: What is “Boolean”? What values does it store? Can other types be converted to it?

Discussion 2: Relational and Logical Operators

D2: For each of the following, identify whether it is: (a) a Boolean value; (b) a relational operator; or (c) a logical operator.

  1. ==:
  2. !=:
  3. or:
  4. True:
  5. >:
  6. and:
  7. >=:
  8. <:
  9. False:
  10. <=:
  11. not:

📝 Operators

  • the following is ordered from Highest to Lowest precedence
  • Arithmetic Operators
    • () brackets
    • ** exponent
    • % modulus/remainder
    • // floor/integer division
    • / division
    • * multiplication
    • - subtraction (also a unary operator; make number negative)
    • + addition (also a unary operator; make number positive)
  • Relational Operators
    • compare two values to produce a truth (boolean) result
    • == equal to
    • != not equal to
    • < less than
    • > greater than
    • <= less than or equal to
    • >= more than or equal to
    • in
  • Logical Operators
    • combine Boolean values to return a single truth value
    • not
    • and
    • or

Discussion 3: Conditionals

D3: How do we use an if statement? What are the variants? How do we know what is contained inside it and what is after?

📝 if...elif...else Structure

if <condition_1>:
  <statements/operations to be executed if condition1 is True>
elif <condition_2>:
  <statements/operations to be executed if condition2 is True>
...
elif <condition_x>:
  <statements/operations to be executed if conditionx is True>
else:
  <statements/operations to be executed if all conditions are False>

Exercise 1

E1: Evaluate the following truth expressions:

  1. True or False:
  2. True and False:
  3. False and not False or True:
  4. False and (not False or True):

📝 Logical Operator Truth Tables

aba and ba or b
FalseFalseFalseFalse
FalseTrueFalseTrue
TrueFalseFalseTrue
TrueTrueTrueTrue
anot a
FalseTrue
TrueFalse

Exercise 2

E2: For each of the following if statements, give an example of a value for var which will trigger it and one which will not.

  1. [object Object]

  2. [object Object]

  3. [object Object]

  4. [object Object]

Exercise 3

E3: What’s wrong with this code? How can you fix it?

Exercise 4 (assignment VS equality)

E4: What’s wrong with this code? How can you fix it?

2 Sequences, Indexing and Slicing

Discussion 4: Sequences

D4: What is a “Sequence”? What sequences have we seen so far? What operators can we use with sequences?

📝 Sequence Operations

Discussion 5: Indexing

D5: What is indexing? How can you do it?

Dicussion 6: Slicing

D6: What is slicing? How can you do it?

Exercise 5

python
index012345
negative index-6-5-4-3-2-1
  • length: 6
  • Slicing: [start:end:step]

E5: Evaluate the following given the assignment s = "python"

  1. s[1]:
  2. s[-1]:
  3. s[1:3] + s[3:5]:
  4. s[10]:
  5. s[10:]:
  6. s[-4:-2]:
  7. s[:-4]:
  8. s[::2]:
  9. s[::-1]:

3 Functions

Discussion 7: Functions

D7: What is a “function”? How do we call (use) one? How do we define one ourselves?

📝 Function (and Docstring) Structure

CONSTANT = 0 # declare constants before all functions
def <function-name>(<parameters>):
    """
    docstring describing what the function does.
    """
    <function-body>
  • Ways to structure docstrings

    • short and sweet one-liners

      • example:
        def multiplier(a, b):
            """Takes in two numbers, returns their product."""
            return a*b
        
    • detailed params/args, returns, raises, example

      • examples:

        def add_binary(a, b):
            """
            Returns the sum of two decimal numbers in binary digits.
        
            Parameters
            ----------
            a (int): A decimal integer
            b (int): Another decimal integer
        
            Returns
            -------
            binary_sum (str): Binary string of the sum of a and b
            """
            #...
        
  • PEP257 - Docstring Conventions

  • What are the most common Python docstring formats? [closed] | stackoverflow

Discussion 8: Return

D8: What does it mean to “return” a value from a function and why would we want to? Does a function always need a return value?

Discussion 9: Function of Functions

D9: Why should we use functions? Could we live without functions?

Discussion 10: Brackets and Functions

D10: Why are brackets important when calling a function? Are they needed even if it takes no arguments?

Exercise 6

E6: What’s wrong with this code? How can you fix it?

Coding Problems

Problem 1

Problem 2

Problem 3