W4: Booleans, Operators, Conditionals, Sequences, Indexing, Slicing, and Functions
Coming Up
- Booleans, Relational and Logic Operators, and Conditionals (
ifstatements) - Sequences, Indexing and Slicing
- Functions
Booleans, Relational and Logic Operators, and Conditionals
D1: What is “Boolean”? What values does it store? Can other types be converted to it?
# ints -> bool
# print(bool(0)) # 0 converts to False
# print(bool(1)) # all other values convert to True
# floats -> bool
# print(bool(0.0)) # 0.0 converts to False
# print(bool(3.1415)) # all other values convert to True
# str -> bool
# print(bool("")) # empty string converts to False
# print(bool(" ")) # all other values convert to True
# print(bool("Hi!")) # all other values convert to True
D2: For each of the following, identify whether it is: (a) a Boolean value; (b) a relational operator; or (c) a logical operator.
You can type your answer to D2 here...
1. `==`:
2. `!=`:
3. `or`:
4. `True`:
5. `>`:
6. `and`:
7. `>=`:
8. `<`:
9. `False`:
10. `<=`:
11. `not`:
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?
You can type your answer to D3 here...
E1: Evaluate the following truth expressions:
You can type your answer to E1 here...
1. `True or False`:
2. `True and False`:
3. `False and not False or True`:
4. `False and (not False or True)`:
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.
You can type your answer to E2 here...
1. `if 10 > var >= 5:`
2. `if var[0] == "A"and var[-1] == "e":`
3. `if var in ["VIC", "NSW", "ACT"]:`
4. `if var:`
E3: What’s wrong with this code? How can you fix it?
letter = input("Enter a letter: ")
if letter == 'a' or 'e' or 'i' or 'o' or 'u':
print("vowel")
else:
print("consonant")
E4: What’s wrong with this code? How can you fix it?
eggs == 3
if eggs = 5:
print("spam")
else:
print("not spam")
Sequences, Indexing and Slicing
D4: What is a “Sequence”? What sequences have we seen so far? What operators can we use with sequences?
You can type your answer to D4 here...
D5: What is indexing? How can you do it?
You can type your answer to D5 here...
D6: What is slicing? How can you do it?
You can type your answer to D6 here...
E5: Evaluate the following given the assignment s = “python”
| p | y | t | h | o | n | |
|---|---|---|---|---|---|---|
| index | 0 | 1 | 2 | 3 | 4 | 5 |
| negative index | -6 | -5 | -4 | -3 | -2 | -1 |
- length: 6
- Slicing:
[start:end:step]
You can type your answer to E5 here...
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]`:
# only use the code block for checking your answers!
Functions
D7: What is a “function”? How do we call (use) one? How do we define one ourselves?
You can type your answer to D7 here...
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?
def example_1():
return 1
print("abc")
return 2
return 3
print(example_1) # what do you think this will return?
D9: Why should we use functions? Could we live without functions?
You can type your answer to D9 here...
D10: Why are brackets important when calling a function? Are they needed even if it takes no arguments?
You can type your answer to D10 here...
E6: What’s wrong with this code? How can you fix it?
def calc(n1, n2):
answer = n1 + (n1 * n2)
print(answer)
num = int(input("Enter the second number: "))
result = calc(2, num)
print("The result is:", result)
Coding Problems
Problem 1
"""
P1: Write a function which takes a string as a single argument, and returns a
shortened version of the string consisting of its first three letters and then
every second letter in the rest of the word. shorten('uncalled') should return
'uncle'.
"""
Problem 2
"""
P2: Write a function which takes a sentence as a single argument (in the form of
a string), and evaluates whether it is valid based on whether the first letter is
capitalised and the last character is a full stop. It should return a Boolean
value True or False.
"""
Problem 3
"""
P3: Write a program which asks the user for two numbers and an operator out of
+, -, / and * and performs that operation on the two numbers, printing the
result.
"""