Syntax & Formatting
If Statements
Get Me the Loops
Functions
Best Practices
100

Why is Python called a dynamically typed language?

Short answer: Python variables do not have associated types.

Explanation:

Dynamic typing means that the type of the variable is determined only during runtime. While the values have associated types, the variables themselves are untyped in Python.

100

What is short-circuit evaluation?

Python stops evaluating a boolean expression/operation if the truth value of expression has been determined already. The evaluation of expression takes place from left to right.

For example:

(A and B) is False, if A is False; it doesn’t matter what B is. 

(A or B) is True, if A is True; it doesn’t matter what B is.

Hence, the second part of the boolean expression (B) is not evaluated in either cases!

100

True or False:

The break statement is the only way a loop can be terminated.

False. We have learnt about 3 scenarios where loops get terminated:

1. break

2. After completing all iterations. Example: 

- when the condition is false in a while loop

- when the loop has gone through the entire range in a for loop

3. function return

100

What's the difference between print and return within a function?

Within a function, print is a function that is used to show values to the user (usually in the command line). The function will continue to execute the rest of its statements after encountering a print statement. 

return is a Python keyword that stops the execution of the function and sends a value to where the function was called. 

Would you like to see an example?

100

What's the most important thing you should do before starting to code a solution?

Think of the logic and write pseudo code/comments/the structure of your function!

200

True or False:

You can display a value in script mode without using a print statement.

(Think: What's script mode? What other modes are there?)

FALSE. You need print statements in your Python file to display values in script (batch) mode.

[Script mode is another term for batch mode.]

In batch mode, you run a file containing a complete Python program. There should be explicit print statements in the program to display output in the terminal/command line.

Example:

>>> python3 myProgram.py

<output printed as a result of running the whole program>

*****

In interactive mode, you can display a value without an explicit print statement.

Example: 

>>> python3

>>> print(1 * 2)

2

>>> 1 * 2

2

200

Correct or Incorrect:

The expression 0 and " abc " returns 0.

Correct.

The and operator returns the first operand if it is falsy; otherwise, it returns the second operand. 0 is considered falsy in Python - this is short circuit evaluation in action! If the first argument in an and expression is falsy, Python does not need to evaluate the second argument to know that the whole expression cannot be true.


On the other hand, the or operator returns the first truthy operand it encounters, or the last operand if all are falsy. Since 0 is considered falsy, the expression 0 or " abc " would evaluate further and returns " abc ", as it is the first (and in this case, the only) truthy operand following 0.

200

True or False:

range(10,0,-2) gives the sequence range(0,10,2) in reverse order.


False.

range(10,0,-2) --> [10,8,6,4,2]

range(0,10,2) --> [0,2,4,6,8]

200

Two Truths and a Lie:

1. You can define a function within another function.

2. All of these Python data types are immutable: str, tuple, bytes, float

3. This function call is legal in Python: functionName ( x2 = ’b ’, x1 = ’a ’, ’c ’ , ’d ’ )

The Lie: This function call is legal in Python: functionName ( x2 = ’b ’, x1 = ’a ’, ’c ’ , ’d ’ )

This function call is illegal. Can you tell me why?

[Refer to Slide Set 6, Slide 42]

Positional arguments must come first in order before keyword arguments in function calls.

Additionally, non-default parameters must come before default parameters in the function definition. This is to avoid ambiguity in function calls.

200

Identify the errors in the code (syntax, runtime, logic).

Refer to Question_BestPractices200.png.

Refer to  Solution_BestPractices200.png.

Is sum a reserved word in Python? No.

So technically you can use sum as a variable name, but it is not best practice to do so because it is a built-in function in Python. (Same goes for max, print, input, str and other built-in Python functions)

What will happen if you do? Python will reassign the function with the new value and you wouldn't be able to use the original sum function normally within that program.

300

Two Truths and a Lie:

1. None, global, input are Python keywords.

2. Calling a function from the math module before importing it will result in a runtime error. 

3. This does not result in an error:

a = 0

b = 8

a, b = b, a

(If you think this is the lie, please tell us what kind of error this would cause.)

The Lie:1. input is NOT a reserved word (keyword) in Python. input is a built-in Python function to read a line of input from the user and return it as a string.

Very Important: Make sure your Python code is indented consistently! This is the most common syntax error in Python if statements & loops.

300

Rewrite this nested if statement in two ways:

if (x < 10):

    if (x > 0):

        print(x)

1. 

if (x < 10 and x > 0):

    print(x)

2. 

if (0 < x < 10):

    print(x)

300

Refer to Question_Loops300.png.

Refer to Solution_Loops300.png.

300

Refer to Question_Functions300.png.

Refer to Solution_Functions300.png.

300

What kind of statement(s) will you use to:

1. Classify function inputs according to certain criteria?

2. Compute the factorial of a number?

1. If-Elif-Else and logical statements can be used to classify inputs according to certain "criteria" (conditions).

2. Loops

400

Which of these pairs of function calls will return the same values?

1. round(20.5)         round(19.5)

2. round(19.5)         int(20.5)

3. round(20.5, 0)     math.floor(20.5)

4. format(20.52, '2.1f')   round(20.52, 1)

All of them will return the same values EXCEPT 4. 

int does not round, it truncates - meaning it throws away the decimal point and anything that comes after it. 

math.floor(x) rounds down.

round(20.5) --> 20 because it rounds to even at the halfway decimal point between two integers.

For #3:

round(20.5, 0) --> 20.0 (float)

math.floor(20.5) --> 20 (int)

But round(20.5, 0) == math.floor(20.5) --> True!

For #4:

format(20.52, '2.1f') -> '20.5'

round(20.52, 1) -> 20.5

Both will yield 20.5, but in different types: format() returns a string, while round() returns a float.

400

What is information hiding?

What is one way this is implemented in Python?

What is another term for this form of information hiding?

1. Information hiding is a software design principle in Object-Oriented Programming, where certain parts of a program are hidden from/inaccessible to the user.

2. Using functions - details of the steps within a function are not visible to the user of the function when it is called

3. Functional Abstraction - hiding inner functionality from users

Example: print()

M
e
n
u