Python Flashcards
how do you write a comment in Python?
with the hash symbol #
how are variables assigned in python?
with the = sign
can variable names begin with numbers?
no
what does the error syntax error mean?
syntax error means that there is something wrong with the way the program is written
what does the Name Error error mean?
when the Python interpreter sees a word it doesn’t recognise
what does Python do before performing division?
converts all integers into floating point numbers
what is exponentiation?
the process of raising a quantity to some assigned power (using superscript numbers - x to the power of y)
what notation is used for exponentiation?
**
what is string concatenation?
the process of adding strings together
what is the plus equals += operator for?
to add to the current value of a variable
what notation do you use for a multi-line string?
three quotation marks, either “”” or ‘’’
what function is used to allow users to assign a value to a variable?
the input ( ) variable
a boolean expression is a ____ that can either be ___ or ___
a boolean expression is a statement that can either be true or false
what operators do you use to create a boolean expression?
relational operators
relational operators ___ two items and return either ___ or ___ false
relational operators compare two items and return either True or False
what are relational operators also known as?
comparators
what is the notation for the not equals relational operator / comparator?
!=
what is the notation for boolean equals operator?
==
what type are True and False known as?
True and False are their own type of bool
are True and False the only bool types?
yes
what is a variable assigned a bool as a value called?
a boolean variable
give two ways to create a boolean variable
a) assign a bool True or False to a variable
b) set a variable equal to a boolean expression
boolean variables are the building blocks of ____ _____
boolean variables are the building blocks of conditional statements
what notation takes the place of ‘then’ in a conditional statement in english (if x then y)
then is written with a colon :
how can you build larger boolean expressions?
using boolean operators
give 3 boolean operators
and
or
not
what does the boolean operator ‘or’ do?
‘or’ combines two expressions into a larger expression that is True if either component is True
only ___ component needs to be True for an or statement to be true
only 1 component needs to be True for an or statement to be true
what does the boolean operator ‘not’ do?
it reverses the value of any boolean value
where does the not operator go in a boolean statement?
at the start
eg
not 1 + 1 == 2 (False)
not 7 < 0 (True)
else statements always appear in conjunction with __ statements
else statements always appear in conjunction with if statements
what does an elif statement do?
an elif statement checks another condition after the previous if statement conditions aren’t met
what is a list in python
a data structure
what notation does a list begin with
square brackets [ ]
what is a method?
a python functionality that can be used to create, manipulate, and delete data
what is the syntax for using a method with a list?
list_name.method()
what is the location of an element in a list called?
its index
python lists are __-indexed
python lists are zero-indexed
what does zero-indexed mean?
that python lists start at 0, not 1
how do you select the last element of a list?
with the index -1
how can you change a specific value in a list?
by reassigning a value using its specific index
how can you change a specific value in a list?
by reassigning a value using its specific index
what method can you use to remove items from a list?
with the .remove( ) method
what is a list that contains another list called?
a two-dimensional (2D) list
what two inputs does the .insert( ) method take?
the index you want to insert to
the element you want to insert and the specified index
what does the .pop( ) method do?
removes elements at a specific index
how do you remove the last element from a list using the .pop( ) method?
don’t input an input into the brackets ( )
what does the len( ) function do?
gives the number of elements in a list
do range objects need to be converted to lists in order to determine their length ( with len() )
no
what is it called when only a portion of a list is extracted?
slicing
how do you slice the first n elements of a list?
with the notation:
list_name[:n] - colon used to represent ‘the first of’ n
list_name[:3] would select list items with index 0 - 2 from list called list_name
how do you slice the last n elements from a list
with the notation:
list_name[-n:] - colon used to represent ‘the last of’ n, placed at end
list_name[-2:] would select the last two list items from the end of the list
how do you slice all but the last n elements of a list?
with the notation:
list_name[:-n] - colon used to represent ‘the last of’ n, placed at end
list_name[:-2] would slice all but the last two elements of a list
how do you sort a list of string in reverse alphabetical order
list_name.sort(reverse=True)
what would happen if you assign the result of the .sort( ) method to a variable?
it would return None - the sort method modifies the list directly and does not need to be assigned to a variable
how is the method sorted( ) different from the method .sort( )
- sorted() comes before a list (ie sorted(list_name) as opposed to list_name.sort)
- sorted( ) generates a new list instead of modifying the existing list