1/137
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Python lacks built-in syntax for constants, so how do you signal that a variable is unchangeable?
The convention in Python is to fully capitalise the name of the variable and use underscores for spaces to signify that the variable is to be treated as a constant (can’t be changed). This may also solely signify a Global Variable.


Create an instance called song of the class where name = "Happy Birthday" and artist = "unknown"
song = Song ( "Happy Birthday", "unknown")

How can we assign class variables a value when the class instance is being constructed?
By adding them as a parameter in the init function and setting the variable equal to the parameter value.

What is wrong with this code?
The class keyword is missing.
Following conventional naming syntax, what should a class name look like?
Class names usually have the first letter capitalized and the rest lowercase, like with Person here.

When defining the parameters of a function in its parenthesis, what does a -> signify after the closing parenthesis?
This is the start of a type hint, which tell you and other programmers what data types to expect. A -> and the data type following it tells you the type of data this function returns.

What are hash tables known as in Python?
Dictionaries or dict

Write a print statement that displays the first item of the class variable countries.
print(gymnastics.countries[0])
![<p>print(gymnastics.countries[0])</p>](https://knowt-user-attachments.s3.amazonaws.com/85eaf15f-b7a2-41b6-aeaf-6540297af911.jpg)
What are classes used for?
To reduce repeated code by grouping it into a class.
When do we use the keyword self ?
When we need to access class variables or methods inside the class definition.
How do we define the constructor method?
def __init__(self):
What is the purpose of a constructor?
To construct an instance of a class object with unique class variables.

How do we call the display_color function defined inside the Flower class?
We can use an instance of the class, a ., and then the name of the function. For example, rose_flower = Flower() & rose_flower.display_color()


What does the code self.color = color do in this example?
You’re storing the value of the parameter color inside the object as self.color, so it can be accessed later. We can then pass in a value to a specific class instance (rocky) using that parameter (with rocky = Virtual_Pet(“red”)).
What’s the difference between a variable defined in a class and a variable defined in a class by using a constructor method (__init__)?
A variable defined normally in the class body is a class variable, which is shared by all objects made from that class (the value of that class variable is also shared for the entire class, unless overridden).
A variable defined inside the constructor (__init__) is an instance variable, meaning each object gets its own separate copy that can be different for each instance.
What does the constructor method look like when creating a method for a class, and what does it do?
The constructor method looks like __init__() and allows us to set unique values for the class variables when we create an instance.


Access and call the method introduce using the class instance pikachu .
pikachu.introduce()

Do we still pass in self as the first parameter of a class method when it uses a constructor (__init__)?
Yes, you still include self as the first parameter.


How do we access rocky 's method print_color?
rocky.print_color()
What is the name of the first parameter we pass into a method? and what is its purpose?
self is the first parameter of any method. We use self as a parameter in class methods so that they can access the class variables (by making it within their scope).
What is a method in Python?
In Python, methods are functions that are defined as part of a class.
It is common practice that the first argument of any method that is part of a class is the actual object calling the method. This argument is usually called self.

Why do we need to store the class instance in a variable?
So we can access the class instance.
What syntax do you use to create a class object instance?
You need the class name followed by ().
What are variables inside a class used for?
To store data that is related to the given class.
What is a class?
A template that can have variables and functionality associated with it. Can be used to create class objects.

What does this code do?
We're defining a class (VirtualPet) and creating an instance (an object) of it (fluffy)
What is a class instance?
Instantiation is when you create an object (instance) from a class. When you store that instance (class object) inside a variable, it’s called an instance variable. It’s stored inside the object itself (usually created in __init__.

What is a class definition?
A class definition is the code that creates a new class. It describes what attributes and methods the class will have.

How do we access a class variable?
To access a class variable, we add the instance name (variable containing class definition), a . , and the name of the variable we want. Like skippy.wagging_tail here.


What are we doing here?
We are creating variables from the VirtualPet class template (these are called instances). So fluffy and benny are instances of the VirtualPet class (definition).
Instead of creating new variables for each of the different configurations for the same object (which will take a lot of time and could lead to mistakes), what could we use instead?
We can create classes instead to help us group data and functionality. A class is just a template that we use to create many similar but distinct things.


What happens when we slice a list with a step value but without providing a start position or stop position?
We can use the step value with no start or end value when slicing. By default, this will work from the start to the end of the full original list.
True or False, when using a negative step value when slicing, start needs to be either omitted or greater than stop to return elements?
True

What will this code return?
['B', 'D', 'F'] , we can use a different slicing format with two colons with [start:stop:step] , where the step determines how Python steps between start and end.
How can we start creating a template called Person via a class?
We add the keyword class followed by a name for class template (Person) and a colon.


What happens if the step value in a list slice is negative?
The step value can be negative, which allows us to use a start position larger than the end value. The order of the elements is reversed.


What temperatures will be printed?
All of them.
What does the del keyword do?
Allows us to delete objects, or items within a data structure.
What happens when we use the loop variable as the expression in a list comprehension?
We copy the original list.
![<p>Complete the list slicing in the code so that [3, 2, 1] is printed at the end.</p>](https://knowt-user-attachments.s3.amazonaws.com/b60f8443-3dab-45de-aa4b-f5b61e794ae2.jpg)
Complete the list slicing in the code so that [3, 2, 1] is printed at the end.
[::2]
![<p>[::2]</p>](https://knowt-user-attachments.s3.amazonaws.com/e0524bff-0a3b-4584-9c73-d3b51a59661a.jpg)
![<p>Complete the start:stop:step notation so that [5, 5, 5] is printed.</p>](https://knowt-user-attachments.s3.amazonaws.com/b3c823ba-0fe2-43a0-b691-72e821b4f4e0.jpg)
Complete the start:stop:step notation so that [5, 5, 5] is printed.
[-1:2:-2]

Should the following code be grouped into a class?
Yes, a class could group a Person with the variable's 'name' and 'age'.
Why would we use an if statement in a list comprehension?
We add in to the end of the list comprehension to filter the elements in the new list that meet a certain condition.


Why can’t we use this get_full_name() function inside a list comprehension?
Because it doesn’t have a return value.

What is the difference between the halved_lc variable and the halved_loop variable?
Both list variables contain the exact same values. However, halved_lc uses list comprehension to use a more compact version of the code in halved_loop
How do we conventionally indicate that something is a class?
Its name will start with a capital letter.
To eliminate duplicates from a list, how we can transform that list into a set?
We code set(list_name)

What do we use to surround the key-value pairs stored in a dictionary?
Curly braces {}

What is the format of slicing notation?
[start:stop], where the value to the left of the colon is the start position for the slice; meanwhile, the value to the right of the colon is the stop position for the slice. Note that the element in the stop position is not included.
What do we use to retrieve multiple values from a list?
Slicing (slice notation)


Which of these functions can we use inside a list comprehension?
The divide(number, divisor) function as it has a return value.

What will this code print?
[] an empty list
What is list comprehension?
List comprehension is a way to create a new list by applying an expression on each element in an existing list. Usually uses an expression to apply to each element paired with a for loop.


Which of these two variables stores a set?
team_2 because the values are surrounded by curly braces {}

What does this display in the console?
An error because “pumila” isn’t present in the set.

Since sets don't have indices (as they’re unordered), how can we check if a certain element exists in the set?
Via the in keyword.

What happens when we run this code?
We receive an error as tuples are immutable and therefore can’t be modified. Although, values in a tuple can be accessed/retrieved via indexing or negative indexing.

What happens when we specify a range outside the length of a list as the starting index of a slice?
It will return an empty list, rather than an error.

Why is it often conventional to have a trailing comma , after the last element in a Python list?
Because it makes it easier to copy-paste and reorder list items in code. Even though it’s not logically necessary.
What is a web framework?
You can think of a web framework as sort of like a library that’s written in Python, that specialises in doing some things in Python for you that you would otherwise have to do yourself.
In Python, why would we use negative indexing on a object?
Negative indexing allows us to retrieve values from the end of an indexable object, such as a list.


What has REGISTRANTS been initialised as?
REGISTRANTS is a global variable that has been initialised as an empty dictionary / dict object.
What function do you use to add a new value to a set (unordered)?
You use .add()

What does the difference() instruction do when used with two sets?
It gives us a set of elements present in the left set but missing from the right one.

What's wrong with this code?
Sets don't have indices, so we can't access elements by indices like with reactions [0]
When we want to make sure that a collection of values can't have any duplicates, what do we store it in?
A set
What happens if we only include a start position when slicing a list?
It will retrieve all the values from that starting list position to the end of the list.

How do we remove an element from a set?
Use the .remove() function and specify which value inside the parentheses.
How do we check if a dictionary contains a certain key?
With the in keyword
What are the parts that make up a loop comprehension that filters a list?
First, an expression, then a for loop, and finally, a conditional statement.
How do you know if a set is a subset?
When all the elements in a set are present in another larger set, we call that smaller set a subset.

What function can you use to check if a set is a subset of another set?
You can use the .issubset() function. Where you code potential_subset.issubset(set)

How many values can a key be associated with?
Only one value can be associated with a key within a dictionary.
How do you identify a Decorator in Python and what does it do?
An @ symbol indicates a decorator. Decorators are a way to modify or enhance functions (or classes) without changing their actual code.


What does this code print?
['Egypt', 'Brazil'] , If no start or end value are given, Python will automatically work from the end of a list if a negative step is provided.
What do we use to loop through all the keys in a dictionary?
A for loop
What do we use pop() for when it comes to dictionaries?
To remove a specific key and its associated value.

What does the difference set contain?
{‘giraffe’, ‘whale’}
What’s the difference between the .union() function and the .intersection() function?
While union() gives us all elements of the two sets, intersection() gives us only the common ones.

What does this code do/say?
It defines a function called registrants that says, render the template registrants.html, and make a variable inside that template named registrants, whose value is whatever REGISTRANTS currently holds.

What does this list comprehension do?
It loops through item_prices and copies each value that is less than 50

What does the code inside the loop do?
It's displaying the tuple value associated with the key stored in the winner variable.

What framework is the render_template() function from?
The Flask framework

What happens if we run this code?
We get an error because tuples are immutable.
To get a set of elements that are present in one set, but not in another set, what function can we use?
You can use .difference() function. Where we code set1.difference(set2)

What does m/1000 represent in this list comprehension?
The expression that is applied on each element accessed in the for loop.
What data types can a dictionary's keys be?
Strings, numbers, booleans, or tuples.
What commonly used templating engine is used for frameworks such as Flask, Bottle, Morepath, and Django (optionally)?
The Jinja2 engine
What is a dictionary called in Python?
A Dict Object
What does Flask’s session function do?
In Flask, the session function is used to store data specific to each user that you want to be available across different requests from the that user (on a per browser/user basis). It’s stores user-specific data in their browser as cookies with their own session data. This is useful for keeping track of user information such as login credentials (not passwords) and login status, without sharing it with other users.
What is the default method for a route?
The GET request method.
What is the requirements.txt file used for in a Python project?
The requirements.txt file is used to declare the dependencies for a project. It does this by listing the packages (and their versions) that your program needs to run. The pip package manager is generally used to until these packages.
What is pip and what is it used for?
Pip is a package manager for Python. It allows you to install and manage additional libraries and dependencies (packages) that are not included in the standard Python library. For example, if you have a requirements.txt file, you can use pip to install all the packages listed in that file.
What are Django, Pyramid, and Flask examples of?
Web frameworks
What type of brackets do you use to declare items in a Python list?
Square brackets []
![<p>Square brackets []</p>](https://knowt-user-attachments.s3.amazonaws.com/fb2a6d26-f623-4421-b12c-ac2e79e59e20.jpg)
Why is it good practice to check if a key is part of the dictionary before removing it?
To avoid getting an error in the case that the key isn’t present.
What happens when we use add() to add a value that a set already contains?
The value won’t be added, since duplicates aren’t allowed in sets.
What should we return when we're just interested in multiple individual values
A tuple with multiple values.
What instruction do we use to remove a key-value pair from a dictionary?
.pop() function
In Python, both .append() and .insert() are methods used to add elements to a list. But what is the difference between them?
.append(x) adds an element x to the end of the list
.insert(i, x) adds an element x at a specific position i in the list. The elements after position i are shifted to the right.