python Flashcards

1
Q

reverse python string

A

txt = “Hello World”
txt = txt[::-1]
print(txt)

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

Add list to class

A

class Person():
#self represents the object itself
def __init__(self, name):
self.name = name
self.books = []

def add_book(self, title):
    self.books.append(title)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

python used most often to _____

A

Websites, data analysis & visualization, task automation.

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

Python is interpreted, meaning ____

A

it does not need to compile

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

Python passes parameters by ____

A

reference by default, but also by value

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

what is __init__

A

the first method of a class. when you instantiate an object it is automatically invoked to initialize members of a class.

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

remove whitespace from string

A

string.strip()

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

shuffle list

A

import random
random.shuffle(list1)

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

what does break do?

A

breaks out of the current loop

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

string slicing syntax

A

str[1:5]
index starts at 0, inclusive 1st field, exclusive 2nd field.

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

class creation

A

class Person:
def sayHello(self):
print(“Hello”)

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

create class instance and use method

A

p1 = Person()
p1.sayHello()

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

define class function that takes a parameter

A

class Person:
def add_one(self, x):
return x + 1

print(p1.add_one(5))

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

class init method purpose

A

method is called whenever an instance of it is created

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

init method syntax

A

def __init__(self):
pass

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

init method to create an object with a name by default

A

def __init__(self, name):
self.name = name

17
Q

getter method

A

def get_name(self):
return self.name

18
Q

setter method

A

def set_age(self, name):
self.name = name

19
Q

make classes that are connected to each other (add person to course)

A

class Course:
def __init__(self, name):
self.name = name
self.people = []

 def add_person(self, person):
      self.people.append(person)
20
Q

inheritance is used when?

A

When classes have a lot in common. You only need to hard code the different parts in the classes and inherit the similar code.

21
Q

how inheritance is implemented

A

You have subclasses take the parent class as a parameter. if Pet is the parent class and cat is a subclass, you’d do class Cat(Pet):

22
Q

overriding in inheritance

A

subclass functions will override parent class methods with the same name

23
Q

how to extend init in the parent class if you have an extra attribute you want to add (color)

A

if you have a Pet parent class with an init method that has a name parameter, the subclass will look like this:
class Cat(Pet):
def__init__(self, name, color):
super().__init__(name)
self.color = color

24
Q

what is a class attribute

A

something you can access using the name of the class.
class Person:
num = 0

print(Person.num)
# displays 0

25
Q

how to modify class attribute

A

you can assign a new value to it outside of the class. if you do Person.num = 5, and future person objects created after this will have a num of 5.

26
Q

class method syntax

A

@classmethod
def num(cls):
return cls.num()

27
Q

what does a class method do?

A

acts on the class itself. So you can keep track of changes to class attributes

28
Q

class method example

A

class Person:
number_of_people = 0
def __init__(self):
Person.add_person()
@classmethod
def add_person(cls):
return cls.num

29
Q

when are static methods used?

A

in a class which contains common functions, but without needing to create an instance of the class to use them.

30
Q

static method implementation

A

class Math:
@staticmethod
def add_one(x):
return x + 1

print(Math.add_one(1))
#displays 2

31
Q

what do static methods not do?

A

change anything