Python

0.0(0)
studied byStudied by 10 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/191

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:24 PM on 1/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

192 Terms

1
New cards

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.

<p>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 <strong>constant</strong> (can’t be changed). This may also solely signify a Global Variable.</p>
2
New cards

If __name__ == “__main__”:

When you import a module, Python executes all its code. This pattern checks if the file is being run directly versus imported. When run directly, a special variable equals "__main__", but when imported, it equals the module’s name instead. Code inside this conditional block only runs when executed directly, letting you include test/demo code that won’t run during imports.​​​​​​​​​​​​​​​​

3
New cards

How do you import the p⁠i⁠ functionality from the m⁠a⁠t⁠h⁠ module?

from math import pi

<p>from math import pi </p>
4
New cards

What does the f⁠r⁠o⁠m⁠ keyword do when importing modules?

It allows us to import parts of a module's functionality.

5
New cards

How do we import multiple modules in the same import statement?

By placing a comma between the modules we're importing.

6
New cards

What's the s⁠t⁠a⁠t⁠i⁠s⁠t⁠i⁠c⁠s⁠ module for?

It provides methods to help with common statistical calculations.

7
New cards
<p>In this scenario, why do we not need to state statistics.mean() to use the mean function from the statistics module?</p>

In this scenario, why do we not need to state statistics.mean() to use the mean function from the statistics module?

Because we used the from keyword in the import statement to import the mean() function individually.

8
New cards

How do you import a specific function from a Python module without importing the whole function?

You use the from keyword with the import keyword.

<p>You use the <strong>from </strong>keyword with the <strong>import </strong>keyword.</p>
9
New cards

How do you access the mean() function in Python?

You must import the statistics module.

<p>You must import the statistics module. </p>
10
New cards

Can we use modules to reuse other people's code?

Yes, modules are a way developers make their code available online for people to use.

11
New cards

What's the difference between a module and a class?

Modules usually contain lots of classes, while a class is singular.

12
New cards

What instruction can we use to find out the functionality a module has?

We use help(<moduleName>).

13
New cards

How do we use the .sqrt() method from Python’s built-in math module?

We state the module name, followed by the method name and the data we are accessing/modifying. For example, math.sqrt(16)

<p>We state the module name, followed by the method name and the data we are accessing/modifying. For example, math.sqrt(16)</p>
14
New cards

How do we gain access to a module so that we can use its features in our code?

To use a module, we import it with the keyword i⁠m⁠p⁠o⁠r⁠t⁠ followed by the module's name.

15
New cards

What data structure follows the FIFO (First in, First out) principle?

A queue

16
New cards

What is the use of a module in OOP?

Modules group related classes and data and make them accessible from one place.

<p><span>Modules group related classes and data and make them accessible from one place.</span></p>
17
New cards

What are the steps taken in the compiling process?

1 - Preprocessing step (takes care of any of the hash symbols in C, such as hashdefine)

2 - Compiling step (compiles your code into assembly code)

3 - Assembling step (converts that assembly code into machine code)

4 - Linking step (takes your machine code and combines it with your installed libraries to give you one executable program)

18
New cards

What is the largest number an 8-bit unsigned binary digit can represent?

255 (there’s 256 possibilities, but remember that we always start counting from 0)

19
New cards

How do we abstract a class?

Write a few core methods that handle low-level functions.

20
New cards
<p>Call the method defined in the class using the  d⁠r⁠o⁠n⁠e⁠  variable.</p>

Call the method defined in the class using the d⁠r⁠o⁠n⁠e⁠ variable.

drone.fly()

21
New cards
<p>Fill in the missing code so that instances of S⁠t⁠u⁠d⁠e⁠n⁠t⁠ receive a n⁠a⁠m⁠e⁠ property.</p>

Fill in the missing code so that instances of S⁠t⁠u⁠d⁠e⁠n⁠t⁠ receive a n⁠a⁠m⁠e⁠ property.

def __init__(self, name):

self.name = name

<p>def __init__(self, name):</p><p>self.name = name</p>
22
New cards
<p>Which methods does  p⁠h⁠o⁠n⁠e⁠  have?</p>

Which methods does p⁠h⁠o⁠n⁠e⁠ have?

call() and charge()

23
New cards

When a function is inside an object, what is it called?

A method.

24
New cards
<p>How can we customize a C⁠o⁠w⁠b⁠o⁠y⁠ ' to have the same functionality as P⁠e⁠r⁠s⁠o⁠n⁠ except g⁠r⁠e⁠e⁠t⁠(⁠)⁠ prints ‘H⁠o⁠w⁠d⁠y⁠!⁠’ ?</p>

How can we customize a C⁠o⁠w⁠b⁠o⁠y⁠ ' to have the same functionality as P⁠e⁠r⁠s⁠o⁠n⁠ except g⁠r⁠e⁠e⁠t⁠(⁠)⁠ prints ‘H⁠o⁠w⁠d⁠y⁠!⁠’ ?

Add a subclass of Person called Cowboy and change it to print Howdy! in greet ().

25
New cards

How does a subclass override its inherited methods?

Defining the inherited method in the subclass and changing the behavior.

26
New cards

Polymorphism refers to objects' ability to do which of the following?

Customize their implementation of an inherited behavior.

27
New cards
<p>Instances of  E⁠l⁠e⁠c⁠t⁠r⁠i⁠c⁠  will inherit the overridden method from  E⁠l⁠e⁠c⁠t⁠r⁠i⁠c⁠ , not the original one from  C⁠a⁠r⁠ . What will  t⁠e⁠s⁠l⁠a⁠.⁠d⁠r⁠i⁠v⁠e⁠(⁠)⁠  log?</p>

Instances of E⁠l⁠e⁠c⁠t⁠r⁠i⁠c⁠ will inherit the overridden method from E⁠l⁠e⁠c⁠t⁠r⁠i⁠c⁠ , not the original one from C⁠a⁠r⁠ . What will t⁠e⁠s⁠l⁠a⁠.⁠d⁠r⁠i⁠v⁠e⁠(⁠)⁠ log?

Whirr!

28
New cards

What does Polymorphism ensure when an object’s/instance’s method is called?

Polymorphism ensures that the proper method will be executed based on the calling object’s class.

29
New cards

With inheritance, we can extend a child class's functionality. But what if we want to implement class behaviors differently from each other?

We can use something called polymorphism. This means a subclass can override the methods it inherits from its superclass. We simply set the same name method on the subclass.

In this example, in the Lion subclass, we are changing the behaviour of the speak() method Lion inherited from the Feline class. We do this my redefining the method with def speak(self):

<p>We can use something called polymorphism. This means a subclass can override the methods it inherits from its superclass. We simply set the same name method on the subclass.</p><p>In this example, in the Lion subclass, we are changing the behaviour of the speak() method Lion inherited from the Feline class. We do this my redefining the method with def speak(self):</p>
30
New cards

Why is implementing abstraction important in OOP?

So others can use a class without knowing how it works.

31
New cards

How do we implement abstraction in OOP?

Write a few core methods that handle low-level functions.

32
New cards
<p>Choose the correct class so that instances of Professor can <strong>inherit</strong> both  n⁠a⁠m⁠e⁠  and  s⁠u⁠b⁠j⁠e⁠c⁠t⁠  properties.</p>

Choose the correct class so that instances of Professor can inherit both n⁠a⁠m⁠e⁠ and s⁠u⁠b⁠j⁠e⁠c⁠t⁠ properties.

class Professor(Teacher):

<p>class Professor(Teacher):</p>
33
New cards
<p>Sally is a 40-year-old mother of two kids. Create a new instance that encapsulates her information.</p>

Sally is a 40-year-old mother of two kids. Create a new instance that encapsulates her information.

parent = Parent(“Sally”, 40, 2)

<p>parent = Parent(“Sally”, 40, 2)</p>
34
New cards
<p>Call  s⁠u⁠p⁠e⁠r⁠(⁠)⁠.⁠_⁠_⁠i⁠n⁠i⁠t⁠_⁠_⁠  with  n⁠a⁠m⁠e⁠  and  a⁠g⁠e⁠  so that instances of  P⁠a⁠r⁠e⁠n⁠t⁠  receive these properties.</p>

Call s⁠u⁠p⁠e⁠r⁠(⁠)⁠.⁠_⁠_⁠i⁠n⁠i⁠t⁠_⁠_⁠ with n⁠a⁠m⁠e⁠ and a⁠g⁠e⁠ so that instances of P⁠a⁠r⁠e⁠n⁠t⁠ receive these properties.

super().__init__(name, age)

<p>super().__init__(name, age)</p>
35
New cards
<p>How would you create a subclass called  L⁠a⁠p⁠t⁠o⁠p⁠  that inherits the  t⁠u⁠r⁠n⁠O⁠n⁠(⁠)⁠  method from  C⁠o⁠m⁠p⁠u⁠t⁠e⁠r⁠ .</p>

How would you create a subclass called L⁠a⁠p⁠t⁠o⁠p⁠ that inherits the t⁠u⁠r⁠n⁠O⁠n⁠(⁠)⁠ method from C⁠o⁠m⁠p⁠u⁠t⁠e⁠r⁠ .

class Laptop(Computer):

36
New cards
<p>Which methods can we call on the  j⁠e⁠t⁠  instance?</p>

Which methods can we call on the j⁠e⁠t⁠ instance?

Both fly() and invert()

37
New cards
<p>The  d⁠e⁠v⁠  instance is missing the  c⁠o⁠m⁠p⁠a⁠n⁠y⁠  property. What line do we need to add to the  D⁠e⁠v⁠e⁠l⁠o⁠p⁠e⁠r⁠  subclass?</p>

The d⁠e⁠v⁠ instance is missing the c⁠o⁠m⁠p⁠a⁠n⁠y⁠ property. What line do we need to add to the D⁠e⁠v⁠e⁠l⁠o⁠p⁠e⁠r⁠ subclass?

super().__init__(company)

38
New cards

Which class method sets the properties of instances?

__init__()

39
New cards
<p>What is the super() function used for?</p>

What is the super() function used for?

super() is a Python function that lets a child class access methods from its parent class. It’s used for inheritance.

So super().__init__ is used to modify the properties the child class inherited from its parent class.

40
New cards

How does inheritance make our OOP code more efficient?

Functions need to be set only once.

41
New cards

In OOP, what is it called when classes receive methods from other classes to improve efficiency?

Inheritance. Inheritance lets us create classes that have different properties and behaviours without coding each one from scratch.

Here we see that the C⁠h⁠i⁠l⁠d⁠ class is inheriting the P⁠a⁠r⁠e⁠n⁠t⁠ class because it's inside the parentheses after the class name.

<p>Inheritance. Inheritance lets us create classes that have different properties and behaviours without coding each one from scratch.</p><p>Here we see that the  C⁠h⁠i⁠l⁠d⁠  class is inheriting the  P⁠a⁠r⁠e⁠n⁠t⁠  class because it's inside the parentheses after the class name.</p>
42
New cards
<p>What is the point in the self keyword in the return statement?</p>

What is the point in the self keyword in the return statement?

When you write self.base inside the method, you’re saying “look for the base attribute on whatever instance (object using this class) eventually calls this method.”

So self is the mechanism that connects the method to the instance’s data (or the class’s data as a fallback).​​​​​​​​​​​​​​​​

43
New cards
<p>In FP, code is not encapsulated. Can you explain why this code does not show encapsulation?</p>

In FP, code is not encapsulated. Can you explain why this code does not show encapsulation?

The data and the function are not grouped together in an object.

44
New cards
<p>In OOP, what is it called when we group together related data and functions in the same object?</p>

In OOP, what is it called when we group together related data and functions in the same object?

Encapsulation

45
New cards

What is the self parameter used for when creating an instance method?

In Python, self is a conventional name for the first parameter of instance methods in a class. It represents the instance of the class itself (the specific object that the method is being called on).

When you call a method on an object, Python automatically passes that object as the first argument to the method (self). The self parameter receives this object, allowing the method to access and modify the object’s/instance’s attributes and call other methods on that same instance.

46
New cards
<p>Write the code to add a method called  a⁠d⁠d⁠M⁠o⁠n⁠e⁠y⁠(⁠)⁠  that accepts a parameter called  a⁠m⁠o⁠u⁠n⁠t⁠:</p>

Write the code to add a method called a⁠d⁠d⁠M⁠o⁠n⁠e⁠y⁠(⁠)⁠ that accepts a parameter called a⁠m⁠o⁠u⁠n⁠t⁠:

def addMoney(self, amount):

<p>def addMoney(self, amount):</p>
47
New cards

What is object-oriented programming?

A programming style where we use objects to bundle together related data (properties) and functionality (methods).

48
New cards
<p>Is this code an example of Functional Programming (FP) or Object-Oriented Programming?</p>

Is this code an example of Functional Programming (FP) or Object-Oriented Programming?

It’s Object-Oriented Programming (OOP), where we group data and functionality as properties and methods inside objects, like V⁠i⁠r⁠t⁠u⁠a⁠l⁠_⁠P⁠e⁠t⁠ here.

49
New cards

True or False, In the FP style, we keep data and functionality separate?

True, we pass data into functions whenever we want something.

<p>True, we pass data into functions whenever we want something.</p>
50
New cards

Functional Programming (FP)

A programming paradigm focused on building applications by composing pure, mathematical functions, treating functions as first-class citizens (data) to avoid shared mutable state and side effects, resulting in declarative, modular, and often more predictable code, especially useful for complex tasks like big data and concurrency. It emphasizes immutability, expressions over statements, and uses recursion instead of loops. 

51
New cards

What is object-oriented programming (OOP) and functional programming (FP) examples of?

Paradigms

52
New cards

What term is used to refer to different styles of coding?

Paradigms

53
New cards
<p>What's wrong with this code?</p>

What's wrong with this code?

self needs to be the first parameter of the __init__ method

54
New cards
<p>What is the difference between  n⁠a⁠m⁠e⁠  and  s⁠e⁠l⁠f⁠.⁠n⁠a⁠m⁠e⁠  in the following code?</p>

What is the difference between n⁠a⁠m⁠e⁠ and s⁠e⁠l⁠f⁠.⁠n⁠a⁠m⁠e⁠ in the following code?

name refers to the value passed when the instance is created and self. name refers to the class variable, name

55
New cards
<p>What’s wrong with this code?</p>

What’s wrong with this code?

print(self.email) should be print(jane.email) instead

56
New cards
<p>Create an instance called song of the class where  n⁠a⁠m⁠e⁠ ⁠ ⁠=⁠ "⁠H⁠a⁠p⁠p⁠y⁠ B⁠i⁠r⁠t⁠h⁠d⁠a⁠y⁠"⁠  and  a⁠r⁠t⁠i⁠s⁠t⁠ =⁠ "⁠u⁠n⁠k⁠n⁠o⁠w⁠n⁠"⁠ </p>

Create an instance called song of the class where n⁠a⁠m⁠e⁠ ⁠ ⁠=⁠ "⁠H⁠a⁠p⁠p⁠y⁠ B⁠i⁠r⁠t⁠h⁠d⁠a⁠y⁠"⁠ and a⁠r⁠t⁠i⁠s⁠t⁠ =⁠ "⁠u⁠n⁠k⁠n⁠o⁠w⁠n⁠"⁠

song = Song ( "Happy Birthday", "unknown")

<p><span>song = Song ( "Happy Birthday", "unknown")</span></p>
57
New cards

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.

58
New cards
<p>What is wrong with this code?</p>

What is wrong with this code?

The class keyword is missing.

59
New cards

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 P⁠e⁠r⁠s⁠o⁠n⁠ here.

<p>Class names usually have the first letter capitalized and the rest lowercase, like with  P⁠e⁠r⁠s⁠o⁠n⁠  here.</p>
60
New cards

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.

<p>This is the start of a type hint, which tell you and other programmers what data types to expect. A -&gt; and the data type following it tells you the type of data this function returns.</p>
61
New cards

What are hash tables known as in Python?

Dictionaries or dict

62
New cards
<p>Write a print statement that displays the first item of the class variable c⁠o⁠u⁠n⁠t⁠r⁠i⁠e⁠s⁠.</p>

Write a print statement that displays the first item of the class variable c⁠o⁠u⁠n⁠t⁠r⁠i⁠e⁠s⁠.

print(gymnastics.countries[0])

<p>print(gymnastics.countries[0])</p>
63
New cards

What are classes used for?

To reduce repeated code by grouping it into a class.

64
New cards

When do we use the keyword s⁠e⁠l⁠f⁠ ?

When we need to access class variables or methods inside the class definition.

65
New cards

How do we define the constructor method?

def __init__(self):

66
New cards

What is the purpose of a constructor?

To construct an instance of a class object with unique class variables.

67
New cards
<p>How do we call the display_color function defined inside the Flower class?</p>

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()

<p>We can use an instance of the class, a ., and then the name of the function. For example, rose_flower = Flower() &amp; rose_flower.display_color() </p>
68
New cards
<p>What does the code self.color = color do in this example?</p>

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”)).

69
New cards

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.

70
New cards

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.

<p>The constructor method looks like __init__() and allows us to set unique values for the class variables when we create an instance.</p>
71
New cards
<p>Access and call the method  i⁠n⁠t⁠r⁠o⁠d⁠u⁠c⁠e⁠  using the class instance  p⁠i⁠k⁠a⁠c⁠h⁠u⁠ .</p>

Access and call the method i⁠n⁠t⁠r⁠o⁠d⁠u⁠c⁠e⁠ using the class instance p⁠i⁠k⁠a⁠c⁠h⁠u⁠ .

pikachu.introduce()

<p>pikachu.introduce()</p>
72
New cards

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.

<p>Yes, you still include self as the first parameter.</p>
73
New cards
<p>How do we access  r⁠o⁠c⁠k⁠y⁠ 's method  p⁠r⁠i⁠n⁠t⁠_⁠c⁠o⁠l⁠o⁠r⁠?</p>

How do we access r⁠o⁠c⁠k⁠y⁠ 's method p⁠r⁠i⁠n⁠t⁠_⁠c⁠o⁠l⁠o⁠r⁠?

rocky.print_color()

74
New cards

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 s⁠e⁠l⁠f⁠ as a parameter in class methods so that they can access the class variables (by making it within their scope).

75
New cards

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.

<p>In Python, methods are <strong>functions that are defined as part of a class</strong>.</p><p>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.</p>
76
New cards

Why do we need to store the class instance in a variable?

So we can access the class instance.

77
New cards

What syntax do you use to create a class object instance?

You need the class name followed by ().

78
New cards

What are variables inside a class used for?

To store data that is related to the given class.

79
New cards

What is a class?

A template that can have variables and functionality associated with it. Can be used to create class objects.

80
New cards
<p>What does this code do?</p>

What does this code do?

We're defining a class (VirtualPet) and creating an instance (an object) of it (fluffy)

81
New cards

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__.

<p>Instantiation is when you create an object (instance) from a class. <span>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__.</span></p>
82
New cards

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.

<p><span>A class definition is the code that creates a new class. It describes what attributes and methods the class will have.</span></p>
83
New cards

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 s⁠k⁠i⁠p⁠p⁠y⁠.⁠w⁠a⁠g⁠g⁠i⁠n⁠g⁠_⁠t⁠a⁠i⁠l⁠ here.

<p>To access a class variable, we add the instance name (variable containing class definition), a  .⁠ , and the name of the variable we want. Like  s⁠k⁠i⁠p⁠p⁠y⁠.⁠w⁠a⁠g⁠g⁠i⁠n⁠g⁠_⁠t⁠a⁠i⁠l⁠  here.</p>
84
New cards
<p>What are we doing here?</p>

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).

85
New cards

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.

<p>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.</p>
86
New cards
<p>What happens when we slice a list with a step value but without providing a start position or stop position?</p>

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.

87
New cards

True or False, when using a negative s⁠t⁠e⁠p⁠ value when slicing, start needs to be either omitted or greater than stop to return elements?

True

88
New cards
<p>What will this code return? </p>

What will this code return?

['B', 'D', 'F'] , we can use a different slicing format with two colons with [⁠s⁠t⁠a⁠r⁠t⁠:⁠s⁠t⁠o⁠p⁠:⁠s⁠t⁠e⁠p⁠]⁠ , where the s⁠t⁠e⁠p⁠ determines how Python steps between s⁠t⁠a⁠r⁠t⁠ and e⁠n⁠d⁠.

89
New cards

How can we start creating a template called Person via a class?

We add the keyword c⁠l⁠a⁠s⁠s⁠ followed by a name for class template (Person) and a colon.

<p>We add the keyword  c⁠l⁠a⁠s⁠s⁠  followed by a name for class template (Person) and a colon.</p>
90
New cards
<p>What happens if the step value in a list slice is negative?</p>

What happens if the step value in a list slice is negative?

The step value can be negative, which allows us to use a s⁠t⁠a⁠r⁠t⁠ position larger than the e⁠n⁠d⁠ value. The order of the elements is reversed.

<p>The step value can be negative, which allows us to use a  s⁠t⁠a⁠r⁠t⁠  position larger than the  e⁠n⁠d⁠  value. The order of the elements is reversed.</p>
91
New cards
<p>What temperatures will be printed?</p>

What temperatures will be printed?

All of them.

92
New cards

What does the del keyword do?

Allows us to delete objects, or items within a data structure.

93
New cards

What happens when we use the loop variable as the expression in a list comprehension?

We copy the original list.

94
New cards
<p>Complete the list slicing in the code so that  [⁠3⁠,⁠ 2⁠,⁠ 1⁠]⁠  is printed at the end.</p>

Complete the list slicing in the code so that [⁠3⁠,⁠ 2⁠,⁠ 1⁠]⁠ is printed at the end.

[::2]

<p>[::2]</p>
95
New cards
<p>Complete the  s⁠t⁠a⁠r⁠t⁠:⁠s⁠t⁠o⁠p⁠:⁠s⁠t⁠e⁠p⁠  notation so that  [⁠5⁠,⁠ 5⁠,⁠ 5⁠]⁠  is printed.</p>

Complete the s⁠t⁠a⁠r⁠t⁠:⁠s⁠t⁠o⁠p⁠:⁠s⁠t⁠e⁠p⁠ notation so that [⁠5⁠,⁠ 5⁠,⁠ 5⁠]⁠ is printed.

[-1:2:-2]

96
New cards
<p>Should the following code be grouped into a class?</p>

Should the following code be grouped into a class?

Yes, a class could group a Person with the variable's 'name' and 'age'.

97
New cards

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.

<p>We add in to the end of the list comprehension to filter the elements in the new list that meet a certain condition. </p>
98
New cards
<p>Why can’t we use this get_full_name() function inside a list comprehension?</p>

Why can’t we use this get_full_name() function inside a list comprehension?

Because it doesn’t have a return value.

99
New cards
<p>What is the difference between the halved_lc variable and the halved_loop variable?</p>

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

100
New cards

How do we conventionally indicate that something is a class?

Its name will start with a capital letter.