3 Datatypes
3 Datatypes
(AI)
Agenda
▰ Data Types
▰ List
▰ Tuple
▰ Dictionary
▰ Sets
▰ Byte and Bytearray
2
Data Types
3
Back to main
Data Types:
5
Accessing character of a String:
6
https://www.alphacodingskills.com/python/img/python-string.png
Example: Strings in Python using single
quotes
#String with single Quotes
String1 = 'Welcome to the AI Class'
print("String with the use of Single Quotes: ")
print(String1)
#String with double Quotes
String1 = "I'm a Python"
print("\nString with the use of Double Quotes: ")
print(String1)
# String with triple Quotes
String1 = '''I'm a Python and I live in a world of "AI"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
# String with triple Quotes allows multiple lines
String1 = '''AI
With
Python''’
print("\nCreating a multiline String: ") 7
print(String1)
Example: Accessing characters of String
8
String Slicing:
print(MyString[6:])
print(MyString[:-7],"\n")
print(MyString[:])
Output:Learn
Learn
Python
Learn
Learn Python 9
String Concatenation:
text_1 = 'Learn'
text_2 = 'Python'
MyString = text_1 + text_2
print(MyString)
Output: LearnPython
Learn Python
10
Numeric:
11
Integers: (int)
12
Four forms of Integers:
13
▰ By default the answer of these four forms are in decimal
forms, so to change that we need to convert them.So,
Here:
▰ For converting into binary we will do : bin(15) -.> 0b1111
▰ For converting into octal : oct(100) -> 0o144
▰ For converting into hexadecimal : hex (1000) -> 0x3e8
14
Float:
15
Complex Numbers:
16
https://i.ytimg.com/vi/QUGmwPwtbpg/hqdefault.jpg
Complex Numbers:
▰ The real part can be binary, octal, or hex anything, but the
image should be decimal only.
▰ OB111 + 20j it is correct, But 14+ 0B101 it is incorrect.
▰ We can also take float values as well x=10.5+20.1j.
▰ Addition or subtraction of the complex numbers is simple. We
simply do addition or subtraction of the real part of both
numbers and imaginary parts of both numbers.
17
Example: Python program to demonstrate
numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
18
Boolean:
Data type with one of the two built-in values, True or False.
Sometimes a boolean value is also represented as 0 (for
false) and 1 (for true).
True and False with capital ‘T’ and ‘F’ are valid booleans
otherwise python will throw an error.
Non-Boolean objects can be evaluated in Boolean context
as well and determined to be true or false.
19
Example: Python program to demonstrate
boolean type
x = bool(5)
#display x:
print(x)
Output:
True
<class 'bool'>
20
Back to main
Sequence Type - Lists
21
Back to main
Lists
22
Access List Items
❑ List items are indexed and you can access them by referring to
the index number:
Access Items Negative Indexing
23
Access List Items
Range of Indexes
By leaving out the start value, the range will start at the first item:
By leaving out the end value, the range will go on to the end of the list:
24
Change List Items
25
Change List Items
If you insert more items than you replace, the new items will be inserted where you specified,
and the remaining items will move accordingly:
If you insert less items than you replace, the new items will be inserted where you
specified, and the remaining items will move accordingly:
26
Add List Items
27
Add List Items
Extend List
28
Remove List Items
29
Sort Lists
30
Sort Lists
Sort Descending
31
Sort Lists
Reverse Order
32
Copy Lists
Copy a List
33
Join Lists
34
Basic List Operations
35
Built-in List Functions & Methods
36
Built-in List Functions & Methods
38
Back to main
Tuples
Output: ()
39
Back to main
Access Tuple Items:
Range of Indexes
40
Access Tuple Items:
41
Update Tuples
42
Update Tuples
Remove Items
43
Join Tuples
44
Basic Tuples Operations
45
Built-in Tuple Functions
47
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) [GCC
4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help",
"copyright", "credits" or "license" for more information.
>>> range(10)
range(0, 10)
>>> type(range(10))
<class 'range'>
range (n)
Gives values to 0 to n-1
range(begin : end)
Gives values from the beginning to the n-1
Range (begin, end, increment/Decrement)
R = range(10)
R= range(1, 10)
R = range(1, 21,2 )
R= range (20, 1, -2)
49
Back to main
Mapping Type: Dictionary
dic = { }
#we can add values to empty dictionary like:
dic[key1] = value1
dic[key2] = value2
51
# Creating an empty Dictionary
Dict = {}
print(Dict)
55
Back to main
Creating an empty set
Output: {1, 2, 3}
discard(): Removes the element from the set
{1.0, (1, 2, 3), 'Hello'}
remove(): Removes the element from the set
pop(): Returns and removes a random element from the set
57
Add Set Items
Add Sets
58
Remove Set Items
Remove Item
59
Combining Sets
60
The intersection and difference
# random dictionary
person = {"name": "Abid", "age": 24,
"Gender": "male"}
fSet = frozenset(person)
print('The frozen set is:', fSet)
Output: The frozen set is: frozenset({'name',
'gender', 'age'})
62
Bytes and BytesArray
Here we have list li= [10, 20,30,40], to convert it into the bytes we need
to do is b=bytes(li),Now it converted into binary data for creation.
Bytes can only have values from 0 to 256. So basically, if we add the
value 256, 257 … We will get the value error.
Now in this as well indexing, slicing is there,But it is immutable (we
cannot change its content) We will get an error. But if we want mutable
bytes then it is known as Bytearray
63
Back to main
None Data Type:
a=None
Print (type(a))
64