What is casting
Casting, also known as type conversion, is a process that converts a variable's data type into another data type
name a built in method
.split()
if you said something like print(), you'd be wrong because methods are functions that instances of a class run on themselves
Write a list comprehension that gives me the even numbers within [0-10] (that's inclusive)
[i for i in range (0, 11) if i%2==0]
What's the syntax of defining a class?
class ClassName:
#code in here
Write a dictionary correlating last names(items) to first names (keys) for George Lopez, Arianna Grande, and Taylor Swift
{'George': 'Lopez',
'Arianna': 'Grande',
'Taylor': 'Swift'}
What's the point of functions?
You use functions in programming to bundle a set of instructions that you want to use repeatedly or that, because of their complexity, are better self-contained in a sub-program and called when needed.
What's a list inside a list called
A nested list
list comprehension of everything in the list "stuff = ['a', 'b', 'aa', 'ca', 'ya' ,'b']" that starts with 'a'
[entry for entry in stuff if entry[0] == 'a']
What method runs upon instantiation of a class?
__init__(self)
write a function that takes in a str and returns the vowels. Only count Y if it's the last letter
def vowel_count(word):
count = 0
for letter in word:
if letter in 'aeiou':
count+=1
if word:
if word[-1] == 'y':
count +=1
return count
What's the point of making classes?
To encapsulate different types of data
name 5 iterable types
range, list, tuple, dict, set (yes, it is iterable)
using the following function:
def is_prime(n): #feel free to make use of this function
for i in range(2,n):
if (n%i) == 0:
return False
return True
make a list comp. of primes up to 1000
[num for num in range(1000) if is_prime(num)]
in the scope of a method, what's "self" mean
self refers to the instance of the class that's running the method. It is the specific member
Write a function that changes a float to the next nearest whole number n
round(n)
what is data hiding and how is it used
Data hiding in Python is the method to prevent access to specific users in the application. Used by placing an underscore in front of a variable
give an example of a keyword argument for a built-in-function
end (for print), sep (also for print)
give a list comprehension of all the powers of 2, but negate one from each result
[(2**num)-1 for num in range (10)]
Which function utilizes the __str__ function?
print()
take a random number out of this list and return it in one line of code (random is imported), but the number must be divisible by 3
list1 = [1, 4, 6, 3]
return random.choice([num for num in list1 if num%3==0])
What does object oriented programming have to do with Python?
Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. Most things in Python are objects, including functions, variables, and and instance of a class
name the argument types
(a, b, /, c, d, *, e, f)
a, b are positional
c, d are flexible
e, f are keyword-only
using a nested list comprehension, find all the lengths of the numbers divisible by 13 that are under 2000
[len(str(num)) for num in [i for i in range(2000) if i%13==0]]
write the line to import the class named "CoolGuy" from the file dope.py
-- DOUBLE JEOPARDY -- (that means double points)
write a pokemon class that has a dictionary of movenames with items corresponding to damage. Write a method that attacks another pokemon by choosing a random move and negating it from the other pokemon's health stat
import random
class Pokemon:
def __init__(self):
attacks = {}
health = 100
def attack(self, other):
attackkey = random.choice(self.attacks.keys())
other.damage(self.attacks[attackkey])
def damage(self, hurtpoints):
self.health -= hurtpoints