Guide 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

ANL251 Python Programming

July 2018 Semester

© 2018 Singapore University of Social Sciences. All rights reserved.


Study Unit 2
Control Flow and Lists

© 2018 Singapore University of Social Sciences. All rights reserved.


Learning Outcomes and Learning Resources

1. Compose appropriate Boolean expressions for given scenarios


– SU2 Chapter 1.1
– Textbook Videos and Exercises 27, 28
3. Construct conditional control flow
– SU2 Chapter 1.2
– Textbook Videos and Exercises 29 ~ 31
4. Use while-loop for repeated tasks
– SU2 Chapter 1.3
– Textbook Video and Exercise 33

© 2018 Singapore University of Social Sciences. All rights reserved.


4. Select lists as the appropriate data structures for given scenarios,
and implement subsetting on Lists
– SU2 Chapters 2.1, 2.2
– Textbook Videos and Exercises 34, 38
– https://docs.python.org/3/library/stdtypes.html#sequence-types-li
st-tuple-range
– https://docs.python.org/3/library/stdtypes.html#mutable-sequenc
e-types
5. Solve problems using Python lists and for-loop
– SU2 Chapter 2.3
– Textbook Video and Exercise 32

© 2018 Singapore University of Social Sciences. All rights reserved.


1. Boolean Expressions

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

• Boolean expressions (SU2 Chapter 1.1, Textbook Videos and


Exercises 27, 28)

• 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.

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

• What are the Python data types we have learned so far?


• What are the Python operators we have learned so far

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

Operators
Precedence(https://docs.python.org/3/reference/expressions.html#oper
ator-precedence)

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz

not 80 >= 50 or 90 >= 50


not ((80 >= 50) or (90 >= 50))
not (80 >= 50)
(80 >= 50) and (70 <= 50)
(80 >= 50) or (70 <= 50)
50 >= 50 and 85 >= 50

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

The minimum passing grade is 50. The


variables math_grade, bio_grade, and cs_grade represent a student's
final grades in three courses. Write the boolean expressions to
correctly represent each of the English sentences below:

The student passed none of the courses


The student passed at least one of the courses.
The student passed all of the courses.
The student passed some but not all of the courses

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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.

© 2018 Singapore University of Social Sciences. All rights reserved.


2. Conditional Statements

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

Conditional statements (SU2 Chapter 1.2, Textbook Videos and Exercises 29


~ 31)

Note: Each indentation level should be indented by 4 spaces. As Python requires


indentation to be consistent, it is important not to mix tabs and spaces. You should
never use tabs for indentation. Instead, all indentation levels should be 4 spaces.

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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.

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz

What is printed when the code below is executed?

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))

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz

Correct the syntax error for each of the code below if there is any.

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

Simplify the code below.

if temp > 28:


print(True)
elif temp < 12:
print(True)
else:
print(False)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

Simplify the code below.

if temperature > 28:


if money > 0.99:
print("I'm buying a lemonade.")

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz

Variables grade1 and grade2 represent grades in two courses. Write


the code to count the number of courses passed (with a 50 or higher).

© 2018 Singapore University of Social Sciences. All rights reserved.


© 2018 Singapore University of Social Sciences. All rights reserved.
3. while-loop

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

while-loop for repeated tasks (SU2 Chapter 1.3, Textbook Video and
Exercise 33)

Note: while-loop is a type of unbounded loop, i.e. it does not stop if we


set an always True condition.

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz

What will be printed when executing the code below?

num = 6
while num > 0:
num = num - 2
print(num)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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.

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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?

Study Handling Exceptions and improve the code above.

© 2018 Singapore University of Social Sciences. All rights reserved.


4. Operations on lists

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

Operations on lists (SU2 Chapters 2.1, 2.2, Textbook Videos and


Exercises 34, 38)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion
Common sequence operations on lists (also applicable to tuple in SU3)
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tupl
e-range

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz
If the variable grades refers to [80, 90, 70, 24], what does each of the
following evaluate to?

grades[1]
grades[1:2]
grade[-1]
grade[3:]
len(grades)
min(grades)
max(grades)
sum(grades)

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

Operations on lists

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

Operations on lists
(https://docs.python.org/3/library/stdtypes.html#mutable-sequence-type
s)

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz
If the variable grades refers to [80, 90, 70, 24], what does each of the
following evaluate to?

grades[1]
grades[1:2]
grade[-1]
grade[3:]
len(grades)
min(grades)
max(grades)
sum(grades)

© 2018 Singapore University of Social Sciences. All rights reserved.


Quiz
What will grades refer to after this code is executed?

grades = [80, 70, 60, 90]


grades.sort()
grades.insert(1, 95)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion
Write a program to add each user input into a list until the user types
enter to end.

© 2018 Singapore University of Social Sciences. All rights reserved.


4. Lists and for-loop

© 2018 Singapore University of Social Sciences. All rights reserved.


Recap

lists and for-loop (SU2 Chapter 2.3, Textbook Video and Exercise 32)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

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)

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

for-loop over indices:

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).

Question 1: Write a program to shift each item in a list one position to


the left and shift the first item to the last position.

Question 2: Write a program to count the corresponding characters of


the two strings that are the same character. Your program may assume
the two strings have the same length.

© 2018 Singapore University of Social Sciences. All rights reserved.


Choosing Test Cases

© 2018 Singapore University of Social Sciences. All rights reserved.


Discussion

© 2018 Singapore University of Social Sciences. All rights reserved.


Choose test
cases to
thoroughly test
the code in Figure
2.2

© 2018 Singapore University of Social Sciences. All rights reserved.


© 2018 Singapore University of Social Sciences. All rights reserved.

You might also like