-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNotes
111 lines (84 loc) · 3.62 KB
/
Notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
##Python Overview (10)##
* Flexible programming language that has become the standard for academic researchers.
* Packages versus language
* NLTK
* Pandas
* Numpy
* import
* import nltk - 'import' is a command, 'nltk' is a package
* from nltk import * - this says 'go to nltk and import everything' - save yourself and don't do this
* from nltk import concordance
OMG! What's the difference??
just importing the package, you have to call it every time, so nltk.concordance. Pulling in specific functions, you don't have to type nltk, but have to remember that that variable has already been used.
* It's a language with rules and vocabulary - multiple ways of writing things just like multiple ways of saying things.
* Note about Object Oriented
##Variables & Types (30)##
1. Variables - container for holding other things (notecards here)
* Rules about variables
* NO SPACES
* must be unique (unless you're overwriting something)
* cannot be a command Python already uses, or is already used by a package.
* Examples - let's do them together!
Open iPython Notebook
x = 8
y = 2
x + y [shift]+[enter]
z = x - y
print(z)
Let's try another
a = 3
b = 4
a = b --- what do you think happened here? Let's find out
print(a)
print(b)
Trivial for this example, but absolutely necessary for writing clear, understandable code
* Common usage
afile = open('/Users/michellejohnson/Documents/Spanish_English_BYTs.csv', 'r')
my_file = afile.read()
2. Types
a. Numbers
i. Integers
ii. Floats
b. Strings " "
usually words, always things Python can't "evaluate"
c. Lists [ ]
like a shopping list
d. Dictionaries { }
key:value pair - Keys are always unique - like your house key!
Values are not always unique... Let's say you are counting words, [(a:200), (the, 500), (at, 200)] would be fine.
Useful for counting instances of things
3. Type Examples
Going to go through a bunch of disconnected examples. The purpose here is to get acquainted with the way Python works. Can think of it like building a toolbox or a learning a new language. Tourists often learn a variety of stock phrases, but to actually communicate, you have to learn about how the pieces work.
a. Numbers covered in Variables
b. Strings
str1 = "Hello"
str2 = "GCDI DRI"
print(str1)
print (str1, str2)
str3 = str1 + str2
What happened? Why wasn't there a space between them?
Python can't evaluate inside of a string
How do we get a space??
srt1 + " " + str2
c. Lists
like a shopping list
groceries = ['apples', 'spinach', 'tofu']
print(groceries)
groceries.append('rice')
print(groceries)
You could set a new_variable here, like new_list = groceries.append(), but the original grocery list is still modified, so now you just have more variables and more confusion, so this is not ideal
Python counts the objects in a list. Python, however, is a mathematician, and begins counting at Zero, this takes a little getting used to, but comes natural very quickly
You can call an item in a list by telling Python which number item to pull out groceries[1] will return the second item in the list.
Lists are most often used for loops, which we will get to in the second half of this session
d. Dictionaries
key:value pair
Most often used for counting things
my_dict = {('steak', 2), ('potatoes', 5), ('cucumber', 6)}
my_dict.keys()
my_dict.values()
my_dict.has_key('tofu')
##Conditionals (35)##
#BREAK#
Loops (30)
Functions (25)
Create/Import Library (20)