Last active
April 3, 2017 14:55
-
-
Save ferrerluis/6628f86a4629c0cefe988d07f1202a99 to your computer and use it in GitHub Desktop.
Files for the Python Bootcamp by PH in Spring 2017
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Monster: | |
| agility = 20 | |
| def __init__(self, health, defense): # => Constructor | |
| self.health = health | |
| self.defense = defense | |
| def say(self): # => Instance Method | |
| print('Rawr') | |
| bob = Monster(30, 10) | |
| ricky = Monster(10, 30) | |
| bob.health = 50 | |
| bob.say() | |
| Monster.say(bob) | |
| print(bob.health, ricky.health) | |
| # a = {'Luis': 20, 'Braxton': 19} | |
| # | |
| # print(a['Luis']) | |
| # a = [['Luis', 20], ['Braxton', 19]] | |
| # | |
| # for person in a: # => DO NOT DO THIS | |
| # if person[0] == 'Luis': | |
| # print(person[1]) | |
| # a = {1, 2, 3, 2} | |
| # | |
| # print(a) | |
| # a = [1, 2, 3] | |
| # | |
| # a.remove(2) | |
| # | |
| # print(a) # => [1, 3] | |
| # a = (1, 2, 3, 4) | |
| # | |
| # print(a) | |
| # print(a.pop()) # => FAILS | |
| # a = [1, 2, 3, 4] | |
| # | |
| # a.insert(2, 'Hello') | |
| # | |
| # print(a) | |
| # a = [1, 2, 3, 4, 5] | |
| # | |
| # print(a[3]) | |
| # | |
| # b = a[1:4] | |
| # | |
| # print(b) | |
| # | |
| # c = a.pop() | |
| # | |
| # print(c) | |
| # | |
| # a.reverse() | |
| # | |
| # print(a) | |
| # | |
| # a.sort() | |
| # | |
| # print(a) | |
| # | |
| # a.append(5) | |
| # | |
| # print(a) | |
| # | |
| # a.insert(len(a)//2, 8) | |
| # | |
| # print(a) | |
| # f = 'Luis' | |
| # l = 20 | |
| # | |
| # a = '{}, {}'.format(l, f) # YOU CAN ALSO USE NUMBERS | |
| # | |
| # print(a) | |
| # f = 'Luis' | |
| # l = 'Ferrer' | |
| # | |
| # a = '{}, {}'.format(l, f) | |
| # b = l + ', ' + f # DO NOT USE | |
| # | |
| # print(a) | |
| # print(b) | |
| # for c in "Hey world": | |
| # print(c) | |
| # import math | |
| # | |
| # a = math.sqrt(4) # => 2 | |
| # b = math.ceil(4.00001) # => 5 | |
| # c = math.floor(4.99999) # => 4 | |
| # | |
| # print(a, b, c) | |
| # a = 2 + 2 | |
| # b = 2 - 2 | |
| # c = 5 // 2 # => 2 | |
| # d = 5 / 2 # => 2.5 | |
| # e = 4 ** 2 # => 16 | |
| # | |
| # print(a, b, c, d, e) | |
| # 345 # Number (int) | |
| # 23.5 # Number (float) | |
| # "Hey world" # String | |
| # [1, 2, 3] # List | |
| # (1, 2, 3) # Tuple | |
| # {'a': 1, 'b': 2} # Dictionary | |
| # 3/28/2017 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests as r | |
| class Pokemon: | |
| def __init__(self, name, abilities=[], stats={}): | |
| self.name = name # => String | |
| self.abilities = abilities # => List | |
| self.stats = stats # => Dictionary | |
| def pull(id): | |
| res = r.get( | |
| 'http://pokeapi.co/api/v2/pokemon/{}/'.format(id) | |
| ) | |
| data = res.json() | |
| name = data['name'].capitalize() | |
| # abilities = [] # => LONGER AND MORE ANNOYING | |
| # for ability_obj in data['abilities']: | |
| # ability_name = ability_obj['ability']['name'] | |
| # abilities.append(ability_name) | |
| abilities = [ability['ability']['name'] for ability in data['abilities']] | |
| stats = {stat['stat']['name']: stat['base_stat'] for stat in data['stats']} | |
| return Pokemon(name, abilities, stats) | |
| # a = [1,2,3,4] | |
| # | |
| # a[0] # => 1 | |
| # | |
| # b = {'a': 1, 'b': 2} | |
| # | |
| # b['a'] # => 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pokemon import Pokemon | |
| pok = Pokemon.pull(45) | |
| print(pok.name, pok.abilities, pok.stats) | |
| # from pokemon import Pokemon | |
| # | |
| # pikachu = Pokemon( | |
| # 'Pikachu', | |
| # ['thunderbolt', 'tailwhip'], | |
| # {'hp': 40} | |
| # ) | |
| # | |
| # print(pikachu.abilities, pikachu.stats) | |
| # import requests | |
| # | |
| # URL = 'http://wttr.in/Atlanta' | |
| # params = {'m': True} | |
| # | |
| # response = requests.get(URL, params=params) | |
| # | |
| # print(response.url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment