Python Crash Course: A Hands-On, Project-Based Introduction to Programming
Rate it:
14%
Flag icon
Numerical Lists
14%
Flag icon
range() Function
14%
Flag icon
for value in range(1,5):
14%
Flag icon
it doesn’t print the number 5:
14%
Flag icon
from 1 to 5, you
14%
Flag icon
use range(1,6):
14%
Flag icon
numbers = list(range(1,6))
14%
Flag icon
even_numbers = list(range(2,11,2))
14%
Flag icon
code. A list comprehension allows you to generate this same list in just one line of code.
14%
Flag icon
squares = [value**2 for value in range(1,11)]
14%
Flag icon
To make a slice, you specify the index of the first and last elements you want to work with.
14%
Flag icon
Python stops one item before the second index you specify.
14%
Flag icon
players[0:3])
15%
Flag icon
For example, if we want to output the last three players on the roster, we can use the slice players[-3:]:
15%
Flag icon
list, you can make a slice that includes the entire original list by omitting the first index and the second index ([:]).
15%
Flag icon
Tuples
15%
Flag icon
Python refers to values that cannot change as immutable, and an immutable list is called a tuple.
15%
Flag icon
A tuple looks just like a list except you use parentheses instead of square brackets.
15%
Flag icon
➊ dimensions = (200, 50)
15%
Flag icon
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.
16%
Flag icon
about tuples, which provide a degree of protection to a set of values that shouldn’t change,
16%
Flag icon
➊     if car == 'bmw':
17%
Flag icon
already in a list, use the keyword in.
17%
Flag icon
keyword not in this situation.
18%
Flag icon
Indentation plays the same role in if statements as it did in for loops.
18%
Flag icon
The if-elif-else Chain
18%
Flag icon
elif age < 18:
20%
Flag icon
allow you to connect pieces of related information.
20%
Flag icon
nest dictionaries
20%
Flag icon
alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) print(alien_0['points'])
20%
Flag icon
A dictionary in Python is a collection of key-value pairs.
20%
Flag icon
In Python, a dictionary is wrapped in braces, {},
20%
Flag icon
A key-value pair is a set of values associated with each other.
20%
Flag icon
can add new key-value pairs to a dictionary at any time.
21%
Flag icon
➊ for key, value in user_0.items(): ➋     print("\nKey: " + key) ➌     print("Value: " + value)
22%
Flag icon
Looping Through All the Keys in a Dictionary
22%
Flag icon
Looping through the keys is actually the default
22%
Flag icon
behavior
22%
Flag icon
for name in favorite_languages:
22%
Flag icon
for name in favorite_languages.keys():
22%
Flag icon
The keys() method isn’t just for looping: It actually returns a list of all the keys,
22%
Flag icon
Nesting
23%
Flag icon
'aeinstein': {            'first': 'albert',            'last': 'einstein',            'location': 'princeton',            },
24%
Flag icon
message = input("Tell me something, and I will repeat it back to you: ")
24%
Flag icon
name = input(prompt)
27%
Flag icon
➊ def greet_user():
27%
Flag icon
Default Values
28%
Flag icon
The return statement takes a value
28%
Flag icon
return person
29%
Flag icon
When you pass a list to a function, the function can modify the list.