Professional Documents
Culture Documents
Day 2 Final MND New
Day 2 Final MND New
Dr.M.Nirmala Devi,
Assistant Professor,
Department of Computer Science and Engineering,
Thiagarajar College of Engineering,
Madurai, Tamil Nadu
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 1
Summary Of Session 1
• Syllabus
• Installation of Python
• Basics of Python
• Features
• Data Types
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 2
Session 2 -Outline
• Variables Naming
• Set and Frozen set
• Memoryview
• Bytes
• Tuple
• Dictionary
• Getting input from user
• input()
• raw_input()
• ASCII values
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 3
Data types
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: Dict
Set Types: set, frozenset
Boolean Type: Bool
Binary Types: bytes, bytearray, memoryview
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 4
Data types
Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 5
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", frozenset
"banana", "cherry"})
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 6
Naming rules for variables
#Legal variable names: #Illegal variable names:
myvar = "John" 2myvar = "John"
my_var = "John" my-var = "John"
_my_var = "John" my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 7
Difference between Set and Frozen Set
>>> cities = set(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
>>> cities
{'Freiburg', 'Basel', 'Frankfurt', 'Strasbourg'}
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 8
Predict the Output
x = set(("apple", "banana", "cherry", "banana"))
print(x)
x = set(("cherry"))
print(x)
print(type(x))
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 9
Output
{'apple', 'cherry', 'banana'}
{'e', 'h', 'r', 'c', 'y'}
<class 'set'>
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 10
Check the Program [memory view]
x = memory view(bytes(5))
print(x)
print(type(x))
[Indentation Error]
Output
<memory at 0x7fe770004640>
<class 'memoryview'>
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 11
Predict the Output
x= 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
print(x)
print(y)
print(z)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 12
Output
<class 'int'>
<class 'int'>
<class 'int'>
1
35656222554887711
-3255522
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 13
Write the code for this output
<class 'int'>
<class 'int'>
<class 'int'>
1 35656222554887711 -3255522
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 14
Solution
x= 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
print(x,"\t" ,y,"\t" ,z)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 15
Use of Bytes
x = bytes(5)
print(x)
x = bytes([5])
print(x)
print(type(x))
Output
b'\x00\x00\x00\x00\x00'
b'\x05'
<class 'bytes'>
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 16
Tuple –Create and Access
Ordered, Immutable and indexed
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print(tup1[0])
print(tup2[1:5])
physics
(2, 3, 4, 5)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 17
Dictionary
-Unordered, changeable[mutable] and indexed
thisdict = { thisdict = {
"brand": "Ford", "brand": "Ford",
"model": "Mustang", "model": "Mustang",
"year": 1964 "year": 1964
} }
print(thisdict) thisdict["year"] = 2018
x = thisdict["model"] print(thisdict)
x = thisdict.get("model")
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 18
Runtime -Input from the User
input ( prompt )
raw_input ( prompt )
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 19
Input()-Python 3.X
val1 = input("Enter the name: ") Input
print(type(val1)) nila
print(val1) 1983
val2 = input("Enter the number: ") Output
print(type(val2)) Enter the name: <class 'str'>
print(val2) nila
Enter the number: <class 'str'>
1983
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 20
raw_input()-Python 2.X
val1 =raw_input("Enter the name: ") Input
print(type(val1)) nila
print(val1) 1983
val2 =raw_input("Enter the number: ") Output
print(type(val2)) Enter the name: <class 'str'>
print(val2) nila
Enter the number: <class 'str'>
1983
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 21
Solution for Int Type
val1 = raw_input("Enter the name: ")
print(type(val1))
print(val1)
val2 = raw_input("Enter the number: ")
print(type(val2))
val2 = int(val2)
print(type(val2))
print(val2)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 22
Find the code
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 23
x=1;y='hi';z=2.3
a=input('x')
b=x+int(a);c=y+a;d=z+float(a)
print(b,c,d)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 24
Newline
x=1;y='hi';z=2.3
a=input('x')
b=x+int(a);c=y+a;d=z+float(a)
print(b,'\n',c ,'\n', d)
x13
hi12
14.3
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 25
ASCII in HEX,OCT,BIN and DEC values
https://www.sciencebuddies.org/science-fair-
projects/references/ascii-table
x="\u0123" print(x) y=ord("t")
print(x) x="\0o123" print(y)
x="\x28" print(x) z=chr(118)
print(x) x="045" print(z)
x="\0x123" print(x)
print(x) x="\b0000001"
x="\123" print(x)
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 26
LAMBDA
x = lambda a: a + 10
print(x(5)) li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
Output: final_list = list(map(lambda x: x*2 , li))
15 print(final_list)
Output:
x = lambda a, b : a * b
print(x(5, 6)) [10, 14, 44, 194, 108, 124, 154, 46, 146,
Output: 122]
30
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 27
Thank you
https://forms.gle/kA1f5JEjb5vzfti58
20-08-2020 Dr.M.NirmalaDevi,AP,CSE,TCE 28