1 of 20

ISA 281

Exam 1 Review

2 of 20

Free Python Resources

This material is outside the course and will not be tested upon.

​

While it's not part of the course, Python for Data Analysis is a great resource for improving your programming skills(plus it’s Free !). It covers some essential Python libraries(Numpy, Pandas and Matplotlib) and I highly recommend reading it. Continuous learning is always better than sprint learning.

  • NumPy
  • Pandas
  • Matplotlib

3 of 20

Agenda

  • Review Overview
  • Variables
  • Operators
    • Arithmetic
    • Assignment
    • Relational
    • Logical
  • if statements
    • Nested if
  • For Loop
  • While Loop

4 of 20

How things will work

Slides

Practice Notebook

Slides and practice notebook cover the same topics in order. Make a copy of the exam1_review.ipynb from canvas.

5 of 20

Make a copy of this document

Make a copy of this document

​

Files > Save a copy in Drive

Collab Link available in Canvas under Exam Review

6 of 20

Some things before we begin

  • Download the Jupyter notebook from TA Resources in Canvas
  • Don’t suffer in silence. Raise your hand and ask questions wherever you stumble.
  • Ask me to repeat if you don’t understand something. I will be more than happy to help.
  • I will stay back after the session and answer questions.
  • Some Common Terms
    • Calling the Variable/function
    • ​

7 of 20

Pseudocode

  • Pseudocode: fake code
  • Informal language that has no syntax rule
  • Not meant to be compiled or executed
  • Used to create model program

Example

​

IF HoursWorked > NormalMax THEN

Display overtime message

ELSE

Display regular time message

ENDIF

Source: Cal Poly

8 of 20

Variables

  • Name that represents a value stored in the computer memory.
  • General format is variable = expression

​

​

  • A variable name must start with a letter or the underscore character.
    • Illegal - “_salary”
  • A variable name cannot start with a number.
    • Illegal - “5_salary”
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
    • Illegal- “$alary”
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name cannot be any of the Python keywords.
    • Illegal - “for = 55”

9 of 20

print()

The print() function in Python is used to output text to the console. It has several parameters that you can use to control its behavior:

​

```python

print(*objects, sep='', end='\n', file=None, flush=False)

```

​

10 of 20

Operators

Arithmetic

Relational

Logical

Assignment

1+1

x=1

x>2

True and True

11 of 20

Python Arithmetic Operators

a + b

Sum a and b (7)

a - b

Subtract b from a (-1)

a * b

a times b (12)

a / b

a divided by b (0.75)

a // b

Integer part of a divided by b (0)

a % b

Rest of a divided by b (3)

a ** b

a to the power of b (81)

Cheat Sheet

25 / 6 =4.167

return round-down(4.167)

Floor Division

Mod (Remainder)

27 / 5 = 5*5 + 2

Practice Question Exam Style - Augmented Assignment 1

12 of 20

Python Assignment Operators

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x - 3

Same as

Same as

Same as

Example

Augmented Operators

13 of 20

Python Relational Operators

> and >=

Greater than and greater than equal to

< and <=

Less than and Less than equal to

==

Equal to

!=

Not Equal to

Example

“=” always comes in the end of a relational operator

14 of 20

Python Logical Operators

Operator

Description

Example

and

Returns True if both statements are true

x < 5 and x < 10

or

Returns True if one of the statements is true

x < 5 or x < 4

not

Reverse the result, returns False if the result is true

not(x < 5 and x < 10)

Practice Question Exam Style - Logical 1

15 of 20

If statements

Conditions are either True/False

16 of 20

Nested if Statements

if x == y:

print('x and y are equal')

else:

​

if x < y:

print('x is less than y')

else:

print('x is greater than y')

Outer

Inner

Nested loops can be hard to read when they get too long. Is there any way we can use just 1 level of if statements and get the job done. Hint - Logical Operators and elif

This can be asked as a direct question on the exam

  • In-class Coding Exercise 1 : Number Classification (Nested `if`)
  • In-class Question Exam Style - 1

17 of 20

For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

Example

​

For in some Iterable Object

Do this for item in object

​

18 of 20

For Loop - break and range()

  • The break directive in a loop exits the loop immediately. Loops have their standard way of exiting.

​

  • The break provides an extra way to exit the loop if some special condition occurs. Usually the break is put inside an if that checks for some condition.

​

  • Break also applies to while
  • Range is a built-in generator, which generates sequences of integers.
  • The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
  • range(start, stop, step=1)
  • Practice Question Exam Style - range 1
  • Coding exercise 3 - For Loop and If

Break

range(start, stop, step=1)

19 of 20

While Loop

  • With the while loop we can execute a set of statements as long as a condition is true.
  • In order for a loop to stop executing, something has to happen inside the loop to make the condition false
  • Will never execute if condition is false to start with

Coding Exercise: Hard while Loop problem

20 of 20

Feedback