{.separator}After this lesson, you will be able to:
You're leveling up!
You have the proper foundation. Now, let's check how you're doing.
[]).0.```python my_queens = ["Cersei", "Daenerys", "Arwen", "Elsa", "Guinevere"] step_counts_this_week = [8744, 5256, 7453, 3097, 4122, 2908, 6720]
weird_list = [1, "weird", ["nested list"], "eh?"] ```
Challenge: Can you recall how to slice a section of the list? For example, items 2 through 5 of
step_counts_this_week?
: to represent a range of indices.python
step_counts_this_week = [8744, 5256, 7453, 3097, 4122, 2908, 6720]
days_2_thru_5 = step_counts_this_week[2:6] # Items 2, 3, 4, and 5
Pro tip: It's
6instead of5because the range is exclusive.
What about looping a list?
```python my_queens = ["Cersei", "Daenerys", "Arwen", "Elsa", "Guinevere"]
for queen in my_queens: print(queen, "is the most powerful queen!") ```
Challenge: What if I want to loop from 1 to 10 and print out the numbers? How do I do this without a data structure to loop over?
To loop 1–10 without a data structure:
```python
for i in range(1, 11): print(i) ```
11 in the code?{}) or from lists with the set() function.```python email_set = {'my_email@gmail.com', 'second_email@yahoo.com', "third_email@hotmail.com"}
my_list = ["red", "yellow", "green", "red", "green"] my_set = set(my_list)
```
()).python
rainbow_colors_tuple = ("red", "orange", "yellow", "green", "blue", "indigo", "violet")
{key: value, key: value}).python
my_puppy = {
"name": "Fido",
"breed": "Corgi",
"age": 3,
"vaccinated": True,
"fave toy": ["chew sticks", "big sticks", "any sticks"]
}
Challenge: Can you recall how to iterate (loop) over each key of
my_puppyand print out both the key and the corresponding value?
Iterating a dictionary is similar to a list:
python
for key in my_puppy:
print(key, "-", my_puppy[key])
Outputs:
name - Fido
breed - Corgi
age - 3
vaccinated - True
fave toy - chew sticks
def, (), and :.```python
def say_hello(): print("hello!")
say_hello() say_hello() say_hello() ```
Parameters are in the function definition.
Challenge: Rewrite the code below to use a single function with one parameter.
print statement!```python def multiply(x, y): return x * y
result = multiply(3, 4) # Result is now equal to 12. ```
self is a reference to the created object.```python class Animal(): def init(self): self.energy = 50
def get_status(self):
if self.energy < 20:
print("I'm hungry!")
elif self.energy > 100:
print("I'm stuffed!")
else:
print("I'm doing well!")
```
Challenge: How do you declare a new
Animal?
Declaring a new Animal from the class:
python
my_animal = Animal() # Creates a new Animal instance.
my_animal.get_status() # Prints "I'm doing well!"
my_animal.energy += 100 # We can access properties!
my_animal.get_status() # Prints "I'm stuffed!"
A class can inherit properties and methods from another class.
You Do: Create a new class, Dog, which inherits from Animal.
Dog has an extra function, bark(), that prints "bark".Dog has an extra property, breed.| Table of Contents | t |
|---|---|
| Exposé | ESC |
| Full screen slides | e |
| Presenter View | p |
| Source Files | s |
| Slide Numbers | n |
| Toggle screen blanking | b |
| Show/hide slide context | c |
| Notes | 2 |
| Help | h |