Python Fundamentals Flashcards
GENERAL
GENERAL
How does logical operators work?
There are two main logical operators ‘and’ & ‘or’ which operate
on boolean values. The ‘and’ operator needs both values to be
True to return a True result. The ‘or’ operator only needs
one value to be True to return a True result.
What is boolean logic?
Boolean logic is the name given to comparison operations whose
result is True or False.
==
!=
>
<
>=
<=
are all comparison operations that result in True or False
Is it possible to assign a maximum or minimum value to a variable in python.
Yes, python has the concept of positive and negative infinity which is a float.
What is a bitwise operation?
A bitwise operation is a logical operation performed on individual bits where 0 represents false and 1 represents true.
A bitwise ‘&’ operation tends to drop bits
A bitwise ‘|’ operation tends to add the bit together
What is binary? how does it work?
The binary number system is the base two representation of numbers using only two symbols 0 and 1.
When writing a number in binary, the prefix 0b is used to indicate that what follows is a binary number.
0b0101 is 5
What is floor division and what is the result of performing this operation?
floor division is just like regular division except the result is always an integer i.e. the decimal portion of the result is dropped NOT rounded.
What are the main types in python?
string, integer, float, boolean and None
How do we assign a multi-line string?
By using “”” (triple quotes)
Is there an increment and decrement operator in python?
Yes it is the ‘+= ‘ and ‘-=’.
‘++’ and ‘- -‘ do not exist in python like many C-style programming languages.
Is type hinting enforced by python?
The python interepter does not enforce types. Python is dynamically typed therefore type hinting only allows the user of the code to see the expected types.
Is python statically or dynamically typed?
What does this mean?
python is dynamically typed, which means you can assign any type to a variable, more IMPORTANT this also means that you can change the type of the variable after its assignment.
This in NOT recommended however.
DATE
<===>
DATE
<===>
syntax for creating a date object?
Use single integer inputs when creating the date object.
CONTROL FLOW
<===========>
CONTROL FLOW
<===========>
Short hand syntax for if-statement?
(do something) if (condition) else (do something else)
for-else….?
The else statement can be used with a loop as folows:
FUNCTONS
<===========>
FUNCTONS
<===========>
what is a function and what keyword is used to create one?
A function is a reusable block of code, that returns a result or performs an action. The ‘def’ keyword is used to declair a function.
Function parameters vs arguments
The function parameters are the inputs to the function at its definition, the arguments on the other hand are the inputs used when calling the function.
Can you call a function before it ‘s declaration?
You cannot call a function before you declare it. A common convention in many languages is to use a single main function which is then called at the end as the entry point for the program, this ensures all subsequent functions are called in the correct order.
What value is returned by a function without a return keyword or the return keyword without a value?
None
Can a Python function return multiple values?
Yes. A return statement followed by a list of comma seperated values will return each value. When the function is called an equal number of variables must be used in the order they were returned.
What are the three types of arguments we can pass to a function?
Positional arguments, Keyword arguments, and Default arguments
What are positional arguments?
A positional arguments is an arguments that can be called by their position in the function definition.
positional call to function:calculate_taxi_price(100, 10, 5)
What are Keyword arguments?
Arguments that can be called by their name.
keyword argument call to function:calculate_taxi_price(rate=0.5, discount=10, miles_to_travel=100)
What are default arguments?
Arguments that are given default values in the function definition.
def calculate_taxi_price(miles_to_travel, rate, discount = 10):
print(miles_to_travel * rate - discount )
When using a default argument, we can either choose to call the function without providing a value for a discount (and thus our function will use the default value assigned) or overwrite the default argument by providing our own.
How would you create a function that accepts unlimited arguments?
Note that to assing a value to the age argument you must use key-word assignment syntax (age=10) or position age as the first argument in the sequence.
Accept unlimited keyword arguments?
Note kwargs is just a convention and not a keyword.
Unlimited args and kwargs
args:tuple kwargs:dict
Positional args only?
Kwargs only?
Combination args?
LOOPS
<======>
LOOPS
<======>
What is a loop? how do you construct a for loop?
A loop allows us to perform an action multiple times without explicitly writing it each time.
Note: In the sample code 0-9 will be printed.
How does the range() function work?
The range function takes a single input and returns a range object. The range object can be converted into a list by using the list() function.
How does the range() function work in a for loop?
The range function when used with a for loop generally uses two parameters a start value and end value which is exclusive. An optional third argument can be used the ‘step’ which determines how much to add to ‘i’ in each iteration of the loop. using a negative value for the step input will cause the iteration to go backwards.
What is a While-loop?
What is the syntax?
A while loop performs a given set of instructions as long as a given condition is true.
while True: do something
Note: The condition must eventully evaluate to false to exit the loop.
How does the ‘break’ and ‘continue’ keywords affect the processing of a loop?
When the python interpreter hits the ‘break’ statement it will exit the loop.
In the case of the ‘continue’ keyword the interpreter will skip to the next iteration cycle.
LIST
<====>
LIST
<====>
What is a list and how do you create one?
A list is a simple data structure that can store items of any data type. Lists also know as an array stores and references items by index. The index start at zero and indicates the items position in the list:
How do you find the number of items in a list?
Pass the list as an argument to the ‘len()’ function
Can items in a list at a specific index be mutated?
Yes. simply assign the new value directly to the desired index. Note that this will replace any value currently at that index.
What does the ‘append()’ method do when used with a list?
The append method adds the given argument to the end of the list.
What does the ‘.pop()’ method do when used with a list?
The pop method removes the last item in the list and returns it. It can also take an optional argument which is the index of the element you wish to remove.
my_list.pop(2)
How does the .insert method work when used on a list?
The ‘.insert’ method is called on the list, it expects a minimum of two inputs the index for insertion and the element to be inserted.
When accessing a list
alternative to the ‘indexed for-loop’?
when should you use it?
If you do not need to update the item(s) in a list i.e. you only need to access the item and not its index you can use the ‘for-in’ syntax
Using slice syntax “return a slice of the list from index 1, up to but not including 5, skipping every 2nd value.
Note that slicing returns a new list.
How do we concatenate lists together?
Simply use the ‘+’ operator.
you can also use the += operator or call the extend() method on a list.
How to check for an item in a list without using a ‘if-statement’?
By using the ‘in’ keyword. This will return a Boolean value.
How do we delete specific items from a list?
Use the ‘del’ keyword followed by the name of the list square brackets containing the index or slices you wish to delete.
The ‘.pop()’ method with a index as an argument can also be used.
How to remove an item from a list by name not index?
Use the remove() method with the item as the argument.
How does the ‘.count()’ method work?
The ‘.count()’ method will count the number of times an element appears in a list.