18/11/2023, 19:20 Data_Types.
ipynb - Colaboratory
Python Character Set
A character set is a set of valid characters acceptable by a programming language in scripting.
Letters:- A-Z, a-z
Digits:- 0 to 9
Special Symbols:- space + - / ( ) [ ] = ! = < > , ‘ “ $ # ; : ? &
White Spaces:- Blank Space , Horizontal Tab, Vertical tab, Carriage Return.
Other Characters:- Python can process all 256 ASCII and Unicode Characters
Tokens or Lexical Unit
Individual elements that are identified by programming language are called tokens or lexical unit.
Keyword/Reserved Words
In Python, some words are reserved to represent some meaning or functionality. They are called
reserved words. There are 33 reserved words available in Python.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 1/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
import keyword
keyword.kwlist
from google.colab import drive
drive.mount('/content/drive')
Identifiers
A name in a python program is called an identifier. It is a name given to a function, class, variable,
module or other objects that will be used in the python program.
Rules to define identifiers in Python
The only allowed characters are alphabets (uppercase/lowercase), digits(0-9), underscore
symbol (_)
No other special symbols are allowed ~!@#$%^&*()-
Identifier should not start with a digit. Eg. 123abc is not a valid identifier but abc123 is
allowed.
Identifiers are case sensitive. Eg. abc and ABC are two different identifiers.
Reserved words cannot be used as identifiers. Eg. if =10 is invalid.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 2/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
a = 10
abc123 = 5
abc =12
ABC = 14
_abc = 1
print(a)
print(abc123)
print(abc)
print(ABC)
print(_abc)
def = 1
123abc =3
ca$h = 4
Many Values to Multiple Variables
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
One Value to Multiple Variables
x = y = z = "Orange"
print(x)
print(y)
print(z)
x = 1
X='1'
print(type(x))
print(type(X))
<class 'int'>
<class 'str'>
Note
If an identifier starts with _ symbol then it indicates that it is private.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 3/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
If identifier starts with __ (double underscore symbols) then it indicates that it is a strongly
private identifier.
If an identifier starts and ends with __ two underscore symbols then the identifier is a
language defined special symbol.
Data Types
int data type
To represent whole numbers (integral values) int values can be represented into following ways:
1. Decimal Form : (0-9)
2. Binary Form : (0/1)
3. Octal Form : (Base-8)
4. Hexa decimal Form : (Base-16)
a = 10
b = 0b1010
c = 0o12
d = 0xa
e = -5
print(a)
print(b)
print(c)
print(d)
print(e)
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
x = 5
print(x + 1) # Addition
print(x - 1) # Subtraction
print(x * 2) # Multiplication
print(x ** 2) # Exponentiation
x += 1
print(x)
x *= 2
print(x)
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 4/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
float type
to represent floating point values (decimal values)
f = 1.23
print(f)
type(f)
y = 2.5
print(type(y))
print(y, y + 1, y * 2, y ** 2)
Complex data type
containing real and imaginary part . eg. x + yj
a = 3+5j
print(a)
b = 0B11+5j
print(b)
c = 3+0B11j
print(c)
Mathematical Functions
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 5/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
import math
# abs() returns absolute value and fabs() for absolute value of float
print(abs(-5))
print(math.fabs(23.4))
print("\n")
#ceil() returns the smallest integer not less than x
print(math.ceil(-100.12))
print(math.ceil(100.72))
print("\n")
#floor() returns the largest integer not greater than x
print(math.floor(-45.17))
print(math.floor(100.72))
print("\n")
#max() returns the value largest of its arguments
print(max(80, 100, 1000))
print(max(-80, -20, -10))
print("\n")
#min() returns the value smallest of its arguments
print(min(80, 100, 1000))
print(min(-80, -20, -10))
print("\n")
#pow() returns x to the power of y
print(math.pow(100, 2))
print(math.pow(4, 0.5))
print("\n")
#round() returns x rounded to n digits from the decimal point
print(round(80.2356, 2))
print(round(80.23456, 2))
print("\n")
#sqrt() returns the square root of x for x > 0
print(math.sqrt(100))
Boolean data type
To represent boolean values. True/False
t, f = True, False
print(type(t))
print(type(f))
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 6/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
str data type
A string is a sequence of characters enclosed within single quotes or double quotes.
s1 = "abc"
s2 = 'abc'
s3 = '''This is a
"character"'''
s4 = """This is a
character"""
print(s1)
print(s2)
print(s3)
print(s4)
hw12 = '{} {} {}'.format('hello', 'world', 12) # string formatting
print(hw12)
String Special Operators
# '+' to concatenate, or combine, two strings
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Hello World
# '*' to repeat the same string
a = "Hello "
print(a*2)
Hello Hello
Slicing of strings
slice means "a piece"
[] operator is called a slice operator, which can be used to retrieve parts of a string.
In python, strings follow zero based index.
+ve index means forward direction from Left to Right.
-ve index means backward direction from Right to Left.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 7/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
#Get the characters from position 'a' to position 'b' (not included)
a = "Hello, World!"
print(a[2:5])
#Get the characters from the start to position 'b' (not included)
print(a[:5])
#Get the characters from position 'a', and all the way to the end
print(a[2:])
#Use negative indexes to start the slice from the end of the string
print(a[-5:-2])
String Methods
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 8/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
#capitalize() capitalizes the first letter of the string
a = 'hello'
print(a.capitalize())
print("\n")
#count() returns the the number of occurrences of the substring from the given input string.
str12 = "This is a string."
substr ="is"
print(str12.count(substr, 3, 10))
print("\n")
#index() to return the index of the input string where the substring is found
str1 = "This is a string."
str2 = "is"
print(str1.index(str2))
print("\n")
#isalpha() to check whether the string consists of alphabets
a = "abc123"
b = "abc"
print(a.isalpha())
print(b.isalpha())
print("\n")
#isdigit() to check whether the string consists of digits
x = "1234"
print(x.isdigit())
print("\n")
#islower() to check whether the string contains lowercases
#isupper() to check whether the string contains uppercases
str1 = "hello"
print(str1.islower())
str2 ="Hello"
print(str1.isupper())
print("\n")
#len() to get the length of a string
print(len("hello"))
print("\n")
#lower() converts all characters into lowercased
#upper() converts all characters into uppercased
str1 = "HELLO"
str2 ="hello"
print(str1.lower())
print(str2.upper())
print("\n")
# strip() strips all the specified characters in the string
a = "77777 This is 777 a string.7777"
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 9/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
print(a.strip('7'))
print(a.lstrip('7'))
print(a.rstrip('7'))
Type Casting
To convert the value of one type to another type.
print(int(2.5))
print(int(True))
print(int(False))
print(int("10"))
print(bin(10))
print(oct(10))
print(hex(10))
print(int(0B1010))
print(int(3+5j))
print(int("ten"))
print(float(10))
print(float(True))
print(float(False))
print(float("10"))
print(float("10.5"))
print(float(3+5j))
print(float("ten"))
print(complex(10))
print(complex(True))
print(complex(False))
print(complex("10"))
print(complex("10.5"))
#To convert 'x' & 'y' into complex number
print(complex(10,-2))
print(complex(True, False))
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 10/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
print(bool(0))
print(bool(1))
print(bool(0.5))
print(bool(0.0))
print(bool(0+2j))
print(bool(0+0j))
print(bool("True"))
print(bool("False"))
print(bool(""))
print(bool(" "))
Lists
Lists are used to store multiple items in a single variable.
Lists are created using square brackets.
Heterogeneous objects are allowed.
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0].
Insertion order is preserved. If you add new items to a list, the new items will be placed at the
end of the list.
list1 = [10, 20.5, 10, "a", "b", True]
print(list1)
print(type(list1))
print(list1[0])
print(list1[-1])
print(list1[1:3])
#updating lists
list1[0]=100
print(list1)
#deleting list elements
del list1[2]
print(list1)
Basic List operations
print(len(list1)) #length
print(list1+list1) #concatenation
print(list1*2) #repetition
List Functions
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 11/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
list1 = [456, 700, 200]
print(max(list1)) #maximum
print(min(list1)) #minimum
list2 = ['hello','this', 'is', 'a', 'list']
print(max(list2))
print(min(list2))
List Methods
list2 = [10, 20, 10]
list3 =[True, 15.2, 'abc']
list2.append(30) #append
list2.append("hello")
print(list2)
list2.remove(20) #remove
print(list2)
print(list2.count(10)) #count the object occurences
list2.extend(list3) #extending the list
print(list2)
print(list2.index('hello')) #checking index of the object
list2.insert(2, 300) #insert object at a particular index
print(list2)
print(list2.pop()) #removes and returns last object from the list.
print(list2.pop(5))
print(list2)
list2.reverse() #reverse the list
print(list2)
Tuples
A tuple is a collection of objects which are ordered, immutable and allow duplicate items.
Tuples use parentheses, whereas lists use square brackets.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 12/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
tup1 = ('abc', 'xyz', 15, 20, True)
tup2 = (1, 2.3, 3.5, 5)
print(tup1[0])
print(tup2[1:3])
print(tup1[-1])
print(type(tup1))
tup1[2]=3
print(tup1)
del tup1[2]
print(tup1)
tup3 = (3, 5, 6.8, 'python')
print(tup3)
del tup3
print(tup3)
#len() returns the length of the tuple
print(len(tup1))
#concatenation
print(tup1+tup2)
#repetition
print(tup1*2)
#tuple() returns the tuple
a= ['hello', True, 2+3j, 15.8, 30]
t = tuple(a)
print(type(a))
print(type(t))
print(t)
Sets
Sets are used to store multiple items in a single variable.
A set is a collection which is unordered, immutable, and unindexed.
Set items are unchangeable, but you can remove items and add new items.
Set items can appear in a different order every time you use them, and cannot be referred to
by index or key.
Sets cannot have two items with the same value. The values True and 1 are considered the
same value in sets, and are treated as duplicates.
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 13/14
18/11/2023, 19:20 Data_Types.ipynb - Colaboratory
set1 = {'abc', 'hello', 12.5, 90, True, 1, 0}
set2 = {'xyz', 200, False}
print(set1)
print(type(set1))
set1.add('Python')
print(set1)
set1.update(set2)
print(set1)
https://colab.research.google.com/drive/1eoId68EsCfrbGvmA7HBrmxY0x7AuBo_w#printMode=true 14/14