Shivakumar IE6400 Lecture2 STUDENT Part 1
Shivakumar IE6400 Lecture2 STUDENT Part 1
Engineering
Fall 2024
-- STUDENT VERSION --
Variables
print(x)
print(y)
NEU
In [4]: x = "University"
Northeastern University
In [5]: x = 5
y = 10
z=x+y
# --- Added the code here ---
print(z)
# ---------------------------
15
print(z)
Northeastern University
In [8]: x = "Data"
y = "Analytics"
z = "Engineering"
In [9]: x = 5
y = "John"
5 John
Exercise 2 Casting
print(x)
print(y)
print(z)
1
2.8
3
print(x)
print(y)
print(z)
print(w)
1.0
2.8
3.0
4.2
print(x)
print(y)
print(z)
S1
2
3.0
print(x)
print(y)
print(z)
3
3
3
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
<class 'int'>
<class 'int'>
<class 'int'>
In [17]: x = 1.10
y = 1.0
z = -35.59
<class 'float'>
<class 'float'>
<class 'float'>
In [18]: x = 35e3
y = 12E4
z = -87.7e100
<class 'float'>
<class 'float'>
<class 'float'>
In [19]: x = 3+5j
y = 5j
z = -5j
<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
print(a)
print(A)
4
Sally
print(x)
print(y)
print(z)
Orange
Banana
Cherry
print(x)
print(y)
print(z)
Orange
Orange
Orange
print(fruits)
print(x)
print(y)
print(z)
Global Variables
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
myfunc()
print("Northeastern " + x)
Northeastern University
In [29]: x = "College"
def myfunc():
global x
x = "University"
myfunc()
print("Northeastern " + x)
Northeastern University
Python Strings
print(a)
DAE Program,
Northeastern University,
Boston Campus
USA.
print(a)
DAE Program,
Northeastern University,
Boston Campus
USA.
In [35]: a = "NORTHEASTERN"
12
True
True
World
Hello
World!
In [46]: b = "DataAnalyticsEngineering"
Analytics
Upper Case
NORTHEASTERN UNIVERSITY
Lower Case
northeastern university
Remove Whitespace
Northeastern University
Northeastern University
Replace String
Northeastern College
Split String
In [52]: a = "Northeastern"
b = "University"
c= a+b
# --- Added the code here ---
#
# ---------------------------
print(c)
NortheasternUniversity
In [54]: a = "Northeastern"
b = "University"
print(c)
Northeastern University
In [58]: quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
In [59]: quantity = 3
itemno = 567
price = 49.95
print(myorder.format(price,quantity,itemno))
print(txt)
Python Booleans
Exercise 25 Boolean Values
In [61]: 1==2
False
Out[61]:
In [62]: x="aBC"
y="ABC"
x==y
False
Out[62]:
False
Out[64]:
False
Out[65]:
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
In [69]: 1+1
2
Out[69]:
In [70]: 2**3
8
Out[70]:
In [71]: 2*3
6
Out[71]:
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
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
list
Out[81]:
list
Out[82]:
[1, 5, 7, 9, 3]
Negative Indexing:
banana
Range of Indexes
print(thislist)
print(thislist)
print(thislist)
print(thislist)
['apple', 'watermelon']
print(thislist)
print(thislist)
print(thislist)
['apple', 'cherry']
print(thislist)
print(thislist)
['apple', 'cherry']
print(thislist)
['apple', 'banana']
In [ ]: print(thislist)
print(thislist)
[]
print(thislist)
Sort Descending
print(thislist)
print(thislist)
print(thislist)
print(mylist)
print(thislist)
print(mylist)
print(list3)
print(list1)
print(list1)