Skip to content

Commit 0ec2700

Browse files
committed
init commit
1 parent a3ad43f commit 0ec2700

12 files changed

+613
-0
lines changed

22. understanding imports.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'''
2+
Now that we've used a module, statistics, it would be a good time to
3+
explain some import practices. As with many things in programming, there
4+
are many ways to import modules, but there are certainly some best
5+
practices.
6+
7+
So first, when you import a module, you are basically loading that
8+
module into memory. Think of a module like a script. Many if not most modules
9+
are just a single python script. So, when you go to import it, you use the file
10+
name. This can help keep code clean and easy to read. Many python developers
11+
just program everything in 1 script. Other developers, say from a language like
12+
java are going to be very used to doing lots of imports with a file for each
13+
type of job that's happening. Just like there are many ways to import, there
14+
are many more ways to program.
15+
16+
So let's talk about basic importing:
17+
'''
18+
19+
# here, we have imported the entire statistics module, and we can reference
20+
# any of the functions within it.
21+
# this loads that entire module, and will run any code that is
22+
# set to run in the module
23+
# remember if name == main? Again, that's what we use to stop code
24+
# from running when we just want to import.
25+
import statistics
26+
27+
example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]
28+
29+
# now, to use our import above, since we just imported statistics,
30+
# we must preceed any funciton wi thin statistics with a statistics.
31+
# so, for example, to access the mean() function within statistics:
32+
33+
print(statistics.mean(example_list))
34+
35+
36+
# and that's it. Let's look briefly at the statistics module and the mean
37+
# function...c:/python34/lib/statistics (not in site-packages)
38+
# take note of the package's location, we will talk more on that soon.
39+
# so now you see that statistics is just a python script, and mean is
40+
# just another function.
41+
42+
# sometimes, you will see people who import things "as". This
43+
# helps to keep typing to a minimum. For example
44+
45+
import statistics as s
46+
47+
# above we've imported statistics as s, so when we want to reference something
48+
# within statistics, we just need to do s. now.
49+
50+
print(s.mean(example_list))
51+
52+
# What if you don't even want to type that S though? Well there's an app for that!
53+
# you can just import all of the functions like so:
54+
55+
from statistics import mean
56+
57+
# so here, we've imported the mean function only.
58+
59+
print(mean(example_list))
60+
61+
# and again we can do as
62+
63+
from statistics import mean as m
64+
65+
print(m(example_list))
66+
67+
# what if we want to import other functions?
68+
69+
from statistics import mean, median
70+
71+
# here we imported 2 functions.
72+
73+
print(median(example_list))
74+
75+
# what if we want to use the as as well????
76+
77+
from statistics import mean as m, median as d
78+
79+
print(m(example_list))
80+
print(d(example_list))
81+
82+
83+
# What if we want to just import everything from statistics like we did initially
84+
# but we dont want to type the statistics because we have
85+
# fat fingers and this will just slow us down?.
86+
87+
from statistics import *
88+
89+
print(mean(example_list))

23. understanding imports 2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import examplemod
2+
3+
examplemod.ex('test')

23.-doc- examplemod.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def ex(data):
3+
print(data)

24. python lists vs tuples.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Lets compare python lists and tuples. They are often confused.
3+
4+
A python tuple is immutable, meaning you cannot change it. That said,
5+
less space is going to be required to generate a tuple, and you can use it
6+
for things where you don't want the tuple to change. To define a tuple, you
7+
either use round brackets, or no brackets. A popular use for this is multiple
8+
variable assignments from a function return.
9+
10+
For example, you might see something like:
11+
'''
12+
13+
def example():
14+
return 15, 12
15+
16+
x, y = example()
17+
print(x,y)
18+
19+
# in the above case, we have used a tuple and cannot modify it... and
20+
# we definitely do not want to!
21+
22+
23+
24+
'''
25+
A python list is mutable, and can contain just about any python data. to
26+
define a list, you use square brackets.
27+
28+
'''
29+
30+
x = [1,3,5,6,2,1,6]
31+
32+
'''
33+
You can then reference the whole list like:
34+
'''
35+
print(x)
36+
37+
# or a single element by giving its index value.
38+
# index values start at 0 and go up by 1 each time
39+
40+
print(x[0],x[1])
41+
42+
43+
44+
'''
45+
So again, a python tuple is a colleciton of data that is immutable.
46+
Generally the only time you will use this is when it is extremely
47+
important that the tuple is immutable.
48+
49+
Then you have a python list, which is mutable and highly malleable.
50+
51+
A tuple is defined by no containing brackets, or round brackets. A list
52+
is defined by square brackets.
53+
'''

25. python lists.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'''
2+
Since lists are mutable, this means that we will be using lists for
3+
things where we might intend to manipulate the list of data, so how
4+
can we do that? Turns out we can do all sorts of things.
5+
6+
We can add, remove, count, sort, search, and do quite a few other things
7+
to python lists.
8+
'''
9+
10+
# first we need an example list:
11+
12+
x = [1,6,3,2,6,1,2,6,7]
13+
14+
# lets add something.
15+
16+
# we can do .append, which will add something to the end of the list, like:
17+
18+
x.append(55)
19+
20+
print(x)
21+
22+
23+
# what if you have an exact place that you'd like to put something in a list?
24+
25+
x.insert(2,33)
26+
27+
print(x)
28+
29+
# so the reason that went in the 3rd place, again, is because we start
30+
# at the zero element, then go 1, 2.. .and so on.
31+
32+
# now we can remove things... .remove will remove the first instance
33+
# of the value in the list. If it doesn't exist, there will be an error:
34+
35+
x.remove(6)
36+
37+
print(x)
38+
39+
#next, remember how we can reference an item by index in a list? like:
40+
41+
print(x[5])
42+
43+
# well we can also search for this index, like so:
44+
45+
print(x.index(1))
46+
47+
# now here, we can see that it actually returned a 0, meaning the
48+
# first element was a 1... when we knew there was another with an index of 5.
49+
# so instead we might want to know before-hand how many examples there are.
50+
51+
print(x.count(1))
52+
53+
# so we see there are actually 2 of them
54+
55+
# we can also sort the list:
56+
x.sort()
57+
58+
print(x)
59+
60+
61+
# what if these were strings? like:
62+
63+
y = ['Jan','Dan','Bob','Alice','Jon','Jack']
64+
65+
y.sort()
66+
print(y)
67+
68+
# noooo problemo!
69+
70+
71+
# You can also just reverse a list, but, before we go there, we should note that
72+
# all of these manipulations are mutating the list. keep in mind that any
73+
# changes you make will modify the existing variable.
74+
75+
76+
77+
78+

26. python list dimension.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Lists that we have covered so far have all been 1 dimensional,
3+
but you can have lists within lists within lists within lists if you want.
4+
5+
Often times, if you want to have lists within lists, your superior choice
6+
will be something like dictionaries, which we'll get to, but
7+
there will be times when a multi dimensional list is the best choice.
8+
'''
9+
10+
x = [[2,6],[6,2],[8,2],[5,12]]
11+
12+
# so we already know how to reference elements in a list, we can do:
13+
14+
print(x[2])
15+
16+
# but we can also take this deeper since we have more dimensions now:
17+
18+
print(x[2][1])
19+
20+
# this can go on indefinitely with very thick lists.
21+
# you might see how this can quickly get messy, let's consider
22+
# how to properly display lists in code that have multiple dimensions.
23+
# you might not typically hard code multi dimensional lists,
24+
# but it's good to know how to do it:
25+
26+
27+
y = [[5,2],
28+
[6,2],
29+
[3,1],
30+
[12,6]
31+
]
32+
33+
# this is slightly cleaner, and python automatically understands it:
34+
35+
print(y)
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+

27. reading CSV files.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'''
2+
Next up, let us cover the reading of CSV files into memory.
3+
4+
One of the more popularly requested file types for reading into
5+
memory is csv files, so let us cover them.
6+
7+
here I will just create a simple example.csv file
8+
9+
10+
11+
12+
'''
13+
import csv
14+
15+
with open('example.csv') as csvfile:
16+
readCSV = csv.reader(csvfile, delimiter=',')
17+
for row in readCSV:
18+
print(row)
19+
print(row[0])
20+
print(row[0],row[1],row[2],)
21+
22+
23+
#### next part....
24+
25+
import csv
26+
27+
with open('example.csv') as csvfile:
28+
readCSV = csv.reader(csvfile, delimiter=',')
29+
dates = []
30+
colors = []
31+
for row in readCSV:
32+
color = row[3]
33+
date = row[0]
34+
35+
dates.append(date)
36+
colors.append(color)
37+
38+
print(dates)
39+
print(colors)
40+
41+
42+
#### now you want to maybe ask some questions with data...
43+
# what day was the color green let's ask?
44+
45+
import csv
46+
47+
with open('example.csv') as csvfile:
48+
readCSV = csv.reader(csvfile, delimiter=',')
49+
dates = []
50+
colors = []
51+
for row in readCSV:
52+
color = row[3]
53+
date = row[0]
54+
55+
dates.append(date)
56+
colors.append(color)
57+
58+
print(dates)
59+
print(colors)
60+
61+
# now, remember our lists?
62+
63+
whatColor = input('What color do you wish to know the date of?:')
64+
coldex = colors.index(whatColor)
65+
theDate = dates[coldex]
66+
print('The date of',whatColor,'is:',theDate)
67+
68+
69+
#but..................... what if we enter something that is non-existent?
70+
# oh noes, an error!
71+
72+
73+
74+
75+

27.-doc-example.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1/2/2014,5,8,red
2+
1/3/2014,5,2,green
3+
1/4/2014,9,1,blue

28. multi line print.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
So now I just briefly want to cover multi line print outs.
3+
I remember making beautiful print outs as sort of guis, but I would do
4+
print and then print out the line, then another print... and so on,
5+
but you can actually do multi line prints with just 1 print. So let's see:
6+
'''
7+
8+
9+
10+
print(
11+
'''
12+
This
13+
is
14+
a
15+
test
16+
'''
17+
)
18+
19+
print(
20+
'''
21+
So it works like a multi-line
22+
comment, but it will print out.
23+
24+
You can make kewl designs like this:
25+
26+
==============
27+
| |
28+
| |
29+
| BOX |
30+
| |
31+
| |
32+
==============
33+
'''
34+
)

0 commit comments

Comments
 (0)