3/1/23, 10:42 AM Basic data types in python.
ipynb - Colaboratory
Experiment 1
Object - Study of basic data types in Python
Software required - Google Colab
Python supports 3 categories of data types:
1. Basic types - int, float, complex, bool, string, bytes
2. Container types - list, tuple, set, dict
3. User-defined types - class
There is no need to define type of a variable. During execution the type of the variable is inferred from the context in which it is being used.
Hence Python is called dynamically-typed language.
Type of particular data can be checked using a function called type().
#Integer data type
a = 123
print(a)
print(type(a))
123
<class 'int'>
#Multiple variable assignment
am = nm = km = lm = 5
#float data type
b = 3.141528E3
print(b)
print(type(b))
3141.528
<class 'float'>
#string data type
c = 'koush'
print(c)
print(type(c))
koush
<class 'str'>
Boolean data type
In programming we often need to know if an expression is True or False.
We can evaluate any expression in Python, and get one of two answers, True or False.
print(10 > 9)
True
print(10 == 9)
False
Python also has many built-in functions that return a boolean value, like the isinstance() function, which can be used to determine if an object is
of a certain data type:
utx = 200
print(isinstance(utx, int))
True
#Print a message based on whether the condition is True or False:
amsg = 200
bmsg = 33
if bmsg > amsg:
print("bmsg is greater than amsg")
https://colab.research.google.com/drive/1xxdw-Mme_JiDMLu_zZPQWcIClO1Nz7mW#scrollTo=TSD-vpOkP-Jx&printMode=true 1/5
3/1/23, 10:42 AM Basic data types in python.ipynb - Colaboratory
else:
bmsg is not greater than amsg
print("bmsg is not greater than amsg")
Complex numbers are written with a "j" as the imaginary part
Note: You cannot convert complex numbers into another number type.
E = 3+4j
print(E)
print(type(E))
(3+4j)
<class 'complex'>
Random Numbers -
Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make
random numbers:
#Example- Import the random module, and display a random number between 1 and 9:
import random
print(random.randrange(1, 10))
Arithmetic Operations -
ab = 3 ** 2
print(ab)
print(type(ab))
9
<class 'int'>
ac = 7//3
print(ac)
print(type(ac))
2
<class 'int'>
qw = 7%2 #% yields remainder 1
qw
ad = 3
ad**=4
print(ad)
81
ae= 3
ae+=4
print(ae)
af=12
af%=10
print(af)
z = 1 + 4.33
print(z)
print(type(z))
5.33
<class 'float'>
y = 1 + 2 + 4j
print(y)
https://colab.research.google.com/drive/1xxdw-Mme_JiDMLu_zZPQWcIClO1Nz7mW#scrollTo=TSD-vpOkP-Jx&printMode=true 2/5
3/1/23, 10:42 AM Basic data types in python.ipynb - Colaboratory
print(type(y))
(3+4j)
<class 'complex'>
ya = 1.33 + 2 + 4j
print(ya)
print(type(ya))
(3.33+4j)
<class 'complex'>
Built - in functions
Python has many built-in functions that are always available in any part of the program. The print() function that we have been using to send
output to screen is a built-in function.
print(min(10,20,30,40))
10
print(round(2.567))
print(round(2.5678,2))
2.57
print(hex(26)) #returns hexadecimal equivalent
0x1a
print(oct(26)) #returns octal equivalent
0o32
Built in modules
Apart from built-in functions, Python provides many built-in modules. Each module contains many functions.
import math #importing math module
print(math.factorial(5))
120
Type Conversion - You can convert from one type to another with the int(), float(), and complex()methods:
abc = complex(3/1.5, 5/2.5)
print(abc)
(2+2j)
ax = float(3)
print(ax)
3.0
axz = int(3.33)
print(axz)
Strings are Arrays
Square brackets can be used to access elements of the string.
aj = "Hello, World!"
print(aj[1])
Looping Through a String
https://colab.research.google.com/drive/1xxdw-Mme_JiDMLu_zZPQWcIClO1Nz7mW#scrollTo=TSD-vpOkP-Jx&printMode=true 3/5
3/1/23, 10:42 AM Basic data types in python.ipynb - Colaboratory
Since strings are arrays, we can loop through the characters in a string, with a for loop.
for x in "GOD":
print(x)
G
O
D
#String Length
#To get the length of a string, use the len() function.
at = "Hello, World!"
print(len(at))
13
#To check if a certain phrase or character is present in a string, we can use the keyword in.
txt = "My name is Koush Rastogi"
print("Koush" in txt)
True
Slicing a string
bt = "Hello, World!"
print(bt[2:5])
llo
print(bt[:5])
Hello
print(bt[2:])
llo, World!
#Negative slicing
print(b[-5:-2])
orl
#String modification
#Python has a set of built-in methods to modify strings.
#The upper() method returns the string in upper case:
print(bt.upper())
HELLO, WORLD!
print(bt.lower())
hello, world!
#strip() method removes any whitespace from the beginning or the end:
btc = " Hello, World! "
print(btc.strip())
Hello, World!
#The replace() method replaces a string with another string:
print(bt.replace("H", "J"))
Jello, World!
#String Concatenation
#To concatenate, or combine, two strings you can use the + operator.
kab = 'Hello'
kbk = 'World'
knc = kab + kbk
print(knc)
HelloWorld
#Use the format() method to insert numbers into strings:
https://colab.research.google.com/drive/1xxdw-Mme_JiDMLu_zZPQWcIClO1Nz7mW#scrollTo=TSD-vpOkP-Jx&printMode=true 4/5
3/1/23, 10:42 AM Basic data types in python.ipynb - Colaboratory
age = 32
txt = "My name is Koush, and I am {} years old"
print(txt.format(age))
My name is Koush, and I am 32 years old
Colab paid products - Cancel contracts here
check 0s completed at 10:39 AM
https://colab.research.google.com/drive/1xxdw-Mme_JiDMLu_zZPQWcIClO1Nz7mW#scrollTo=TSD-vpOkP-Jx&printMode=true 5/5