ORM & Modeling

ORMs Once upon a time, if you needed to interact with a database, you had to write SQL. But now […]

Read More
Comments Off on ORM & Modeling

APPENDIX

class Monster: hit_points = 1 sound = ‘roar’ … def battlecry(self): return self.sound.upper() >>> from monster import Monster >>> jubjub […]

Read More
Comments Off on APPENDIX

APPENDIX: Dicts & Tuples

# Dictionary >>> my_dict = {‘name’: ‘Kenneth’, ‘job’: ‘Teacher’} # Keys >>> my_dict[‘job’] ‘Teacher’ # Dictionaries as keys >>> name_dict […]

Read More
Comments Off on APPENDIX: Dicts & Tuples

APPENDIX: Lists

a_list.append( [4, 5] ) my_list = list( range(10) ) my_list + [10, 11, 12] our_list.extend( range(5,11) ) alpha.insert(1,’b’) new_list = […]

Read More
Comments Off on APPENDIX: Lists

Hack-n-Slash [Game]

monster.py import random from combat import Combat COLORS = [‘yellow’, ‘red’, ‘blue’, ‘green’] class Monster(Combat): min_hit_points = 1 max_hit_points = […]

Read More
Comments Off on Hack-n-Slash [Game]

Overriding Inheritance

You can override an inherited method in a class by simply re-defining it in the subclass. #[combat.py]: import random class […]

Read More
Comments Off on Overriding Inheritance

Instance Methods

#[character.py] class Character: experience = 0 hit_points = 10 def get_weapon(self): weapon_choice = input(“Weapon ([S]word, [A]xe, [B]ow: “).lower() if weapon_choice […]

Read More
Comments Off on Instance Methods

__str__( )

It is a special method that is called whenever something is converted into a string. >>> from monster import Dragon >>> […]

Read More
Comments Off on __str__( )

Inheritance

Add New Attributes Set foreign values of corresponding keys as new attributes in the sent self instance, with the setattr( […]

Read More
Comments Off on Inheritance

Objects

Class A class is a collection of attributes and methods that can be instantiated. class Monster: hit_points = 1 color […]

Read More
Comments Off on Objects

Dungeon Game

Random Choice >>> my_list = list(range(50)) >>> my_list [0,1,2,3,4, … , 49] >>> import random >>> random.choice(my_list) 15 >>> random.choice(my_list) […]

Read More
Comments Off on Dungeon Game

Tuple Iteration

enumerate( iterable ) Printing List via for-loop >>> my_alphabet_list = list(‘abcdefghijklmnopqrstuvwxyz’) >>> count = 0 >>> for letter in my_alphabet_list: …   […]

Read More
Comments Off on Tuple Iteration

Tuples Basics

Introduction Tuples are immutable lists. >>> my_tuple = (1,2,3) >>> my_tuple (1,2,3) Parentheses are not what makes a tuple a […]

Read More
Comments Off on Tuples Basics

Teachers Stats

teachers_dict = {‘Jason Seifer’: [‘Ruby Foundations’, ‘Ruby on Rails Forms’, ‘Technology Foundations’], ‘Kenneth Love’: [‘Python Basics’, ‘Python Collections’]} def most_classes(teachers_dict): […]

Read More
Comments Off on Teachers Stats

Dictionary Unpacking & Iteration

Unpacking Considering dictionaries, the format( ) function can be used several ways. Regular Way >>> my_string = “Hi! My name […]

Read More
Comments Off on Dictionary Unpacking & Iteration

Dictionary Basics

Introduction >>> my_dict = {‘name’: ‘Kenneth’} >>> my_dict {‘name’: ‘Kenneth’} Key Order of keys can change over time. This is why […]

Read More
Comments Off on Dictionary Basics

Slice with Steps

[Start:Stop:Step] >>> my_list = list(range(20)) >>> my_list [0,1,2,3, … , 19] >>> my_list[::2] [0,2,4,6,8, … ,18] >>> my_list[2::2] [2,4,6,8, … […]

Read More
Comments Off on Slice with Steps

Slices

Slicing Strings >>> my_string = “Hello there” >>> my_string[1:5] ‘ello’ >>> my_string[2] ‘l’ Slicing Lists >>> my_list = list(range(1,6)) >>> my_list […]

Read More
Comments Off on Slices

Pop

pop( index ) >>> names = [“Bashar”, “Ahmad”, “Mustafa”] >>> name_1 = names.pop() >>> name_1 ‘Mustafa’ >>> names [“Bashar”, “Ahmad”] […]

Read More
Comments Off on Pop

Removing List Items

Item Index >>> a_list = list(‘abzcd’) >>> a_list.index(‘z’) 2 Delete by index List Items Deletion >>> del a_list[2] >>> a_list [‘a’, […]

Read More
Comments Off on Removing List Items