Computer Science/coding Flashcards
Variables
Used to store data/information so that it can easily be repeated or reused throughout the code
In Python variable are used for things that are subject to change
Primitive data
The most basic forms of data:
Strings
Boolean (defined in Python as true or false (1 or 0))
Number
Operators
Different symbols that represent an operation
Enable us to process data and transform it
Arithmetic operators - make calculations
Comparison operators - determine the relationship between two values resulting in a Boolean
Logical operators - determine the logical state of multiple Boolean values or expressions resulting in another Boolean
Functions
Used in coding lots they are sequences of instructions that perform specific tasks
The inputs are called parameters
The actual values for each input are called arguments
Lists
Order items so that they’re in a specific sequence
The position of a value in a list is called its index
Adding things to the end of an existing list is called appending them to the end
Print statement
Used in Python to get the computer to ‘say’ something
e.g. print “Hello, world” makes the computer display that
Mismatched quotation marks will cause an error
Python 3 also requires brackets
Modulo operator
Indicated by %
It returns the remainder after a division is performed
Comments
Pieces of information used to describe the code
Use a # followed by the comment
Multi line strings
Use triple quotation marks
Converting between data types
Use str() for converting to a string
Use int() to convert to a numeric data type
float() to make it a decimal
len()
Determines the length of a string
“Ryan”.lower()
“Ryan”.upper()
Makes all the letters in the string lowercase
So this would give ryan
Makes all the letters in the string uppercase
str()
Turns non-strings into strings
E.g. 2 to “2”
% operator
Used to combine strings with variable
The % variable will replace the %s in the string
Boolean operators order
Not
And
Or
Lists
come in these brackets []
to choose an item from a list use its index - remember to count from 0 (positive indexing)
can also choose negative indexing, but this is rarely used only for long lists (the last item had an index -1, the next one -2 and so on)
list slicing - use a colon to access a few items in the list e.g. [1:9] will access index 1 to 8 of the list (the last index is discluded)
e.g.
will = [jalksjfl, fslakdjf, fjlaj]
they can have mixed data types
if you have a list within a list, rather than accessing and then saving as a variable you can chain indices to access something
Dictionary
similar to a list but has ‘keys’ instead of indices and the contents are in { }
they come in the form key : value
to retrieve a value from a list you use the same form as lists
dictionary values can be of any data type whilst dictionary keys can be of any except lists and dictionaries
What two rules must be adhered to when naming variables?
Variable names cannot start with a number
We must only use letters, numbers or underscores
concatenation
The process of linking two or more strings together, usually involving the str() function
open()
reader()
list()
used to open files
used to read files (must be imported from the csv module)
transforms files into lists
For loops
Consist of an iteration variable, an iterable variable and a body
The indented code in the body gets repeated as many times as there are elements in the iterable variable
each time the code in the loop is executed its called an iteration
Comparison operators
== An operator used to describe/determine if two values are equal
The result is either true or false (boolean)
!= An operator used to describe/ determine if two values aren’t equal
there are also > < >= and <=
type()
Used to determine the data type of whatever is found between the brackets
If statements
Conditional statements that usually involve some sort of comparison
They must be followed by a boolean value or an expression that evaluates to a boolean value
logical operators : and or
these are added to the if statements to add more conditions
How does python use multiple booleans?
It combines them to a single boolean
when using and : will only be true if all the individual booleans are true
when using or : will only be false if all the individual booleans are false
else
Only executed if the if statement preceding it is false
Compound statement
When an if statement is combined with an else clause
elif
used within an if statement
it is only executed if the preceding clauses (e.g. if or elif)
resolve to false and the elif condition resolves to true
improves efficiency as it means the computer doesn’t iterate over redundant code
adding values to an empty dictionary
takes the form dictionary_name[index] = value
Why can’t dictionaries and lists be keys?
hash() python uses this to try and convert each dictionary key to an integer even if its not that type
the hash command doesn’t transform lists and dictionaries to integers hence lists and dictionaries can’t be keys
in operator
used to determine if certain keys exist within a dictionary
or used in loops
min()
max()
used to determine the minimum and maximum values
How does the len() function work
Imagine we have a list - list = [1, 2, 3, 4, 5]
if we use a loop and a variable called length we can manually determine the length:
length = 0
for value in list:
length += 1
This would give us an output of 5 and that is effectively how the len() function works
How does the sum function work?
Imagine we have: list = [1, 2, 4, 5, 64]
sum_manual = 0
for value in list:
sum_manual += value
This is effectively what the sum function does
Built-in functions
These are functions which pre-exist and can be readily used with python e.g. len() sum()
But python also allows us to build our own functions
How we make functions?
e.g. to make a function to square numbers:
def square(a_number): squared_number = a_number * a_number return squared_number
to find the square of 6 we would then use square(a_number = 6) or we could use square(6)
Input variables like a_number are known as parameters, whereas 6 is known as the argument - the parameter a_number took in 6 as an argument
we start with a def statement to introduce defining the function
we specify the name of the variable that will serve as an input and then show what we want to do with that input
The definition of a function
The three elements that make up the definition are:
The def statement
The body
The return statement
Keyword arguments
When inputting the argument into the function the order doesn’t matter
e.g. subtract(a, b)
subtract(a=10, b=7) = 3 and subtract(b=7, a=10) = 3
Positional argument
The argument isn’t as clearly defined as keyword arguments so order does matter
e.g. def subtract(a, b):
return a - b
subtract(10, 7) isn’t the same as subtract(7, 10) as we are using positional arguments
Interfering with built in functions
Don’t name your functions the same as built in functions as this could confuse you and will alter the way the function operates
It leads to abnormal function behaviour
You should also avoid naming variables or lists after built in functions
Default arguments
Creating a function with some of the parameters being certain default values
e.g. def add_value(x, constant = 10):
return x + constant
What if we want to produce a function that can be used to produce either the sum of two numbers or the difference between two numbers?
We use multiple return statements with a conditional statement:
def sum_or_difference(a, b, do_sum = true): if do_sum: return a + b else: return a - b
Tuple
Another data type very similar to a list
It’s in this form: (9, 0) or this form 9, 0
Used for storing multiple values
Just as lists they support positive and negative indexing
Differences between tuples and lists - existing values can be modified in lists but they can’t in tuples
Functions with multiple outputs
We can have functions that return more than one variable
e.g. a function that returns the difference between two numbers and the sum of two numbers
This returns a tuple
If you put square brackets around the variables in the return statement then this returns a list