Skip to content

W4: Booleans, Operators, Conditionals, Sequences, Indexing, Slicing, and Functions

Coming Up

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

Booleans, Relational and Logic Operators, and Conditionals

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

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

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?

E1: Evaluate the following truth expressions:

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.

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

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

Sequences, Indexing and Slicing

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

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

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

E5: Evaluate the following given the assignment s = “python”

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

Functions

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

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?

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

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

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

Coding Problems

Problem 1

Problem 2

Problem 3