Shivakumar IE6400 Lecture4 STUDENT Part 3
Shivakumar IE6400 Lecture4 STUDENT Part 3
Engineering
Fall 2024
-- STUDENT VERSION --
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
In [76]: a = 33
b = 200
b is greater than a
In [77]: a = 33
b = 33
if b > a:
print("b is greater than a")
# --- Added the code here ---
elif a==b:
# ---------------------------
print("a and b are equal")
In [78]: a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
# --- Added the code here ---
else:
# ---------------------------
print("a is greater than b")
a is greater than b
In [79]: a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
In [80]: a = 200
b = 33
a is greater than b
In [81]: a = 2
b = 330
In [82]: a = 330
b = 330
In [84]: a = 200
b = 33
c = 500
OR operator
In [85]: a = 200
b = 33
c = 500
Not operator
In [86]: a = 33
b = 200
Exercise 7 Nested If
In [87]: x = 41
if x > 10:
print("Above ten,")
Above ten,
and also above 20!
if b > a:
# --- Added the code here ---
pass
# ---------------------------
In [89]: i = 1
# --- Added the code here ---
while i<6:
# ---------------------------
print(i)
i = i + 1
1
2
3
4
5
In [91]: i = 1
while i < 6:
print(i)
i = i + 1
1
2
3
In [92]: i = 0
while i < 6:
i += 1 # same as i = i + 1
print(i)
1
2
4
5
6
In [93]: i = 1
while i < 6:
print(i)
i += 1
1
2
3
4
5
i is no longer less than 6
apple
banana
cherry
Mango
b
a
n
a
n
a
apple
banana
print(x)
apple
print(x)
apple
cherry
0
1
2
3
4
5
2
5
8
11
14
17
20
23
26
29
0
1
2
3
4
5
Finally finished!
0
1
2
for x in adj:
# --- Added the code here ---
for y in fruits:
# ---------------------------
print(x, y)
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
Python Functions
In [2]: # No arguments
my_function()
Northeastern University
Exercise 22 Arguments
In [111… my_function('Boston')
my_function('Vancouver')
my_function('Seattle')
Multiple Arguments
Northeastern University
Northeastern University
Northeastern University
Northeastern Boston
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
I am from Sweden
I am from India
I am from Norway
I am from Brazil
my_function(fruits)
apple
banana
cherry
print(my_function(3))
print(my_function(5))
print(my_function(9))
15
25
45
add(1,2)
3
Out[7]:
In [8]: # Return a list
def vector(a,b):
y=list(range(a,(b+1)))
return y
z = vector(10,40)
print(z)
[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]
z=dictionary('NEU','Boston')
print(z)
myfunction()
Python Lambda
print(x(5))
print(x(7))
print(x(9))
15
17
19
print(x(5, 6))
print(x(7, 5))
print(x(3, 4))
30
35
12
In [13]: # --- Added the code here ---
x = lambda a, b, c : a+b+c
# ---------------------------
print(x(5, 6, 2))
print(x(1, 2, 3))
print(x(3, 3, 3))
13
6
9
Python NumPy
Installation of NumPy
Import NumPy
print(np.__version__)
1.21.5
print(arr)
print(type(arr))
[1 2 3 4 5]
<class 'numpy.ndarray'>
print(arr)
[1 2 3 4 5]
Dimensions in Arrays
0-D Arrays
arr = np.array(42)
print(arr)
42
1-D Arrays
print(arr)
[1 2 3 4 5]
2-D Arrays
print(arr)
[[1 2 3]
[4 5 6]]
3-D arrays
print(arr)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
0
1
2
3
print(arr)
print('Number of dimensions :',arr.ndim)
[[[[[1 2 3 4]]]]]
Number of dimensions : 5
print(arr)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
2nd element on 1st row: 2
print(arr)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
5th element on 2nd row: 10
In [ ]: import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr)
Negative Indexing
[2 3 4 5]
[5 6 7]
[1 2 3 4]
Negative Slicing
[5 6]
[7 8 9]
[[ 3 4 5]
[ 8 9 10]]
[[2 3 4]
[7 8 9]]
(2, 4)
print(arr)
print('shape of array :', arr.shape)
[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)
print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Reshape From 1-D to 3-D
print(newarr)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
print(newarr)
[1 2 3 4 5 6]
print(arr)
[1 2 3 4 5 6]
print(arr)
[[1 2 5 6]
[3 4 7 8]]
print(arr)
[[1 4]
[2 5]
[3 6]]
print(arr)
[1 2 3 4 5 6]
print(arr)
[[1 2 3]
[4 5 6]]
print(arr)
[[[1 4]
[2 5]
[3 6]]]
print(newarr)
print(newarr)
print(one_dim_array)
[0. 0. 0. 0.]
[[0. 0. 0.]
[0. 0. 0.]]
1 x N array
[[0. 0. 0. 0.]]
N x 1 array
[[0.]
[0.]
[0.]
[0.]]
[0 0 0]
[[0 0 0 0]
[0 0 0 0]]
print(one_dim_array)
[1. 1. 1. 1.]
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
array([1, 4, 7])
Out[68]: