FIT9136 Catchup Cheat Sheet
FIT9136 Catchup Cheat Sheet
FIT9136 Catchup Cheat Sheet
.count(x)
4 >= 3 True
Counts the number of times x occurs within a string
>= Greater than or equal to
3 >= 4 False
.isnumeric() Returns True if ALL the characters in the string are numeric
Finds the first occurrence of the specified value and return an Variable Assignment
.find()
index A Python variable is a reserved memory location to store values. These values can
Splits the string at the specified separator (, or .) and returns a be integers, strings, floats, and lists among other things. We can then refer to that
.split() value by the variable name we gave it. We assign variables with “=”.
list
my_integer = 5 # We have assigned 5 to a variable my_integer
Returns a string where a specified value is replaced with a my_string = "Hello" # We have assigned a string "Hello" to my_string
.replace() my_list = [1, 2, 3, 4] # We have assigned a list to my_list
specified value my_float = 3.5 # We have assigned a float to my_float
Converting Variable Types Handy Tips!
It is possible to convert one data type into another (in some cases) and often you might find ● Make sure you know what data type you are working with, this
yourself doing it in order to perform some operation. There are two types of conversions in is often the source of much confusion.
Python: ● Make sure you choose the best data type for the job.
● If you are confused about a Boolean comparison try working it
Implicit Explicit out on paper first.
Users convert the data type of an object to the ● IndexErrors and ValueErrors are often trickier to find. Use print
required data type. We use the predefined functions statements when debugging to see where a program stops
Python automatically type converts one
like int(), float(), or str() to specify the desired type as working.
data type into another data type without
long as the data types are compatible (e.g., we cannot ● Get familiar with errors, you will encounter them often and
user involvement. One example of where
convert “hello” into an integer”. knowing how to read and interpret them is the best way
this happens is when we add an integer to
a float - the result is automatically a float. my_integer = 123 forward.
my_float = 1.23
my_integer = 123 my_string = "123" Logic Errors
my_float = 1.23 my_integer = str(my_integer) These occur when a program runs without crashing, but the output
result = my_integer + my_float my_string = float(my_string)
print(result) # 124.23 print(type(my_integer)) # <class 'str'> it produces is incorrect. This means that the logic implemented in
print(type(my_float)) # <class 'float'>
print(type(my_string)) # <class 'float'> incorrect. Python will not give you an error! It is important to
always double check your output and to check it often. Here are
Errors
Errors are the problem in a program which cause a program to stop executing. The error report some things that can help:
that Python gives us includes the type of error, the statement which caused the error and the line
● Understand the process you are implementing
where it occurred. Reading error reports is the first step to debugging. The advice is to run your
code often and make sure you fix them early. Below are some common errors you might ● Try and step through the code with print statements
encounter.
● Try it out on pen and paper
Error Description Advice
● Give the program a smaller range or smaller input to see
Raised when the index of a sequence is out of Double check the index of what
IndexError
range you are trying to access where it breaks down
Raised when a variable is not found in the Double check that this is the
NameError ● If the program is really long, test individual parts of it
local or global scope name of the variable
Raised by the parser when a syntax error is ● Try to explain it to someone else – rubber-ducking a
SyntaxError Double check your spelling
encountered
program is a tried and tested method
E.g., Taking the square root of a
Raised when a function gets an argument of negative number ● If all else fails, step away from the computer and the
ValueError
the correct type but improper value Double check the content of
the value input answer might just come to you.
Control Statements Lists
If and else statements are used to control the flow of a program. If a statement is true then Lists are data structures in Python which store items. They can store a variety of
we execute the body of the if statement. If the if statement is False then we consider the
data types and have notions of start, end and next. Items within a list can be
next statement which may be either an elif or an else. If neither the if or else statement is
directly accessed via their index (remember in Python indexing begins at 0) and
executed then we continue on to the rest of the program.
can be iterated over. We declare a list with the [ ] in Python and they can contain
Begin Control Flow any number of items which may or may not be of the same type. They can also
contain other lists.
False
List Indexing
elif mixed_list = ["Hello", 3, False] # A list with multiple data types
expression True Else if code
print(mixed_list[0]) # The first item - Hello
print(mixed_list[1]) # 3
print(mixed_list[2]) # False
False print(mixed_list[-1]) # The last item - False
print(mixed_list[-2]) # 3
print(mixed_list[-3]) # Hello
else Else code print(mixed_list[4]) # IndexError!
List Indexing
Program Continues
We can apply a number of useful methods to lists. We call them using
list.method(). These are just a small number of methods which can be used with
my_number = 5 # Let's assign a variable lists. Make sure you explore!
Function Purpose
if my_number == 2: # If my_number is equal to 2 then… len() Returns the number of items in a list
print("My number is 2") # …execute this statement
elif my_number == 3: # If my_number is equal to 3 then… .append() Adds an item to the end of the list
print("My number is 3") # …execute this statement
.pop(index) Removes an item at a specified index and returns it
else: # If my_number isn't 2 or 3 then…
print("My number isn't 2 or 3") # …execute this statement .insert(index, item) Inserts item at index
# The program will print "My number isn't 2 or 3" .sort() Sorts a list in ascending order
Loops While Loops
While loops execute statements while a condition is satisfied. They
A control flow statement which is used to repeatedly execute a group of statements as long as a
are used to repeat a task for an indefinite amount of time. They
specific condition is satisfied - also known as iteration. There are two types of loops - while and for check the condition after every loop and stop when the condition
loops. All for loops can be written as while loops although the reverse is not true (think asking is false. Remember to initialise the counter and your exit condition
to avoid infinite loops.
users for input).
# Printing 1 to 5
i = 1
While Loop Structure while i < 6:
print(i)
Step 1: Initialise a counter. i += 1
A Final Note…
End of Loop
Loops are the bread and butter of programming, and you are likely to use them often. They are
incredibly powerful tools for processing, and it is strongly encouraged that you practice them early
Good Luck! and get familiar with all their intricacies. Loops aid us in conducting repetitive tasks (which are
common) and are essential to saving time and minimising errors.