More on this book
Community
Kindle Notes & Highlights
by
Eric Matthes
Started reading
November 1, 2018
Numerical Lists
range() Function
for value in range(1,5):
it doesn’t print the number 5:
from 1 to 5, you
use range(1,6):
numbers = list(range(1,6))
even_numbers = list(range(2,11,2))
code. A list comprehension allows you to generate this same list in just one line of code.
squares = [value**2 for value in range(1,11)]
To make a slice, you specify the index of the first and last elements you want to work with.
Python stops one item before the second index you specify.
players[0:3])
For example, if we want to output the last three players on the roster, we can use the slice players[-3:]:
list, you can make a slice that includes the entire original list by omitting the first index and the second index ([:]).
Tuples
Python refers to values that cannot change as immutable, and an immutable list is called a tuple.
A tuple looks just like a list except you use parentheses instead of square brackets.
➊ dimensions = (200, 50)
When compared with lists, tuples are simple data structures. Use them when you want to store a set of values that should not be changed throughout the life of a program.
about tuples, which provide a degree of protection to a set of values that shouldn’t change,
➊ if car == 'bmw':
already in a list, use the keyword in.
keyword not in this situation.
Indentation plays the same role in if statements as it did in for loops.
The if-elif-else Chain
elif age < 18:
allow you to connect pieces of related information.
nest dictionaries
alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) print(alien_0['points'])
A dictionary in Python is a collection of key-value pairs.
In Python, a dictionary is wrapped in braces, {},
A key-value pair is a set of values associated with each other.
can add new key-value pairs to a dictionary at any time.
➊ for key, value in user_0.items(): ➋ print("\nKey: " + key) ➌ print("Value: " + value)
Looping Through All the Keys in a Dictionary
Looping through the keys is actually the default
behavior
for name in favorite_languages:
for name in favorite_languages.keys():
The keys() method isn’t just for looping: It actually returns a list of all the keys,
Nesting
'aeinstein': { 'first': 'albert', 'last': 'einstein', 'location': 'princeton', },
message = input("Tell me something, and I will repeat it back to you: ")
name = input(prompt)
➊ def greet_user():
Default Values
The return statement takes a value
return person
When you pass a list to a function, the function can modify the list.

