Python Flashcards

(142 cards)

1
Q

how do you write a comment in Python?

A

with the hash symbol #

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how are variables assigned in python?

A

with the = sign

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

can variable names begin with numbers?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does the error syntax error mean?

A

syntax error means that there is something wrong with the way the program is written

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does the Name Error error mean?

A

when the Python interpreter sees a word it doesn’t recognise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does Python do before performing division?

A

converts all integers into floating point numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is exponentiation?

A

the process of raising a quantity to some assigned power (using superscript numbers - x to the power of y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what notation is used for exponentiation?

A

**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is string concatenation?

A

the process of adding strings together

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the plus equals += operator for?

A

to add to the current value of a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what notation do you use for a multi-line string?

A

three quotation marks, either “”” or ‘’’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what function is used to allow users to assign a value to a variable?

A

the input ( ) variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

a boolean expression is a ____ that can either be ___ or ___

A

a boolean expression is a statement that can either be true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what operators do you use to create a boolean expression?

A

relational operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

relational operators ___ two items and return either ___ or ___ false

A

relational operators compare two items and return either True or False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what are relational operators also known as?

A

comparators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what is the notation for the not equals relational operator / comparator?

A

!=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what is the notation for boolean equals operator?

A

==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what type are True and False known as?

A

True and False are their own type of bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

are True and False the only bool types?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is a variable assigned a bool as a value called?

A

a boolean variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

give two ways to create a boolean variable

A

a) assign a bool True or False to a variable

b) set a variable equal to a boolean expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

boolean variables are the building blocks of ____ _____

A

boolean variables are the building blocks of conditional statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what notation takes the place of ‘then’ in a conditional statement in english (if x then y)

A

then is written with a colon :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
how can you build larger boolean expressions?
using boolean operators
26
give 3 boolean operators
and or not
27
what does the boolean operator 'or' do?
'or' combines two expressions into a larger expression that is True if either component is True
28
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
29
what does the boolean operator 'not' do?
it reverses the value of any boolean value
30
where does the not operator go in a boolean statement?
at the start eg not 1 + 1 == 2 (False) not 7 < 0 (True)
31
else statements always appear in conjunction with __ statements
else statements always appear in conjunction with if statements
32
what does an elif statement do?
an elif statement checks another condition after the previous if statement conditions aren't met
33
what is a list in python
a data structure
34
what notation does a list begin with
square brackets [ ]
35
what is a method?
a python functionality that can be used to create, manipulate, and delete data
36
what is the syntax for using a method with a list?
list_name.method()
37
what is the location of an element in a list called?
its index
38
python lists are __-indexed
python lists are zero-indexed
39
what does zero-indexed mean?
that python lists start at 0, not 1
40
how do you select the last element of a list?
with the index -1
41
how can you change a specific value in a list?
by reassigning a value using its specific index
41
how can you change a specific value in a list?
by reassigning a value using its specific index
42
what method can you use to remove items from a list?
with the .remove( ) method
43
what is a list that contains another list called?
a two-dimensional (2D) list
44
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
45
what does the .pop( ) method do?
removes elements at a specific index
46
how do you remove the last element from a list using the .pop( ) method?
don't input an input into the brackets ( )
47
what does the len( ) function do?
gives the number of elements in a list
48
do range objects need to be converted to lists in order to determine their length ( with len() )
no
49
what is it called when only a portion of a list is extracted?
slicing
50
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
51
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
52
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
53
how do you sort a list of string in reverse alphabetical order
list_name.sort(reverse=True)
54
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
55
how is the method sorted( ) different from the method .sort( )
1. sorted() comes before a list (ie sorted(list_name) as opposed to list_name.sort) 2. sorted( ) generates a new list instead of modifying the existing list
56
what is a python tuple?
tuples are used to store multiple items within a single variable
57
what is the difference between a python list and a python tuple?
a tuple is immutable
58
how you create a one element tuple?
store the one element inside brackets, and put a 'trailing' comma after it ie one_element_tuple = (4,)
59
what does the zip function do?
takes two or more lists as inputs and returns an object that contains the new list of pairs, threes, etc
60
what does the zip function return when printed?
a zip object, which contains the location of the new variable in the computer's memory, which isn't always useful
61
what do you do to convert the returned zip object of a zip function?
use the list() function with the name of the zip variable as the input for the list function ie: converted_list = list(names_and_heights)
62
what are the three stages of a loop
1. initialisation 2. repetition 3. end
63
what are the two types of iteration in programming languages?
indefinite iteration and definite iteration
64
what is indefinite iteration
an iteration where the number of times the loop is executed depends on how many times a condition is met
65
what type of loop is a for loop?
a definite iteration
66
does the temporary variable in a for loop need to be defined beforehand?
no
67
what type of loop is a while loop?
an indefinite iteration
68
what does a while loop do?
a while loop performs a set of instructions as long as a given condition is true
69
how do you stop iteration inside a loop?
with the break statement at the end of the loop commands
70
how do you define a function?
``` with def def function_name(): ```
71
what is the beginning of a function also known as?
the function header
72
what is it called when a function is executed?
calling a function
73
how do you call a function in Python?
typing out the function name followed by parentheses function_name()
74
what tells a computer what is part of a function and what isn't part of a function?
how much whitespace there is / indentation
75
what is execution flow?
the order in which code is executed from top to bottom
76
what do function parameters do?
allow the function to accept data as an input value
77
how do you write a function that takes more than one parameter
separate the parameters with commas
78
what are the 3 types of arguments in python?
positional arguments, keyword arguments, default arguments
79
what is a positional argument
arguments that can be called by their position in the function definition
80
what is a keyword argument
arguments that can be called by their name
81
what is a default argument?
arguments that are given default values
82
how do you provide a default value to an argument?
by using the assignment operator = in the function declaration
83
how can you return several values from a function?
by separating the values with commas
84
what can a string be thought of as?
a list of characters
85
what does len do to strings?
returns the number of characters in a string
86
how can you iterate through strings?
with for and while loops
87
what is the syntax for string methods?
string_name.string_method(arguments)
88
what are the 3 string methods that can change the casing of a string?
.lower() .upper() .title()
89
do string methods alter the original string?
no
90
what does the .split() method do to a string?
takes an argument and returns a list of substrings found between the given argument (known as the delimiter)
91
what is the default argument for the .split() method?
to split at spaces
92
what happens when you set the argument of .split() to the last character of a string?
it returns an empty string at the end of the returned list
93
what is a python module?
a collection of python declarations intended to be used as a tool
93
what is a python module?
a collection of python declarations intended to be used as a tool
94
what are modules also called?
libraries or packages
95
what is a package actually?
a directory that holds a collection of modules
96
what does a namespace do?
isolates the functions, classes, and variables defined in the module from the code in the file doing the importing
97
how do you alias a module?
with the as keyword
98
what is pollution?
pollution occurs when the same name could apply to two or more things
99
what is a dictionary?
a set of key: value pairs
100
what character do dictionaries begin and end with?
curly brackets { }
101
how are key and value pairs separated in a dictionary?
by a comma
102
what type can dictionary keys be?
any type
103
can you use lists or dictionaries as values in dictionaries?
no
104
what syntax do you use to add an item to a dictionary?
dictionary_name[key] = value
105
how do you add multiple key:value pairs to a dictionary?
with the .update() method
106
what is the syntax for accessing values by a key?
dictionary_name["key"]
107
what is the default argument of .open()?
r - opens files in read mode
108
what is the argument to open a file in append-mode
'a'
109
what does the with keyword invoke when opening files?
it invokes context manager for the file you're calling open on
110
What does JSON stand for?
JavaScript Object Notation
111
what arguments does the json.dump() method take?
data object, file object you want to save
112
what is a class?
a template for a data type - describes the kind of information that class will hold and how a programmer will interact with that data
113
what is the recommended style for classes?
Capitalised name eg: Class
114
what does instantiated mean?
an instance must be created in order for it to exist
115
how do you instantiate a class?
by calling it like a function, set to a variable with brackets variable = Class()
116
what is a class instance also known as?
an object
117
The pattern of defining classes and creating objects to represent the responsibilities of a program is known as:
Object Oriented Programming / OOP
118
instantiation takes a class and turns it into an ___
object
119
what is a class variable?
a variable that's the same for every instance of a class?
120
when would you use a class variable?
when you want the same data to be available to every instance of a class
121
what is always the first argument in a method (class)
the object that is calling the method
122
what is the convention for naming the first argument of a method?
call it self
123
how many arguments do methods always have?
at least one
124
what is the difference between functions and class methods?
class methods are indented within the class
125
when you call a method it automatically passes the ___ calling the method as the ___ ____
object , first argument
126
what are python methods within a class that have special behaviour called?
magic methods or dunder methods
127
methods that are used to prepare an object being instantiated are called ___
constructors
128
an object is an ___ of a class
instance
129
a class is a ____ for a data type
schematic
130
The data held by an object is referred to as an ___ ____
instance variable
131
are instance variables shared between all instances of a class?
no
132
what are instance variables?
variables that are specific to the object they are attached to
133
instance variables and class variables are both considered ___ of an object
attributes
134
what happens if you try to access an attribute that is neither a class variable not an instance variable?
AttributeError
135
what can you use to find out if an object has an attribute or not?
the hasattr() function
136
do dictionaries and integers have count attributes?
no
137
do strings and lists have count attributes?
yes
138
what is the difference between a list and a set
items can only appear one in a set
139
is a set ordered or unordered?
unordered
140
what does unordered mean
items in a set etc do not have indexes for their location