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