0% found this document useful (0 votes)
24 views21 pages

Shivakumar IE6400 Lecture2 STUDENT Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views21 pages

Shivakumar IE6400 Lecture2 STUDENT Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

IE6400 Foundations for Data Analytics

Engineering

Fall 2024
-- STUDENT VERSION --

Basic of Python - Part 1

Variables

Exercise 1 Creating Variables and Output Variables

In [1]: # --- Added the code here ---


x=1
# ---------------------------

print(x)

In [2]: # --- Added the code here ---


y='NEU'
# ---------------------------

print(y)

NEU
In [4]: x = "University"

# --- Added the code here ---


print("Northeastern "+ x)
# ---------------------------

Northeastern University

In [5]: x = 5
y = 10
z=x+y
# --- Added the code here ---
print(z)
# ---------------------------

15

In [7]: x = "Northeastern "


y = "University"

# --- Added the code here ---


z= x + y
# ---------------------------

print(z)

Northeastern University

In [8]: x = "Data"
y = "Analytics"
z = "Engineering"

# --- Added the code here ---


print(x,y,z)
# ---------------------------

Data Analytics Engineering

In [9]: x = 5
y = "John"

# --- Added the code here ---


print(x,y)
# ---------------------------

5 John

Exercise 2 Casting

In [10]: # --- Added the code here ---


x = (1)
y = (2.8)
z = ("3")
# ---------------------------

print(x)
print(y)
print(z)
1
2.8
3

In [11]: # --- Added the code here ---


x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
# ---------------------------

print(x)
print(y)
print(z)
print(w)

1.0
2.8
3.0
4.2

In [13]: # --- Added the code here ---


x = str("S1")
y = str(2)
z = str(3.0)
# ---------------------------

print(x)
print(y)
print(z)

S1
2
3.0

In [14]: # --- Added the code here ---


x = (3)
y = (3)
z = (3)
# ---------------------------

print(x)
print(y)
print(z)

3
3
3

Exercise 3 Getting the Type

In [15]: x = 1 # int
y = 2.8 # float
z = 1j # complex
name = "Northeastern University"

print(type(x))
print(type(y))
print(type(z))
print(type(name))
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>

In [16]: x = 1
y = 35656222554887711
z = -3255522

# --- Added the code here ---


print(type(x))
print(type(y))
print(type(z))
# ---------------------------

<class 'int'>
<class 'int'>
<class 'int'>

In [17]: x = 1.10
y = 1.0
z = -35.59

# --- Added the code here ---


print(type(x))
print(type(y))
print(type(z))
# ---------------------------

<class 'float'>
<class 'float'>
<class 'float'>

In [18]: x = 35e3
y = 12E4
z = -87.7e100

# --- Added the code here ---


print(type(x))
print(type(y))
print(type(z))
# ---------------------------

<class 'float'>
<class 'float'>
<class 'float'>

In [19]: x = 3+5j
y = 5j
z = -5j

# --- Added the code here ---


print(type(x))
print(type(y))
print(type(z))
# ---------------------------

<class 'complex'>
<class 'complex'>
<class 'complex'>
Exercise 4 Single or Double Quotes?

In [20]: x = "John"
print(x)
# double quotes are the same as single quotes:
x = 'John'
print(x)

John
John

Exercise 5 Case-Sensitive

In [21]: # --- Added the code here ---


a=4
A="Sally"
# ---------------------------

print(a)
print(A)

4
Sally

Exercise 6 Many Values to Multiple Variables

In [22]: # --- Added the code here ---


x,y,z="Orange", "Banana", "Cherry"
# ---------------------------

print(x)
print(y)
print(z)

Orange
Banana
Cherry

Exercise 7 One Value to Multiple Variables

In [23]: # --- Added the code here ---


x=y=z="Orange"
# ---------------------------

print(x)
print(y)
print(z)

Orange
Orange
Orange

Exercise 8 Unpack a Collection

In [24]: fruits = ["apple", "banana", "cherry"]

# --- Added the code here ---


x,y,z=fruits
# ---------------------------

print(fruits)
print(x)
print(y)
print(z)

['apple', 'banana', 'cherry']


apple
banana
cherry

Global Variables

Exercise 9 Create a variable outside of a function, and use it inside the


function

In [25]: x = "University"

def myfunc():
print("Northeastern " + x)

myfunc()

Northeastern University

In [27]: x = "University"

def myfunc():
# --- Added the code here ---
x = "college"
# ---------------------------
print("Northeastern " + x)

myfunc()

print("Northeastern " + x)

Northeastern college
Northeastern University

Exercise 10 The global Keyword

In [28]: def myfunc():


# --- Added the code here ---
global x
# ---------------------------
x = "University"

myfunc()

print("Northeastern " + x)

Northeastern University

In [29]: x = "College"

def myfunc():
global x
x = "University"

myfunc()

print("Northeastern " + x)

Northeastern University

Python Strings

Exercise 11 Multiline Strings

In [30]: # --- Added the code here ---


a = """DAE Program,
Northeastern University,
Boston Campus
USA."""
# ---------------------------

print(a)

DAE Program,
Northeastern University,
Boston Campus
USA.

In [32]: # --- Added the code here ---


a = '''DAE Program,
Northeastern University,
Boston Campus
USA.'''
# ---------------------------

print(a)

DAE Program,
Northeastern University,
Boston Campus
USA.

Exercise 12 Strings are Arrays

In [33]: a = "Northeastern University"

# --- Added the code here ---


print(a[13])
# ---------------------------

Exercise 13 Looping Through a String

In [34]: for x in "NORTHEASTERN":


print(x)
N
O
R
T
H
E
A
S
T
E
R
N

Exercise 14 String Length

In [35]: a = "NORTHEASTERN"

# --- Added the code here ---


print(len(a))
# ---------------------------

12

Exercise 15 Check String

In [36]: txt = "Data Analytics Engineering"

# --- Added the code here ---


print("Engineering" in txt)
# ---------------------------

True

In [38]: txt = "Data Analytics Engineering"

# --- Added the code here ---


if "Engineering" in txt:
print("Yes, 'Engineering' is present.")
# ---------------------------

Yes, 'Engineering' is present.

Exercise 16 Check if NOT

In [40]: txt = "Data Analytics Engineering"

# --- Added the code here ---


print("NEU" not in txt)
# ---------------------------

True

In [41]: txt = "Data Analytics Engineering"

# --- Added the code here ---


if "NEU" not in txt:
print("No, 'NEU' is NOT present.")
# ---------------------------
No, 'NEU' is NOT present.

Exercise 17 Slicing Strings

In [43]: b = "Hello, World!"

# --- Added the code here ---


print(b[7:12])
# ---------------------------

World

Exercise 18 Slice From the Start

In [44]: b = "Hello, World!"

# --- Added the code here ---


print(b[:5])
# ---------------------------

Hello

Exercise 19 Slice To the End

In [45]: b = "Hello, World!"

# --- Added the code here ---


print(b[7:])
# ---------------------------

World!

Exercise 20 Negative Indexing

In [46]: b = "DataAnalyticsEngineering"

# --- Added the code here ---


print(b[-20:-11])
# ---------------------------

Analytics

Exercise 21 Modify Strings

Upper Case

In [47]: a = "Northeastern University"

# --- Added the code here ---


print(a.upper())
# ---------------------------

NORTHEASTERN UNIVERSITY

Lower Case

In [48]: a = "NORTHEASTERN UNIVERSITY"

# --- Added the code here ---


print(a.lower())
# ---------------------------

northeastern university

Remove Whitespace

In [49]: a = " Northeastern University "


print(a)

# --- Added the code here ---


print(a.strip())
# ---------------------------

Northeastern University
Northeastern University

Replace String

In [50]: a = "Northeastern University"

# --- Added the code here ---


print((a.replace("University", "College")))
# ---------------------------

Northeastern College

Split String

In [51]: a = "DAE,Northeastern University"

# --- Added the code here ---


print(a.split(","))
# ---------------------------

['DAE', 'Northeastern University']

Exercise 22 String Concatenation

In [52]: a = "Northeastern"
b = "University"
c= a+b
# --- Added the code here ---
#
# ---------------------------

print(c)
NortheasternUniversity

In [54]: a = "Northeastern"
b = "University"

# --- Added the code here ---


c = a +" "+ b
# ---------------------------

print(c)

Northeastern University

Exercise 23 String Format

In [55]: age = 2025


txt = "We are the DAE student, and we are the class of {}"

# --- Added the code here ---


print(txt.format(age))
# ---------------------------

We are the DAE student, and we are the class of 2025

In [58]: quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."

# --- Added the code here ---


print(myorder.format(quantity,itemno,price))
# ---------------------------

I want 3 pieces of item 567 for 49.95 dollars.

In [59]: quantity = 3
itemno = 567
price = 49.95

# --- Added the code here ---


myorder = "I want to pay {} dollars for {} pieces of item {}."
# ---------------------------

print(myorder.format(price,quantity,itemno))

I want to pay 49.95 dollars for 3 pieces of item 567.

Exercise 24 Escape Character

In [60]: # --- Added the code here ---


txt = "We are the so-called at Northeastern University."
# ---------------------------

print(txt)

We are the so-called at Northeastern University.

Python Booleans
Exercise 25 Boolean Values

In [61]: 1==2

False
Out[61]:

In [62]: x="aBC"
y="ABC"
x==y

False
Out[62]:

In [63]: # Createing X variable where x = 2


x=2

In [64]: x<1 and x>3

False
Out[64]:

In [65]: x<1 and x==3

False
Out[65]:

In [66]: x<1 or x==2

True
Out[66]:

In [67]: x=1
y=2
x is y

False
Out[67]:

In [68]: x="abcd"
y="abc"
x in y

False
Out[68]:

Python Operators

Python Arithmetic Operators


Exercise 26 Arithmetic Operators

In [69]: 1+1

2
Out[69]:

In [70]: 2**3

8
Out[70]:

In [71]: 2*3

6
Out[71]:

Python Assignment Operators

Exercise 27 Assignment Operators

In [72]: x = 5
print(x)

In [73]: x += 3 # x = 5 + 3
print(x)

In [74]: x /= 8 # x = 8 / 8
print(x)

1.0

Python Comparison Operators


Exercise 28 Comparison Operators

In [75]: 1 == 2

False
Out[75]:

In [76]: 1 != 2

True
Out[76]:

In [77]: 1 < 2

True
Out[77]:

In [78]: x = "aBC"
y = "ABC"
x == y

False
Out[78]:

Python Lists

Exercise 29 Creating a list

In [79]: thislist = [100,200,300,400,500,600,700,800,900]


print(thislist)

[100, 200, 300, 400, 500, 600, 700, 800, 900]

In [80]: thislist = ["apple", "banana", "cherry"]


print(thislist)

['apple', 'banana', 'cherry']


In [81]: thislist = ["apple", "banana", "cherry"]
type(thislist)

list
Out[81]:

In [82]: thislist = [1,2,3]


type(thislist)

list
Out[82]:

Exercise 30 Allow Duplicates

In [83]: thislist = ["apple", "banana", "cherry", "apple", "cherry"]


print(thislist)

['apple', 'banana', 'cherry', 'apple', 'cherry']

Exercise 31 List Length

In [84]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


print(len(thislist))
# ---------------------------

Exercise 32 List Items - Data Types

In [85]: list1 = ["apple", "banana", "cherry"]


print(list1)

['apple', 'banana', 'cherry']

In [86]: list2 = [1, 5, 7, 9, 3]


print(list2)

[1, 5, 7, 9, 3]

In [87]: list3 = [True, False, False]


print(list3)

[True, False, False]

In [88]: list4 = ["abc", 34, True, 40, "male"]


print(list4)

['abc', 34, True, 40, 'male']

Exercise 33 Access List Items

In [89]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


print(thislist[1])
# ---------------------------
banana

Negative Indexing:

In [90]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


print(thislist[-2])
# ---------------------------

banana

Range of Indexes

In [91]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

# --- Added the code here ---


print(thislist[2:5])
# ---------------------------

['cherry', 'orange', 'kiwi']

In [92]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

# --- Added the code here ---


print(thislist[:5])
# ---------------------------

['apple', 'banana', 'cherry', 'orange', 'kiwi']

In [93]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

# --- Added the code here ---


print(thislist[2:])
# ---------------------------

['cherry', 'orange', 'kiwi', 'melon', 'mango']

Range of Negative Indexes

In [94]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]

# --- Added the code here ---


print(thislist[-4:-1])
# ---------------------------

['orange', 'kiwi', 'melon']

Exercise 34 Check if Item Exists

In [95]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
# ---------------------------

Yes, 'apple' is in the fruits list


Exercise 35 Change Item Value

In [96]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist[1] = "blackcurrant"
# ---------------------------

print(thislist)

['apple', 'blackcurrant', 'cherry']

Change a Range of Item Values

In [97]: thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]

# --- Added the code here ---


thislist[1:3] = ["blackcurrant", "watermelon"]
# ---------------------------

print(thislist)

['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']

In [98]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist[1:2] = ["blackcurrant", "watermelon"]
# ---------------------------

print(thislist)

['apple', 'blackcurrant', 'watermelon', 'cherry']

In [99]: thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist[1:3] = ["watermelon"]
# ---------------------------

print(thislist)

['apple', 'watermelon']

Exercise 36 Insert Items

In [101… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist.insert(2,'watermelon')
# ---------------------------

print(thislist)

['apple', 'banana', 'watermelon', 'cherry']

Exercise 37 Extend List


In [102… thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]

# --- Added the code here ---


thislist.extend(tropical)
# ---------------------------

print(thislist)

['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']

Exercise 38 Remove Specified Item

In [103… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist.remove("banana")
# ---------------------------

print(thislist)

['apple', 'cherry']

In [104… thislist = ["apple", "banana", "cherry", "banana", "kiwi"]

# --- Added the code here ---


thislist.remove("apple")
# ---------------------------

print(thislist)

['banana', 'cherry', 'banana', 'kiwi']

Exercise 39 Remove Specified Index

In [105… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist.pop(1)
# ---------------------------

print(thislist)

['apple', 'cherry']

In [106… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist.pop()
# ---------------------------

print(thislist)

['apple', 'banana']

Exercise 40 Remove the list

In [107… thislist = ["apple", "banana", "cherry"]


# --- Added the code here ---
del thislist
# ---------------------------

In [ ]: print(thislist)

Exercise 41 Clear the List

In [109… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


thislist.clear()
# ---------------------------

print(thislist)

[]

Exercise 42 Sort Lists

In [111… thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]

# --- Added the code here ---


thislist.sort()
# ---------------------------

print(thislist)

['banana', 'kiwi', 'mango', 'orange', 'pineapple']

In [112… thislist = [100, 50, 65, 82, 23]


thislist.sort()
print(thislist)

[23, 50, 65, 82, 100]

Sort Descending

In [114… thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]

# --- Added the code here ---


thislist.sort(reverse=True)
# ---------------------------

print(thislist)

['pineapple', 'orange', 'mango', 'kiwi', 'banana']

In [115… thislist = [100, 50, 65, 82, 23]


thislist.sort(reverse = True)
print(thislist)

[100, 82, 65, 50, 23]

Exercise 43 Reverse Order

In [116… thislist = ["banana", "Orange", "Kiwi", "cherry"]


# --- Added the code here ---
thislist.reverse()
# ---------------------------

print(thislist)

['cherry', 'Kiwi', 'Orange', 'banana']

Exercise 44 Copy a List

In [117… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


mylist = thislist.copy()
# ---------------------------

print(thislist)
print(mylist)

['apple', 'banana', 'cherry']


['apple', 'banana', 'cherry']

In [118… thislist = ["apple", "banana", "cherry"]

# --- Added the code here ---


mylist = list(thislist)
# ---------------------------

print(thislist)
print(mylist)

['apple', 'banana', 'cherry']


['apple', 'banana', 'cherry']

Exercise 45 Join Lists

In [119… list1 = ["a", "b", "c"]


list2 = [1, 2, 3]

# --- Added the code here ---


list3 = list1+list2
# ---------------------------

print(list3)

['a', 'b', 'c', 1, 2, 3]

In [120… list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

# --- Added the code here ---


for x in list2:
list1.append(x)
# ---------------------------

print(list1)

['a', 'b', 'c', 1, 2, 3]


In [121… list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

# --- Added the code here ---


list1.extend(list2)
# ---------------------------

print(list1)

['a', 'b', 'c', 1, 2, 3]

Revised Date: July 18, 2024

You might also like