forked from dansteingart/MAE-221-Fall-2015-Support-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_basics.py
151 lines (115 loc) · 3.88 KB
/
python_basics.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#Hello there.
#We're going to work together to learn python.
#Briefly, Python is an efficienct object oriented langauge that combines many of the best features of scripting langauges, mathematically oriented langauges (e.g. fortran and matlab), as well as 'proper modern' langauges like C++, and Java.
#Comments are defined with a '#' sign. Nothing to the right of a '#' in python is executed
#This is how we import libraries
from pithy import *
#Pithy is a helper library that abstracts many helper function. abstracting, roughly, means you don't have to worry about it (until something breaks).
#let's get started
print "<b>Variable Assignment Section</b>"
#this is how you set a variable
a = "apple"
b = 1
c = ['apple',3,'whatever']
#This is how you show a variable (look to the right -->)
print a
print b
print c
#throw them together
print a,b,c
print ""
#some math stuff
print "<b>Math Section</b>"
d = 7.0 #use decimals to indicate you would like to do math with a float
e = 9.0
print "d =",d
print "e =",e
print "d+e =",d+e
print "d-e =",d-e
print "d*e =",d*e
print "d/e =",d/e
print "d^2 =",d**2
print "e^2 =",e**2
print "PEMDAS COUNTS!"
print ""
#Notice how python doesn't seem to care what type of data you assigned to a variable. This is because python trusts you to know what you are doing and gives you plenty of rope to hang yourself. please don't hang yourself. For more about this go here: http://bit.ly/python-typing
#Here's where it can get tricky:
print "<b>Error Handling Section</b>"
try:
ab = a+b
print ab
except Exception as err:
print "<span style='color:red'>",err,"</span>"
print "why did that happen?"
#Notice what we did up there: we used try and except to try a risky function ("try:") and then handle ("except Exception as err:") error that risk function might have created. If we did not handle the error the programe would have stopped dead in its tracks.
#Notice, also, that I indented code above. python uses "whitespace" to delimit code. let's examine this with some common programming concepts
print ""
print "<b>Conditional Section</b>"
a = 5
b = 4
if a > b:
print "a is larger"
elif b<a:
print "b is larger"
else:
print "a and b are the same"
#if whitespace is not consistent, an error is thrown
print ""
print "<b>Iterators and Lists Section</b>"
#iteration in python is more similar to matlab than c. below we will use a range operator
#let's make a list of numbers from 1 to 9
a = range(1,10)
print 'a = ', a
#now, to interate through:
print "now let's count"
for i in a:
print i
print ""
#now, to interate through:
print "now be a bit tricky"
for i in a:
print str(i)+"^"+str(i)+" = ",i**i
#what did I just do there?
print ""
print "now for a casting error that python will pass to your frustration"
print ""
for i in a:
print str(i)+"/5 = ",i/5
print ""
print "wait, that's not right"
print ""
for i in a:
print str(i)+"/5 = ",i/float(5)
print "much better, what did I change?"
print "finally, a while conditional"
a = 0
while a < 10:
print a
a += 1
print "what happens if the condition is never met?"
print ""
print "<b>Function Defining Section</b>"
#to define a function
def myfunction(a,b):
return a+b
print myfunction(10,2)
print ""
print "<b>Array Math</b>"
print "we are using a library that allow matlab like functions, such as element-wise multipcation"
#make a list
a = range(0,100,10)
a = array(a) #turn the list to an array that we can treat like a vector
print "a = ",a
print "2*a = ", 2*a
print "a^2 =", a**2
print ""
print "<b>Plotting Section</b>"
#this is how we plot
plot(a,3*a**(.5),label="3*a^(1/2)") #plot a on the x-axis, sqrt(a) on the y-axis, and label "a^(1/2")
plot(a,2*a,label="2*a")
plot(a,a,label="a")
legend() #show a legend
ylabel("Y-Axis") #label your axes
xlabel("X-Axis") #label your axes
showme(kind="still") #show the plot
clf() #clear the data on the plot, IMPORTANT TO USE