-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
125 lines (102 loc) · 3.38 KB
/
hello.py
File metadata and controls
125 lines (102 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Ch 1 - 2
print('\n------ Ch 1 - 2 -------\n')
## F strings. Basically string interpolation.
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
message = f"Hello, {full_name.title()}!"
print(message)
print("{} {}".format(first_name, last_name).title())
## Whitespace, tabs, newlines
print("\tPython")
print("Languages:\nPython\nC\nJavaScript")
print("Languages:\n\tPython\n\tC\n\tJavaScript")
favorite_language = 'python'
print(favorite_language.rstrip())
print(favorite_language.strip())
print(favorite_language.lstrip())
# Ch 3 Lists
bicycles = ['trek', 'cannondale', 'redline']
print(bicycles[-2]) # returns second to last element
print(f'my first bicycle was a {bicycles[-1]}')
bicycles.append('schwinn') # adds an entry
del bicycles[1] # removes cannondale
# pop can also delete at any index. Use it when you
# want to return the popped value.
bicycles.remove('trek') # removes trek
print(bicycles)
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True) # mutates original array
print(cars)
print(sorted(cars)) # does not mutate original array
print(cars) # does not mutate original array
print(len(cars))
# Ch 4: Working With Lists
print('\n------ Ch 4 -------\n')
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f'{magician.title()}, that was a great trick!')
for number in range(1, 21):
print(number)
million_numbers = range(1, 1_000_001)
#for number in million_numbers:
# print(number)
print(sum(million_numbers))
odd_numbers = range(1, 21, 2)
for number in odd_numbers:
print(number)
for number in range(1, 11):
print(number**3)
print([value**3 for value in range(1, 11)])
# Working with part of a list
players = ['charles', 'martina', 'michael', 'florence', 'eli']
# Same as JS slice. Can omit the first and/or last index.
print(players[0:3])
print(players[-2:])
print('\n')
print('Here are the first three players on my team:')
for player in players[:3]:
print(player.title())
# Tuples - Immutable list
print('\nTuples')
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
print('\n')
requested_toppings = []
if requested_toppings:
for topping in requested_toppings:
print(f'Requested {topping}')
else:
print("Are you sure you want no toppings?")
# Dictionaries - Basically JS objects
print('\n')
alien_0 = {'color': 'green', 'job': 'alien', 'weapon': 'banana'}
print(alien_0['color'])
## Use get when the key might not exist
print(alien_0.get('height', 'no such value'))
for k, v in alien_0.items():
print(f"\nKey: {k}")
print(f"Value: {v}")
## Can wrap these dictionaries in methods like sorted or set.
for key in alien_0.keys():
print(f'{key}')
# Functions
## Passing an arbitrary number of arguments:
def make_pizza(*toppings): # Asterisk creates a tuple
""""Print the list of toppings that have been requested."""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
## Using arbitrary keyword arguments
## Double asterisk creates a dictionary
def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about a user"""
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)