Python Flashcards

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
Q

how can you build larger boolean expressions?

A

using boolean operators

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

give 3 boolean operators

A

and
or
not

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

what does the boolean operator ‘or’ do?

A

‘or’ combines two expressions into a larger expression that is True if either component is True

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

only ___ component needs to be True for an or statement to be true

A

only 1 component needs to be True for an or statement to be true

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

what does the boolean operator ‘not’ do?

A

it reverses the value of any boolean value

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

where does the not operator go in a boolean statement?

A

at the start
eg
not 1 + 1 == 2 (False)
not 7 < 0 (True)

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

else statements always appear in conjunction with __ statements

A

else statements always appear in conjunction with if statements

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

what does an elif statement do?

A

an elif statement checks another condition after the previous if statement conditions aren’t met

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

what is a list in python

A

a data structure

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

what notation does a list begin with

A

square brackets [ ]

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

what is a method?

A

a python functionality that can be used to create, manipulate, and delete data

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

what is the syntax for using a method with a list?

A

list_name.method()

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

what is the location of an element in a list called?

A

its index

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

python lists are __-indexed

A

python lists are zero-indexed

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

what does zero-indexed mean?

A

that python lists start at 0, not 1

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

how do you select the last element of a list?

A

with the index -1

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

how can you change a specific value in a list?

A

by reassigning a value using its specific index

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

how can you change a specific value in a list?

A

by reassigning a value using its specific index

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

what method can you use to remove items from a list?

A

with the .remove( ) method

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

what is a list that contains another list called?

A

a two-dimensional (2D) list

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

what two inputs does the .insert( ) method take?

A

the index you want to insert to

the element you want to insert and the specified index

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

what does the .pop( ) method do?

A

removes elements at a specific index

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

how do you remove the last element from a list using the .pop( ) method?

A

don’t input an input into the brackets ( )

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

what does the len( ) function do?

A

gives the number of elements in a list

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

do range objects need to be converted to lists in order to determine their length ( with len() )

A

no

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

what is it called when only a portion of a list is extracted?

A

slicing

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

how do you slice the first n elements of a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

how do you slice the last n elements from a list

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

how do you slice all but the last n elements of a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

how do you sort a list of string in reverse alphabetical order

A

list_name.sort(reverse=True)

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

what would happen if you assign the result of the .sort( ) method to a variable?

A

it would return None - the sort method modifies the list directly and does not need to be assigned to a variable

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

how is the method sorted( ) different from the method .sort( )

A
  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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

what is a python tuple?

A

tuples are used to store multiple items within a single variable

57
Q

what is the difference between a python list and a python tuple?

A

a tuple is immutable

58
Q

how you create a one element tuple?

A

store the one element inside brackets, and put a ‘trailing’ comma after it
ie
one_element_tuple = (4,)

59
Q

what does the zip function do?

A

takes two or more lists as inputs and returns an object that contains the new list of pairs, threes, etc

60
Q

what does the zip function return when printed?

A

a zip object, which contains the location of the new variable in the computer’s memory, which isn’t always useful

61
Q

what do you do to convert the returned zip object of a zip function?

A

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
Q

what are the three stages of a loop

A
  1. initialisation
  2. repetition
  3. end
63
Q

what are the two types of iteration in programming languages?

A

indefinite iteration and definite iteration

64
Q

what is indefinite iteration

A

an iteration where the number of times the loop is executed depends on how many times a condition is met

65
Q

what type of loop is a for loop?

A

a definite iteration

66
Q

does the temporary variable in a for loop need to be defined beforehand?

A

no

67
Q

what type of loop is a while loop?

A

an indefinite iteration

68
Q

what does a while loop do?

A

a while loop performs a set of instructions as long as a given condition is true

69
Q

how do you stop iteration inside a loop?

A

with the break statement at the end of the loop commands

70
Q

how do you define a function?

A
with def
def function_name():
71
Q

what is the beginning of a function also known as?

A

the function header

72
Q

what is it called when a function is executed?

A

calling a function

73
Q

how do you call a function in Python?

A

typing out the function name followed by parentheses

function_name()

74
Q

what tells a computer what is part of a function and what isn’t part of a function?

A

how much whitespace there is / indentation

75
Q

what is execution flow?

A

the order in which code is executed from top to bottom

76
Q

what do function parameters do?

A

allow the function to accept data as an input value

77
Q

how do you write a function that takes more than one parameter

A

separate the parameters with commas

78
Q

what are the 3 types of arguments in python?

A

positional arguments, keyword arguments, default arguments

79
Q

what is a positional argument

A

arguments that can be called by their position in the function definition

80
Q

what is a keyword argument

A

arguments that can be called by their name

81
Q

what is a default argument?

A

arguments that are given default values

82
Q

how do you provide a default value to an argument?

A

by using the assignment operator = in the function declaration

83
Q

how can you return several values from a function?

A

by separating the values with commas

84
Q

what can a string be thought of as?

A

a list of characters

85
Q

what does len do to strings?

A

returns the number of characters in a string

86
Q

how can you iterate through strings?

A

with for and while loops

87
Q

what is the syntax for string methods?

A

string_name.string_method(arguments)

88
Q

what are the 3 string methods that can change the casing of a string?

A

.lower()
.upper()
.title()

89
Q

do string methods alter the original string?

A

no

90
Q

what does the .split() method do to a string?

A

takes an argument and returns a list of substrings found between the given argument (known as the delimiter)

91
Q

what is the default argument for the .split() method?

A

to split at spaces

92
Q

what happens when you set the argument of .split() to the last character of a string?

A

it returns an empty string at the end of the returned list

93
Q

what is a python module?

A

a collection of python declarations intended to be used as a tool

93
Q

what is a python module?

A

a collection of python declarations intended to be used as a tool

94
Q

what are modules also called?

A

libraries or packages

95
Q

what is a package actually?

A

a directory that holds a collection of modules

96
Q

what does a namespace do?

A

isolates the functions, classes, and variables defined in the module from the code in the file doing the importing

97
Q

how do you alias a module?

A

with the as keyword

98
Q

what is pollution?

A

pollution occurs when the same name could apply to two or more things

99
Q

what is a dictionary?

A

a set of key: value pairs

100
Q

what character do dictionaries begin and end with?

A

curly brackets { }

101
Q

how are key and value pairs separated in a dictionary?

A

by a comma

102
Q

what type can dictionary keys be?

A

any type

103
Q

can you use lists or dictionaries as values in dictionaries?

A

no

104
Q

what syntax do you use to add an item to a dictionary?

A

dictionary_name[key] = value

105
Q

how do you add multiple key:value pairs to a dictionary?

A

with the .update() method

106
Q

what is the syntax for accessing values by a key?

A

dictionary_name[“key”]

107
Q

what is the default argument of .open()?

A

r - opens files in read mode

108
Q

what is the argument to open a file in append-mode

A

‘a’

109
Q

what does the with keyword invoke when opening files?

A

it invokes context manager for the file you’re calling open on

110
Q

What does JSON stand for?

A

JavaScript Object Notation

111
Q

what arguments does the json.dump() method take?

A

data object, file object you want to save

112
Q

what is a class?

A

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
Q

what is the recommended style for classes?

A

Capitalised name
eg:
Class

114
Q

what does instantiated mean?

A

an instance must be created in order for it to exist

115
Q

how do you instantiate a class?

A

by calling it like a function, set to a variable with brackets

variable = Class()

116
Q

what is a class instance also known as?

A

an object

117
Q

The pattern of defining classes and creating objects to represent the responsibilities of a program is known as:

A

Object Oriented Programming / OOP

118
Q

instantiation takes a class and turns it into an ___

A

object

119
Q

what is a class variable?

A

a variable that’s the same for every instance of a class?

120
Q

when would you use a class variable?

A

when you want the same data to be available to every instance of a class

121
Q

what is always the first argument in a method (class)

A

the object that is calling the method

122
Q

what is the convention for naming the first argument of a method?

A

call it self

123
Q

how many arguments do methods always have?

A

at least one

124
Q

what is the difference between functions and class methods?

A

class methods are indented within the class

125
Q

when you call a method it automatically passes the ___ calling the method as the ___ ____

A

object , first argument

126
Q

what are python methods within a class that have special behaviour called?

A

magic methods or dunder methods

127
Q

methods that are used to prepare an object being instantiated are called ___

A

constructors

128
Q

an object is an ___ of a class

A

instance

129
Q

a class is a ____ for a data type

A

schematic

130
Q

The data held by an object is referred to as an ___ ____

A

instance variable

131
Q

are instance variables shared between all instances of a class?

A

no

132
Q

what are instance variables?

A

variables that are specific to the object they are attached to

133
Q

instance variables and class variables are both considered ___ of an object

A

attributes

134
Q

what happens if you try to access an attribute that is neither a class variable not an instance variable?

A

AttributeError

135
Q

what can you use to find out if an object has an attribute or not?

A

the hasattr() function

136
Q

do dictionaries and integers have count attributes?

A

no

137
Q

do strings and lists have count attributes?

A

yes

138
Q

what is the difference between a list and a set

A

items can only appear one in a set

139
Q

is a set ordered or unordered?

A

unordered

140
Q

what does unordered mean

A

items in a set etc do not have indexes for their location