|
| 1 | +# the len() function |
| 2 | +meat = 'Bacon' |
| 3 | +print('Bacon has ' + str(len(meat)) + ' characters') |
| 4 | +# Bacon has 5 characters |
| 5 | + |
| 6 | +veggie = 'Broccoli' |
| 7 | +print('Broccoli has ' + str(len(veggie)) + ' characters') |
| 8 | +# Broccoli has 8 characters |
| 9 | + |
| 10 | +tickers = ['lk', 'msft', 'bynd', 'crc'] |
| 11 | +print('There are ' + str(len(tickers)) + ' tickers in the list') |
| 12 | +# There are 4 tickers in the list |
| 13 | + |
| 14 | +for i in range(0, len(tickers)): |
| 15 | + print(tickers[i]) |
| 16 | +# lk |
| 17 | +# msft |
| 18 | +# bynd |
| 19 | +# crc |
| 20 | + |
| 21 | +listofints = [1, 2, 3, 4, 5, 6, 7] |
| 22 | +print(len(listofints)) |
| 23 | +# 7 |
| 24 | + |
| 25 | +tickerprices = {'lk': 45.50, 'msft': 165.70, 'crc': 8.25} |
| 26 | +print('There are ' + str(len(tickerprices)) + ' tickers in the dictionary') |
| 27 | +# There are 3 tickers in the dictionary |
| 28 | + |
| 29 | +mixedtypes = [tickers, listofints, tickerprices, 'Superbowl', True] |
| 30 | +print('There are ' + str(len(mixedtypes)) + ' items in the mixed list') |
| 31 | +# There are 5 items in the mixed list |
| 32 | + |
| 33 | +# range() and list() |
| 34 | +team_members = range(25) |
| 35 | +print(team_members) |
| 36 | +print(len(team_members)) |
| 37 | +# 25 |
| 38 | + |
| 39 | +print(list(team_members)) |
| 40 | +# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] |
| 41 | + |
| 42 | +for player in list(team_members): |
| 43 | + print('Player ' + str(player)) |
| 44 | +# Player 0 |
| 45 | +# Player 1 |
| 46 | +# Player 2 |
| 47 | +# Player 3 |
| 48 | +# Player 4 |
| 49 | +# Player 5 |
| 50 | +# Player 6 |
| 51 | +# Player 7 |
| 52 | +# Player 8 |
| 53 | +# Player 9 |
| 54 | +# Player 10 |
| 55 | +# Player 11 |
| 56 | +# Player 12 |
| 57 | +# Player 13 |
| 58 | +# Player 14 |
| 59 | +# Player 15 |
| 60 | +# Player 16 |
| 61 | +# Player 17 |
| 62 | +# Player 18 |
| 63 | +# Player 19 |
| 64 | +# Player 20 |
| 65 | +# Player 21 |
| 66 | +# Player 22 |
| 67 | +# Player 23 |
| 68 | +# Player 24 |
| 69 | + |
| 70 | +team_a = [] |
| 71 | +team_b = [] |
| 72 | +for player in team_members: |
| 73 | + if player % 2 == 0: |
| 74 | + team_a.append(list(team_members).pop(player)) |
| 75 | + else: |
| 76 | + team_b.append(list(team_members).pop(player)) |
| 77 | + |
| 78 | +for player in team_a: |
| 79 | + print('Player ' + str(player) + ' is on team A') |
| 80 | + |
| 81 | +for player in team_b: |
| 82 | + print('Player ' + str(player) + ' is on team B') |
| 83 | +# Player 0 is on team A |
| 84 | +# Player 2 is on team A |
| 85 | +# Player 4 is on team A |
| 86 | +# Player 6 is on team A |
| 87 | +# Player 8 is on team A |
| 88 | +# Player 10 is on team A |
| 89 | +# Player 12 is on team A |
| 90 | +# Player 14 is on team A |
| 91 | +# Player 16 is on team A |
| 92 | +# Player 18 is on team A |
| 93 | +# Player 20 is on team A |
| 94 | +# Player 22 is on team A |
| 95 | +# Player 24 is on team A |
| 96 | +# Player 1 is on team B |
| 97 | +# Player 3 is on team B |
| 98 | +# Player 5 is on team B |
| 99 | +# Player 7 is on team B |
| 100 | +# Player 9 is on team B |
| 101 | +# Player 11 is on team B |
| 102 | +# Player 13 is on team B |
| 103 | +# Player 15 is on team B |
| 104 | +# Player 17 is on team B |
| 105 | +# Player 19 is on team B |
| 106 | +# Player 21 is on team B |
| 107 | +# Player 23 is on team B |
| 108 | + |
| 109 | +# min() and max() |
| 110 | +print(max(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)) |
| 111 | +# 5 |
| 112 | +print(min(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)) |
| 113 | +# -5 |
| 114 | + |
| 115 | +jeans = 40 |
| 116 | +sweater = 50 |
| 117 | +shoes = 100 |
| 118 | +print(min(jeans, sweater, shoes)) |
| 119 | +# 40 |
| 120 | + |
| 121 | +print(max(jeans, sweater, shoes)) |
| 122 | +# 100 |
| 123 | + |
| 124 | +print(min('Kohls', 'Target')) |
| 125 | +# Kohls |
| 126 | + |
| 127 | +print(min('Bed Bath Beyond', 'Best Buy', 'Applebees')) |
| 128 | +# Applebees |
| 129 | + |
| 130 | +tshirt = 15 |
| 131 | +print(min(tshirt, sweater, jeans, shoes)) |
| 132 | +# 15 |
| 133 | + |
| 134 | +print(max(tshirt, sweater, jeans, shoes)) |
| 135 | +# 100 |
| 136 | + |
| 137 | +# round() abs() and pow() |
| 138 | +frappuccino = 4.72 |
| 139 | +print(round(frappuccino)) |
| 140 | +# 5 |
| 141 | + |
| 142 | +blueberrypie = 3.14159265359 |
| 143 | +print(round(blueberrypie, 4)) |
| 144 | +# 3.1416 |
| 145 | + |
| 146 | +intnum = -7 |
| 147 | +print('Absolute value of -7 is:', abs(intnum)) |
| 148 | +# Absolute value of -7 is: 7 |
| 149 | + |
| 150 | +floatnum = -2.75 |
| 151 | +print('Absolute value of -2.75 is:', abs(floatnum)) |
| 152 | +# Absolute value of -2.75 is: 2.75 |
| 153 | + |
| 154 | +plantroot = -2.5 |
| 155 | +print(abs(plantroot)) |
| 156 | +# 2.5 |
| 157 | + |
| 158 | +print(pow(2, 10)) |
| 159 | +# 1024 |
| 160 | + |
| 161 | +# sorted() |
| 162 | +randomnums = [12, -54, 32, 15, -7, 44] |
| 163 | +sortednums = sorted(randomnums) |
| 164 | +print(sortednums) |
| 165 | +# [-54, -7, 12, 15, 32, 44] |
| 166 | + |
| 167 | +reversednums = sorted(randomnums, reverse=True) |
| 168 | +print(reversednums) |
| 169 | +# [44, 32, 15, 12, -7, -54] |
| 170 | + |
| 171 | +stores = ['Kohls', 'Target', 'Best Buy', 'Walmart', 'Costco'] |
| 172 | +print(sorted(stores)) |
| 173 | +# ['Best Buy', 'Costco', 'Kohls', 'Target', 'Walmart'] |
| 174 | + |
| 175 | +print(sorted(stores, reverse=True)) |
| 176 | +# ['Walmart', 'Target', 'Kohls', 'Costco', 'Best Buy'] |
| 177 | + |
| 178 | +stock_prices = {'Apple': 318.38, 'Google': 1487.64, 'Microsoft': 165.27, 'Cisco': 49.06} |
| 179 | +for key in sorted(stock_prices.keys()): |
| 180 | + print(key, stock_prices[key]) |
| 181 | +# Apple 318.38 |
| 182 | +# Cisco 49.06 |
| 183 | +# Google 1487.64 |
| 184 | +# Microsoft 165.27 |
| 185 | + |
| 186 | +for key, value in sorted(stock_prices.items(), key=lambda item: item[1]): |
| 187 | + print(key, value) |
| 188 | +# Cisco 49.06 |
| 189 | +# Microsoft 165.27 |
| 190 | +# Apple 318.38 |
| 191 | +# Google 1487.64 |
| 192 | + |
| 193 | +for key in sorted(stock_prices.keys(), reverse=True): |
| 194 | + print(key, stock_prices[key]) |
| 195 | +# Microsoft 165.27 |
| 196 | +# Google 1487.64 |
| 197 | +# Cisco 49.06 |
| 198 | +# Apple 318.38 |
| 199 | + |
| 200 | +for key, value in sorted(stock_prices.items(), key=lambda item: item[1], reverse=True): |
| 201 | + print(key, value) |
| 202 | +# Google 1487.64 |
| 203 | +# Apple 318.38 |
| 204 | +# Microsoft 165.27 |
| 205 | +# Cisco 49.06 |
| 206 | + |
| 207 | +shirts = [('Blue', 'XL', 25), ('Red', 'L', 15), ('Green', 'S', 10), ('Yellow', 'M', 20)] |
| 208 | + |
| 209 | +print(sorted(shirts, key=lambda item: item[0])) |
| 210 | +# [('Blue', 'XL', 25), ('Green', 'S', 10), ('Red', 'L', 15), ('Yellow', 'M', 20)] |
| 211 | + |
| 212 | +print(sorted(shirts, key=lambda item: item[1])) |
| 213 | +# [('Red', 'L', 15), ('Yellow', 'M', 20), ('Green', 'S', 10), ('Blue', 'XL', 25)] |
| 214 | + |
| 215 | +print(sorted(shirts, key=lambda item: item[2])) |
| 216 | +# [('Green', 'S', 10), ('Red', 'L', 15), ('Yellow', 'M', 20), ('Blue', 'XL', 25)] |
| 217 | + |
| 218 | + |
| 219 | +# type() and isinstance() |
| 220 | +r = range(0, 20) |
| 221 | +print(type(r)) |
| 222 | +# <class 'range'> |
| 223 | + |
| 224 | +print(type(7)) |
| 225 | +# <class 'int'> |
| 226 | + |
| 227 | +print(type('Z')) |
| 228 | +# <class 'str'> |
| 229 | + |
| 230 | +print(type('A simple string')) |
| 231 | + |
| 232 | + |
| 233 | +# <class 'str'> |
| 234 | + |
| 235 | +class Car: |
| 236 | + def __init__(self, make, model, color): |
| 237 | + self.make = make |
| 238 | + self.model = model |
| 239 | + self.color = color |
| 240 | + |
| 241 | + |
| 242 | +class Truck(Car): |
| 243 | + def fourwheeldrive(self): |
| 244 | + print('four wheel drive engaged') |
| 245 | + |
| 246 | + |
| 247 | +car = Car('Honda', 'Civic', 'Blue') |
| 248 | +print(type(car)) |
| 249 | +# <class '__main__.Car'> |
| 250 | + |
| 251 | +tesla = Car('Tesla', 'Model 3', 'White') |
| 252 | +print(type(tesla)) |
| 253 | +# <class '__main__.Car'> |
| 254 | + |
| 255 | +truck = Truck('Toyota', 'Tacoma', 'Red') |
| 256 | +print(type(truck)) |
| 257 | +# <class '__main__.Truck'> |
| 258 | + |
| 259 | +print(type(car) == type(truck)) |
| 260 | +# False |
| 261 | +print(type(car) == type(tesla)) |
| 262 | +# True |
| 263 | +print(isinstance(car, Car)) |
| 264 | +# True |
| 265 | +print(isinstance(truck, Car)) |
| 266 | +# True |
0 commit comments