1/63
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Branching statements
Are code constructs that execute a block of code depending on a set of conditional criteria.
logical expressions
For branching statements, these criteria are written as ___________, and when satisfied, they execute a corresponding set of instructions.
Control flow
These refer to the order of execution of computer instructions
if statement
signifies an evaluation of its input logical expression. The code block will be executed only if the expression evaluates to a True value.
If-Else statement
A variant of the If statement. In this variant, a separate else code block is executed if the input logical expression of the if statement evaluates to False.
if-elif-else statement
This statement is another extension of the if statement. It can provide a second if condition that will be evaluated if the first if condition evaluates to false.
Three main ways to perform sequence of instructions over each item.
for loop
while loop
list comprehension
for loop
This loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
while loop
This loop can execute a set of statements as long as a condition is true.
Terminating an infinite loop
Manually by pressing the black square button in the menu, indicating ‘kernel interruption.’
List Comprehension
A construct short, and elegant way of creating a list based on existing lists.
Strings
Represents plan text. Should be enclosed in either ingle or double qoutations.
len()
Returns the length of an object.
Strings:Index
Indicates the location of the individual characters in a string.
Strings: Slicing
Accessing a part of the string through index enclosed in square brackets.
Strings: Concatenation
This is indicated by the symbol + performed on strings. Does not perform with a string and an integer.
str()
A built-in function that converts non-string to string.
help()
A built in function to have an idea of how a certain method works.
Lists
It is a sequence of values. The values are called its elements or items. It is defined by enclosing its items in square brackets, separated by commas, and can hold any type of data.
append()
A list method that adds an item at the end of the list.
insert()
A list method that inserts an item at a given index.
remove()
A list method that removes the first occurrence of a string.
list(“[string] ”)
Syntax for converting a string into a list.
Tuple
An immutable sequence type in Python that can hold a collection of items. Uses a pair of parentheses(), elements are separated by commas.
Operations like getting the length, indexing, slicing, using + and in are the same as those in strings and lists.
Homogeneous
A collection with same type. Lists are usually considered this type of collection.
Heterogeneous
Collection with different types. Tuples are usually considered this type of collection.
Dictionary
A general version of a list.
Are indexed by keys
Compromises of key-value pairs, and each key maps to a comma separated key:value pairs.
Declared by key:value pars enclosed in square brackets.
keys()
A dictionary method to get all the keys in a dictionary.
values()
A dictionary method to get all the values in a dictionary.
items()
A dictionary method to get all the key-value pairs in a dictionary.
in operator
An operator when used on dictionaries search occurrence from the keys.
list() on a dictionary
Access a list of dictionary keys.
Functions
A sequence of instructions that performs a task.
def()
used to make new function will be defined.
Components of a user-defined function
Function Header
Function Body
Descriptive String
Function Header
Starts with a keyword def
Followed by a function name
Followed by a pair of parenthesis with the input parameter inside and ends with a colon ( : )
Parameters
Are variables that the user need to supply to a function as input and can be manipulated in the body of the function.
Function Body
Where the step-by-step instructions to be executed upon calling the function was placed.
It is important to note that the instructions in the body function must be indented, and in the case of multiple instructions, all the instructions must be of the same indent.
At the end of function body, it can return some output.
Descriptive String
After defining the function header, an optional descriptive string can be placed in the middle of triple single or triple double qoutes.
This can be used to describe in more detail the task that will be performed by the function.
The descriptive string is also accessed by the command help().
Local Variables
Variables that are local to the function and is not part of the variables tracked across the whole Python notebook.