Skip to content

Commit 3651134

Browse files
committed
Add Day 1 material and files
Original material from https://github.com/stevebtang/python-classes
1 parent 87bd652 commit 3651134

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

Day1/DeckCards.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# each card must have a
2+
# suit (spade, diamond, heart, club)
3+
# value A, J, Q, K, 2 - 10
4+
5+
class Card:
6+
7+
def __init__(self, suit, value):
8+
#YOUR CODE HERE
9+
10+
def getSuit(self):
11+
#YOUR CODE HERE
12+
13+
def getValue(self):
14+
#YOUR CODE HERE
15+
16+
# each card must have a
17+
# suit (spade, diamond, heart, club)
18+
# value A, J, Q, K, 2 - 10
19+
20+
class Deck:
21+
deck = []
22+
dealtCards = []
23+
24+
def __init__(self, suit, value):
25+
# Create deck of cards
26+
# Each deck will have 52 Cards
27+
# 13 Hearts (A, J, K, Q, 2 - 10)
28+
# 13 Spade (A, J, K, Q, 2 - 10)
29+
# 13 Club (A, J, K, Q, 2 - 10)
30+
# 13 Diamonds (A, J, K, Q, 2 - 10)
31+
32+
#Your Code here
33+
self.deck.append(Card("Heart", "J"))
34+
35+
def dealCard(self):
36+
# deal a random card
37+
# once dealt, a card cannot be dealt again
38+
39+
def howManyCardRemain(self):
40+
# return count of undealt cards
41+

Day1/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
##Installation
2+
3+
If you have Python and pygame already installed on your computer, you should be good to go! Otherwise, the easiest way to get started is to go to http://helloworldbookblog.com/tools/ and download the installer for your operating system. This will include everything you need. We will be using Python version 2.X.
4+
5+
If there are installation issues, http://repl.it/ can be used for the first week. It cannot be used for the second week though, because graphics and pygame are required.
6+
7+
## Review of Basic Python
8+
9+
### Functions and Arguments
10+
11+
Open Python IDLE, which should open a console window. Open a new Python window (File -> New Window).
12+
13+
Write a function called `greet_name` that will print the name that we input to the screen. After writing the function, save the python file (name it something that ends with `.py`), then hit `F5` to run the code in the console window.
14+
15+
(Hint: it starts with `def print_name`)
16+
17+
Test it out in the console after the program runs:
18+
19+
>>> print_name("Yun")
20+
Hello, Yun
21+
22+
Now, let's make a new function that takes both a name and an age
23+
24+
>>> greet_name_age("Yun", 28)
25+
Hello, Yun. You are 28 years old.
26+
27+
For an implementation of `print_name` and `greet_name_age`, check in the answers directory.
28+
29+
In all these cases, the values between the parenthesis are called "arguments". Functions can have many arguments as you need.
30+
31+
### Return Values
32+
33+
You can return values from functions
34+
35+
def multiply(a,b):
36+
return a * b
37+
38+
Functions do work and "hide" details from other code. The technical term is "abstraction".
39+
40+
## Classes
41+
42+
A class can be thought of as a blueprint that you can use to create many copies of something.
43+
44+
Each "copy" of the class can have their own set of variables (called properties), however, each copy shares the same set of actions it can do (called methods)
45+
46+
### Example
47+
48+
Open a new file python file and type in:
49+
50+
class Student:
51+
age = 0
52+
name = ""
53+
54+
Run it. What happens?
55+
56+
Nothing happens. This is because you have only created the definition of the class. We'll need to instantiate an object. In other words, we'll need to create a student out of the blueprint for a student.
57+
58+
bobby = Student()
59+
60+
`bobby` is now created, but with no name and no age. Let's give him a name and an age
61+
62+
bobby.name = "Bobby"
63+
bobby.age = 10
64+
65+
Run the program again. What happens?
66+
67+
Nothing happens again. But why is this?
68+
69+
Bobby is now created, but we haven't told him to do anything yet. Let's give him some actions. In your student class, type the following in:
70+
71+
def say_name(self):
72+
print "my name is " + self.name
73+
74+
def say_age(self):
75+
print "my age is " + str(self.age)
76+
77+
Run the file again. In the console, create bobby again and do this:
78+
79+
bobby = Student()
80+
bobby.age = 10
81+
bobby.name = "Bobby"
82+
83+
bobby.say_name()
84+
bobby.say_age()
85+
86+
What happens?
87+
88+
### Exercise
89+
90+
1. Give each student a favorite color property. Then write a method that will have the student tell us his favorite color.
91+
92+
2. Write a class method that will have each Student say his name and age and favorite color.
93+
94+
3. Create a few more students: `jane` and `betty`. Give them ages, names, and favorite colors.
95+
96+
4. Bonus: Give each student a best friend property (hint: use classes!). Write a method to return his/her best friend's name
97+
98+
## Constructors
99+
100+
In the last section, we were creating students without names and ages. We then gave names and ages to the students later. It doesn't make any sense to make Students with names and ages, so let's do the folowwing:
101+
102+
def __init__(self, name, age):
103+
self.name = name
104+
self.age = age
105+
106+
Try to run your program again, and then use the same console command as before:
107+
108+
bobby = Student()
109+
110+
What happens?
111+
112+
You get an error. Since we have an constructor now, you can no longer create a Student without providing enough information to fill in the constructor arguments. Try this:
113+
114+
bobby = Student("bobby", 10)
115+
116+
It should work.
117+
118+
The constructors in Python have the name `__init__`, and have an extra argument you do not need to provide when you instantiate an object - in this case: `self`.
119+
120+
## Lab - Deck of Cards
121+
122+
You will be implementing a deck of cards. Open up the file `DeckCards.py` in this directory inside IDLE.
123+
124+
1. Complete the card class. Each card has a suit (hearts, diamonds, spades, clubs) and a value (A, J, K, Q, 2-10)
125+
126+
2. Complete the deck class. Each deck will have 52 cards - 13 Hearts, 13 Spades, 13 Clubs, 13 Diamonds, each suit with cards A, J, K, Q, 2 to 10.
127+
128+
3. Complete the `dealCards` and `remainingCards` methods. See the code for details.
129+
130+
4. Now write a program that uses the Deck class to deal random cards.
131+
132+
5. Try to implement some simple card games using your Card and Deck classes.

Day1/answers/Student.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Student:
3+
age = 0
4+
name = ""
5+
6+
def __init__(self, name, age):
7+
self.name = name
8+
self.age = age
9+
10+
def say_name(self):
11+
print "my name is " + self.name
12+
13+
def say_age(self):
14+
print "my age is " + str(self.age)
15+
16+
17+
bobby = Student("bobby", 10)
18+
19+
bobby.say_name()
20+
bobby.say_age()

0 commit comments

Comments
 (0)