Common Mistakes
Multiple Returns
Once you return from a function, you can't return again.
| Bad | Good |
|---|---|
|
|
Python is Case-sensitive
int is valid, Int is not. None is valid, none is not.
| Invalid (Error) | Valid |
|---|---|
|
|
Unless a Parameter Has a Default Value, It's Mandatory to Pass It
When you declare a function, you can specify a number of parameters it receives. Unless a parameter has a default value (we'll see how later in the course), you must pass an argument for it; otherwise, the code won't run. (There are some other types of parameters that are exceptions and we'll talk about them later in the course)
| Invalid (Error) | Valid |
|---|---|
|
|
Indentation is a Big Deal in Python
Proper indentation is a must for a Python program. Wrong indentation will result in either errors or unexpected behavior.
Indentation is how you specify if a block of code belongs to a structure (like a function).
| Invalid (Error) | Valid |
|---|---|
|
|
String Concatenation
You can use the + operator to concat two strings (see lecture notes), but both sides need to be strings for it to work.
| Invalid (Error) | Valid |
|---|---|
|
|
Floor Division
This operator // in Python is called floor division or integer division. Although the result value is always a whole integer, the result’s type is not necessarily int. If one or both sides are float, the type of the result is float.
Examples:
a = 6
b = 4
result = a / b # 1.5 (type of result is float)
result = a // b # 1 (whole integer. type is also int because both sides are int)
a = 6.0
b = 4
result = a / b # 1.5 (type of result is float)
result = a // b # 1.0 (whole integer, but type is float because a is float)
Data Types vs. Values
Value is what a variable holds; data type is the type of the value a variable holds. Don't confuse them.
Example:
a = 2
# Value of a is 2
# Data type of a is int
b = "hello"
# Value of b is 'hello'
# Data type of b is str
Void Functions vs. Value Returning
For functions that we declare ourselves, it's clear: if a function has a return statement, it's value-returning. But for built-in functions, such as print and input and float, we don't see their body of code (not unless you check the Python source code). So, you can't use the previous trick. For these functions, you need to know their purpose and how to use them first, and then decide if they're returning anything or not. The rule of thumb here is that if you can assign the result of a function call to a variable and it's not None, then it's value returning.
var_1 = print("hi")
print(var_1) # prints None
var_2 = float(2)
print(var_2) # prints 2.0
Python's Naming Conventions
When it comes to naming variables and functions, the Python convention is to use all lowercase letters, joined by underscore _. Except for constants where the convention is to use all-caps letters.
| Bad | Good |
|---|---|
|
|
Function Scope
Remember that Python has a scope for each variable. If a variable is defined inside a function (whether explicitly or as a parameter), that variable lives inside the function and dies (gets removed) when the function finishes (returns). When you send an argument (of the types we've seen so far) to a Python's function, a copy of it gets sent. So, no matter what you do with that argument inside the function, the original variable is not affected.
Example:
def make_number_half(number: int) -> int:
number = number // 2
print(number)
number = 10
make_number_half(number) # prints 5
print(number) # prints 10, because the original number is not affected by the function
See the code execution visualized here. Click on Next > to execute lines one by one.