Guide 2
Guide 2
Guide 2
• The Python type bool has two values: True and False.
• The comparison operators take two values and produce a Boolean
value.
• There are also logical operators that produce Boolean
values: and, or, and not.
Operators
Precedence(https://docs.python.org/3/reference/expressions.html#oper
ator-precedence)
The minimum passing grade is 50. Variable grade refers to the grade
for a student. Do the expressions below correctly represent the English
sentence: "The student passed.”?
grade >= 50
not (grade < 50)
50 > grade
not not (grade >= 50)
The minimum passing grade is 50, the minimum grade for "A" is 80 and
variables math_grade, bio_grade, and cs_grade represent a student's
final grades in three courses. Write the expression that represents the
English sentence:
The student passed all their courses and earned at least one A.
Conditional statements
1. Change the numbers of cars, people, and trucks, and then trace
through each if-statement to see what will be printed.
2. Try some more complex Boolean expressions like cars > people or
trucks < cars.
def howbig(n):
if n > 100:
return "It's huge."
elif n > 10:
return "It's pretty big."
else:
return "It's not so big.“
print(howbig(150))
Correct the syntax error for each of the code below if there is any.
while-loop for repeated tasks (SU2 Chapter 1.3, Textbook Video and
Exercise 33)
num = 6
while num > 0:
num = num - 2
print(num)
Write a program to ask the user for a "yes" or "no" input and continue
asking until the user gives a valid response. Print the answer.
validating user inputs: The program expects numeric inputs from the
user for the two variables height, weight. What if the user inputs
non-numeric characters?
grades[1]
grades[1:2]
grade[-1]
grade[3:]
len(grades)
min(grades)
max(grades)
sum(grades)
Operations on lists
Operations on lists
(https://docs.python.org/3/library/stdtypes.html#mutable-sequence-type
s)
grades[1]
grades[1:2]
grade[-1]
grade[3:]
len(grades)
min(grades)
max(grades)
sum(grades)
lists and for-loop (SU2 Chapter 2.3, Textbook Video and Exercise 32)
What’s the first line to be printed after executing the code below?
What will be printed if s is initialized as an empty string?
s = ’good day'
for char in s:
print(char)
There are problems where knowing the value of the items in a list or
the characters in a string is not enough; we need to know where it
occurs (i.e. its index).