Python

0.0(0)
studied byStudied by 9 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/137

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

138 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
<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>
3
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.

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

What is wrong with this code?

The class keyword is missing.

5
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>
6
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>
7
New cards

What are hash tables known as in Python?

Dictionaries or dict

8
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>
9
New cards

What are classes used for?

To reduce repeated code by grouping it into a class.

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

11
New cards

How do we define the constructor method?

def __init__(self):

12
New cards

What is the purpose of a constructor?

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

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

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

16
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>
17
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>
18
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>
19
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()

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

21
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>
22
New cards

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

So we can access the class instance.

23
New cards

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

You need the class name followed by ().

24
New cards

What are variables inside a class used for?

To store data that is related to the given class.

25
New cards

What is a class?

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

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

27
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>
28
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>
29
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>
30
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).

31
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>
32
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.

33
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

34
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⁠.

35
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>
36
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>
37
New cards
<p>What temperatures will be printed?</p>

What temperatures will be printed?

All of them.

38
New cards

What does the del keyword do?

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

39
New cards

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

We copy the original list.

40
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>
41
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]

42
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'.

43
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>
44
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.

45
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

46
New cards

How do we conventionally indicate that something is a class?

Its name will start with a capital letter.

47
New cards

To eliminate duplicates from a list, how we can transform that list into a set?

We code set(list_name)

<p>We code set(list_name)</p>
48
New cards

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

Curly braces {}

<p>Curly braces {}</p>
49
New cards

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.

50
New cards

What do we use to retrieve multiple values from a list?

Slicing (slice notation)

<p>Slicing (slice notation)</p>
51
New cards
<p>Which of these functions can we use inside a list comprehension?</p>

Which of these functions can we use inside a list comprehension?

The divide(number, divisor) function as it has a return value.

52
New cards
<p>What will this code print?</p>

What will this code print?

[] an empty list

53
New cards

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.

<p>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.</p>
54
New cards
<p>Which of these two variables stores a set?</p>

Which of these two variables stores a set?

team_2 because the values are surrounded by curly braces {}

55
New cards
<p>What does this display in the console?</p>

What does this display in the console?

An error because “pumila” isn’t present in the set.

<p>An error because “pumila” isn’t present in the set.</p>
56
New cards

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.

57
New cards
<p>What happens when we run this code?</p>

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.

58
New cards
<p>What happens when we specify a range outside the length of a list as the starting index of a slice?</p>

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.

<p>It will return an empty list, rather than an error.</p>
59
New cards

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.

60
New cards

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.

61
New cards

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.

<p>Negative indexing allows us to retrieve values from the end of an indexable object, such as a list.</p>
62
New cards
<p>What has REGISTRANTS been initialised as?</p>

What has REGISTRANTS been initialised as?

REGISTRANTS is a global variable that has been initialised as an empty dictionary / dict object.

63
New cards

What function do you use to add a new value to a set (unordered)?

You use .add()

<p>You use .add()</p>
64
New cards

What does the d⁠i⁠f⁠f⁠e⁠r⁠e⁠n⁠c⁠e⁠(⁠)⁠ 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.

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

What's wrong with this code?

Sets don't have indices, so we can't access elements by indices like with reactions [0]

66
New cards

When we want to make sure that a collection of values can't have any duplicates, what do we store it in?

A set

67
New cards

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.

<p>It will retrieve all the values from that starting list position to the end of the list. </p>
68
New cards

How do we remove an element from a set?

Use the .remove() function and specify which value inside the parentheses.

69
New cards

How do we check if a dictionary contains a certain key?

With the in keyword

70
New cards

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.

71
New cards

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.

<p>When all the elements in a set are present in another larger set, we call that smaller set a subset.</p>
72
New cards

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)

<p>You can use the .issubset() function. Where you code potential_subset.issubset(set)</p>
73
New cards

How many values can a key be associated with?

Only one value can be associated with a key within a dictionary.

74
New cards

How do you identify a Decorator in Python and what does it do?

An @ symbol indicates a decoratorDecorators are a way to modify or enhance functions (or classes) without changing their actual code.

<p>An <code>@</code> symbol indicates a<strong> decorator</strong>.&nbsp;<span><strong><span>Decorators</span></strong><span> are a way to modify or enhance functions (or </span><strong><span>classes</span></strong><span>) without changing their actual code.</span></span></p>
75
New cards
<p>What does this code print?</p>

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.

76
New cards

What do we use to loop through all the keys in a dictionary?

A for loop

77
New cards

What do we use p⁠o⁠p⁠(⁠)⁠ for when it comes to dictionaries?

To remove a specific key and its associated value.

78
New cards
<p>What does the  d⁠i⁠f⁠f⁠e⁠r⁠e⁠n⁠c⁠e⁠  set contain?</p>

What does the d⁠i⁠f⁠f⁠e⁠r⁠e⁠n⁠c⁠e⁠ set contain?

{‘giraffe’, ‘whale’}

79
New cards

What’s the difference between the .union() function and the .intersection() function?

While u⁠n⁠i⁠o⁠n⁠(⁠)⁠ gives us all elements of the two sets, i⁠n⁠t⁠e⁠r⁠s⁠e⁠c⁠t⁠i⁠o⁠n⁠(⁠)⁠ gives us only the common ones.

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

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.

81
New cards
<p>What does this list comprehension do?</p>

What does this list comprehension do?

It loops through item_prices and copies each value that is less than 50

82
New cards
<p>What does the code inside the loop do?</p>

What does the code inside the loop do?

It's displaying the tuple value associated with the key stored in the winner variable.

83
New cards
<p>What framework is the <code>render_template()</code> function from?</p>

What framework is the render_template() function from?

The Flask framework

84
New cards
<p>What happens if we run this code?</p>

What happens if we run this code?

We get an error because tuples are immutable.

85
New cards

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)

86
New cards
<p>What does  m⁠/⁠1⁠0⁠0⁠0⁠  represent in this list comprehension?</p>

What does m⁠/⁠1⁠0⁠0⁠0⁠ represent in this list comprehension?

The expression that is applied on each element accessed in the for loop.

87
New cards

What data types can a dictionary's keys be?

Strings, numbers, booleans, or tuples.

88
New cards

What commonly used templating engine is used for frameworks such as Flask, Bottle, Morepath, and Django (optionally)?

The Jinja2 engine

89
New cards

What is a dictionary called in Python?

A Dict Object

90
New cards

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.

91
New cards

What is the default method for a route?

The GET request method.

92
New cards

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.

93
New cards

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.

94
New cards

What are Django, Pyramid, and Flask examples of?

Web frameworks

95
New cards

What type of brackets do you use to declare items in a Python list?

Square brackets []

<p>Square brackets []</p>
96
New cards

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.

97
New cards

What happens when we use a⁠d⁠d⁠(⁠)⁠ to add a value that a set already contains?

The value won’t be added, since duplicates aren’t allowed in sets.

98
New cards

What should we return when we're just interested in multiple individual values

A tuple with multiple values.

99
New cards

What instruction do we use to remove a key-value pair from a dictionary?

.pop() function

100
New cards

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.