-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_Intro_to_python.py
156 lines (113 loc) · 2.84 KB
/
1_Intro_to_python.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
146
147
148
149
150
151
152
153
154
155
156
# To add a new cell, type '#%%'
# To add a new markdown cell, type '#%% [markdown]'
#%%
from IPython import get_ipython
#%% [markdown]
#<h1> SIT 720 - Python Intro </h1>
#%% [markdown]
# <h2>Introduction to Python</h2>
#%%
dic = {}
dic['one'] = 'This is one'
dic[2] = 'This is two'
print(dic['one'])
print(dic)
dic['one'] = 'One has changed'
print(dic)
#%%
l = [1, 2, 3, 4, 5, 6, 7, 8]
s = "This is a string."
print(l[1:5])
print(s[0:6])
#%% [markdown]
# #### Branching and Decisioning
#%%
trip1 = 15
if trip1 <= 25:
print('25 and Under')
else:
print("25 and Over")
#%%
stat1 = True
stat2 = False
if stat1:
print("1st stat is true")
elif stat2:
print("2nd stat true")
else:
print("Both are false")
#%% [markdown]
# #### Iterations (Loops)
#%% [markdown]
# ##### For Loops
#%%
exampleList = [1, 2, 3, 4, 5]
for i in exampleList:
print(i)
#%%
#String with dynamic object
x=list(range(2,6))
print("Initial List: {}".format(x))
for idx, i in enumerate(x):
x[idx]= i**2
print("The new list:{}".format(x))
#During each step of the for loop, enumerate(x)iterates through the list
#and store the index in [idx] and value in [i].
#%%
newList = [x**2 for x in range (2, 6)]
print(newList)
#%% [markdown]
# ##### While Loops
#%%
i = 0
while i < 5:
print (i, end=" ") # prints each iteration on the same line
i += 1 # adds 1 to the variable i
print() # prints a blank line
print("done") # Note that this is printed outside the loop
#%%
y = range(1, 51)
for i in y:
if i%3==0 and i%2!=0:
print(i)
#%% [markdown]
# ##### Functions
#%%
def func1(s):
"Some stuff"
print("Number of characters in the string: ", len(s))
return 2*len(s)
#%%
func1("test function")
#%% [markdown]
# ###### Returning multiple values from a function
#%%
def powers(x):
xs = x**2
xc = x**3
xf = x**4
return xs, xc, xf
#%%
powers(5)
#%%
y1, y2, y3 = powers(5)
print(y2)
#%% [markdown]
# ##### Anonymous Functions
#%% [markdown]
# Anonymous functions are defined by the keyword lambda in Python. Functions f and g in the cell below basically do the same thing. But g is an anonymous function.
#%%
# define a function
def f(x):
return x**2. # x to the power of 2 - the function give us x squared
# use an anonymous function instead
g = lambda x: x**2. # x to the power of 2 - in other words x squared
print(f(8)) # call the f function and ask for the square of 8
print(g(8)) # call the g anonymous function and ask for the square of 8
#%% [markdown]
# In the cell below, we used anonymous function n_increment(). We create new functions by passing n to n_incremenet(). For example f5 and f9 are functions that add 5 and 9 to their inputs respectively.
#%%
def n_increment(n):
return lambda x: x+n
add5 = n_increment(5)
print(add5(2))