Beginner Python Cheatsheet (Python Crash Course) Flashcards

1
Q

Dictionary:
Add to a dictionary where the key is ‘x_position’ the value is 0 and the dictionary name is alien

Here is the cheatsheet source: http://ehmatthes.github.io/pcc/cheatsheets/README.html

A

alien[‘x_position’] = 0

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

Dictionary:

Looping through all key-value pairs in a dictionary called fav_numbers

A

fav_numbers = {‘eric’: 17, ‘ever’: 4}
for name, number in fav_numbers.items():
print(name + ‘ loves ‘ + str(number))

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

Dictionary:

  1. Looping through all keys in a dictionary called fav_numbers
  2. Looping through all values in a dictionary called fav_numbers
A
1. fav_numbers = {'eric': 17, 'ever': 4}
for name in fav_numbers.keys():
 print(name + ' loves a number')
2. fav_numbers = {'eric': 17, 'ever': 4}
for number in fav_numbers.values():
 print(str(number) + ' is a favorite')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Lists:

How do you check for a value in a list? E.g. check for ‘trek’ in a list called ‘bikes’

A

‘trek’ in bikes

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

List Comprehension:

Square all values in a range(1,11)

A

squares = [x**2 for x in range(1, 11)]

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

Functions:

Create a function with a default parameter

A
def make_pizza(topping='bacon'):
 """Make a single-topping pizza."""
 print("Have a " + topping + " pizza!")
make_pizza()
make_pizza('pepperoni')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Classes - Inheritance:
1. When you use inheritance, do you have to use super() to inherit the attributes in order to use the methods?

  1. Give an example of super used to define attributes. The class is a square and inherits rectangle. The parameters are length.
A
  1. Yes, otherwise the program doesn’t know which attributes to align with the old attributes
2. 
class Rectangle:
    def \_\_init\_\_(self, length, width):
        self.length = length
        self.width = width
    def area(self):
        return self.length * self.width
    def perimeter(self):
        return 2 * self.length + 2 * self.width
class Square(Rectangle):
    def \_\_init\_\_(self, length):
        super().\_\_init\_\_(length, length)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to you read a text file and iterate through all lines in the file?

E.g. the text file name is ‘s.txt’

A
filename = 's.txt'
with open(filename) as file_object:
 lines = file_object.readlines()
for line in lines:
 print(line)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The ‘try’ statement should be followed by what other statement?

A

‘Except’. E.g.:

prompt = "How many tickets do you need? "
num_tickets = input(prompt)
try:
 num_tickets = int(num_tickets)
except ValueError:
 print("Please try again.")
else:
 print("Your tickets are printing.")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly