python Flashcards
reverse python string
txt = “Hello World”
txt = txt[::-1]
print(txt)
Add list to class
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)
python used most often to _____
Websites, data analysis & visualization, task automation.
Python is interpreted, meaning ____
it does not need to compile
Python passes parameters by ____
reference by default, but also by value
what is __init__
the first method of a class. when you instantiate an object it is automatically invoked to initialize members of a class.
remove whitespace from string
string.strip()
shuffle list
import random
random.shuffle(list1)
what does break do?
breaks out of the current loop
string slicing syntax
str[1:5]
index starts at 0, inclusive 1st field, exclusive 2nd field.
class creation
class Person:
def sayHello(self):
print(“Hello”)
create class instance and use method
p1 = Person()
p1.sayHello()
define class function that takes a parameter
class Person:
def add_one(self, x):
return x + 1
print(p1.add_one(5))
class init method purpose
method is called whenever an instance of it is created
init method syntax
def __init__(self):
pass