DATA TYPES
TYPE OF DATA IS CALLED DATA
TYPES
TYPES OF DATA TYPES
14, COMPLEX : a + bj
1, INTEGERS : the whole number that can
have positive or negative numbers
2, FLOAT : decimal numbers
3,BOOLEAN : true or false
4, BYTE : range -128 or +127 , 8 bits
Eg:
Data = b ‘123’
Print(data)
Print(type(dATa)
5, BYTE ARRAY : more than one byte
Eg Data = bytearray( b‘ ’)
Print(data)
Print(type(dATa)
6, RANGE : generating a sequence of
numbers
Eg,
Data = list(range(10)
Print(data)
Print(type(dATa)
7, NONE : nothing
8, LIST : ordered sequence of data
REPERSENTED AS “[]”
Eg ,
Data = [1,2,3,4,5]
Print(data)
Print(type(dATa)
9, TUPLE : REPERSENTED AS “()”
Eg,
Data = (1,2,3,4,5)
Print(data)
Print(type(dATa)
10, SET : REPERSENTED AS “{}”
Eg,
Data = {1,2,3,4,5}
Print(data)
Print(type(dATa)
11, FROZEN SET : ADVANCE VERSION OF
SET
Eg ,
Data = {1,2,3,4,5}
Modified_data = frozenset(data)
Print(modified_data)
Print(type(modified_dATa)
12, DICTIONARY : REPERSENTED AS
“{KEY:VALUE}”
Eg,
Data = {‘apple’ : 250,’orange’ : 125
Print(data)
Print(type(dATa)
13, STRING: “ABC “ , ‘abcd’
Mutable : changeable
List
Set
dictionary
Immutable : cannot change
tuple
strings
int
float
Boolean
Input and output operations
Intput() : taking Data from the user
Output() : print the output by using print()
function
End=’\n’ : next line
End=’’: same line
How to format a string
In 2 ways
1st way
Eg,
Day = 18
Month = 2
Year = 2023
Print(“day = {} month = {} year =
{}”.format(day, month,year))
2nd way
Day = 18
Month = 2
Year = 2023
Print(f“day = {day} month = {month} year
= {year}”)
1., WRITE A PROGRAM TO PRINT HELLO
WORLD
2., WRITE A PROGRAM TO ADD TWO
NUMBERS
3., WRITE A PROGRAM TO SWAP TWO
NUMBERS
4., WRITE PROGRAM TO PRINT OUTPUT
WITHOUT A NEW LINE
END = ‘ ‘
5., WRITE A PYTHON TO GIVE AN EXAMPLE
TO EXPLAIN STRING FORMATTING
6., WRITE A PROGRAM TO MAKE A SIMPLE
CALCULATOR
7., WRITE A PROGRAM TO CALCULATE THE
AREA OF A TRIANGLE
Keywords in python
33 keywords are there in python
We Want to print all the keywords
Import keyword
Print(keyword.kwlist)
It has functionality and meaning
It cannot be used as identifiers or
variables
Eval : converting the data into original data
type
Operators : these are symbols used to perform
operation on operands
Arithematic operator :
+ , - , * , / , // , % ,**
Eg:
A = 10
B = 20
Print(a + b)
Print(a - b)
Print(a / b)
Print(a * b)
Print(a % b)
Print(a ** b)
Comparision operator :
> , >= , < , <= , == , !=
Eg:
a = 10
b = 20
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
print(a == b)
print(a != b)
Logical operator :
And , or , not
Eg : #logical and
print(True and False)
print(True and True)
print(False and True)
print(False and False)
#logical or
print(True or False)
print(True or True)
print(False or True)
print(False or False)
#logical not
print(not False)
print(not True)
Membership operator :
In , not in
Eg : operator in
name_list = ['divya' , 'jahnavi' , 'jyothsna' ,
'murali' , 'sudheer']
print('murali' in name_list)
print('kiran' in name_list)
print('kushal' not in name_list)
print('divya' not in name_list)
Identity operator :]
Is , is not
name1 = 'divya'
name2 = 'divya'
print(name1 is name2)
print(name1 is not name2)
Assignment operator :
= , += , -= , /= , %= , **= ,
//=,>>=,<<=,:=
Bitwise operator :
& , | , ^ , ~ , << , >>
1., Write a python program to slove quadratic
equation
a=2
b=2
c=3
rev1 = (-b + ((b**2 - 4*a*c)** 0.5))/(2*a)
rev2 = (-b - ((b**2 - 4*a*c)** 0.5))/(2*a)
print(rev1)
print(rev2)
2., write a program to convert kilometer to mile
kilometer = eval(input("Enter the kilometer: "))
res = kilometer * 1.3
print(f"{kilometer}km is {round(res, 3)}miles")
3., write a program to convert Celsius to
fahrenheit
celsius = eval(input("Enter the celsius : "))
fahrenheit = ((9 / 5) * celsius) + 32
print(fahrenheit)
4., write a program to compute power of a
number
num = eval(input("Enter a number: "))
power = num * num
print(f"{num} is {power}")
conditional statement or control flow
statements
1.If – else :
percentage = 70
if percentage > 70:
print("pass")
else:
print("fail")
2. If – elif – else
num1 = int(input("Enter the number1 :
"))
num2 = int(input("Enter the number2 : "))
num3 = int(input("Enter the number3 : "))
if num1 > num2:
print("num1")
elif num2 < num3:
print("num2")
else:
print("num3")
3. Nested if
Dodut
If
If
If
Else
If
If
Elif
Else
else
4. If
A=5
If a= 5:
Print(a)
1., write a program to validate user name
and password
user_name = "YOGITHA"
pwd = 12345
print("Welcome to the app\nplease enter
your credentials to sign")
ip_user = input("Enter your user name:\t")
ip_pwd = eval(input("Enter your password:\
t"))
if user_name == ip_user and pwd ==
ip_pwd:
print("login successfull")
else:
print("please enter proper credentials")
2., python program to check if a number is
odd or even
num = eval(input("Enter a number:"))
if((num % 2)==0):
print("even number")
else:
print("odd number")
3., python program to build a tip calculator
bill_amount = eval(input("Enter bill
amount:\t"))
tip_percentage = eval(input("Enter tip
amount:\t"))
members = eval(input("total no of people:\
t"))
tip_amount = (bill_amount * tip_percentage)
/ members
print(tip_amount)
4., python program to build bmi calculator
weight = eval(input("Enter your weight:"))
height = eval(input("Enter your height:"))
BMI = weight/(height ** 2)
print(f"BMI of {weight}KG and
{height}inches is {BMI}")
5., python program to build a number
guessing game
num = eval(input("Enter the number: "))
guess = eval(input("Enter your guess
number: "))
if guess < num:
print("too low")
elif guess > num:
print("too high")
else:
print("Correct Number")
6., python program to convert decimal to
binary , octal , hexadecimal
decimal = eval(input("Enter the decimal
number: "))
bin_number = bin(decimal)
oct_number = oct(decimal)
hex_number = hex(decimal)
print(f"binary:{bin_number}")
print(f"octal:{oct_number}")
print(f"hexa:{hex_number}")
7., python program to check if a number is
positive, negative or 0
number = int(input("Enter your number: "))
if number > 0:
print("Positive Number")
elif number < 0:
print("Negative Number")
elif number == 0:
print("Zero")
8., python program to check leap year
year = eval(input("Enter the year: "))
if (year % 4 == 0 and year % 100 != 0) or
(year % 400 == 0):
print("leap year")
else:
print("not a leap year")
Iterative statements/loops :
There are different types of loops
1, for loop : iterative purpose
2, while loop : conditional based
Range:
It is a data type
It generate sequence of numbers
Range(start,stop,step)
Eg,
data = list(range(10))
print(data)
print(type(data))
eg.,
print(list(range(10)))
print(list(range(0,10)))
print(list(range(0,10,1)))
print(list(range(0,10,2)))
o/p:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
=== Code Execution Successful ===
1., write a program to display only the odd
numbers from 0 to 20 using range()
print(list(range(1,20,2)))
o/p:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
=== Code Execution Successful ===
2., write a program to display only the even
numbers from 0 to 20 using range()
print(list(range(0,20,2)))
o/p:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
=== Code Execution Successful ===
3., write a program to display the numbers
from 10 to 1 in descending order using
range()
print(list(range(10,1,-1)))
o/p:
[10, 9, 8, 7, 6, 5, 4, 3, 2]s
=== Code Execution Successful ===
Advance datatypes:
1., list
-> it is a advanced data type
-> it is a collection of data
-> it is represented as []
-> it can contain
homogenous ,heterogenous and duplicate
data
-> it is mutable data ->add,update,remove
elements
Eg.,
1.,
List = [ 1,3,4,2,4,5,7]
Print(list)
Print(type(list))
Print(list[4])
Print(list[-6])
Print(len(list))
We can append for adding item in
list
We can use pop for removing item
from list
We can use insert for add item in list
based on index
Eg., list( __index:0, __object:”Yogitha”)
Print(list)
Clear means delete all elements in
list
Extend means concatenating
elemnts
1., python program to check if a list is
empty
List1=[1,2,2]
List=[]
If list==list():
Print(“empty list)
Else:
Print(“not empty”)
2., python program to concatenate two
list
List1=[1,3,5,6,8]
List2=[3,4,5,6,7]
Print(list1+list2)
List1.extend(list2)
Print(list1)
3., python program to get the last
element of the list
Marks = [22,34,56,77,87,98,97]
Print(marks[-1]
4., python program to iterate through two
list in parallel
Marks1=[21,34,56,45,6,57]
Marks2=[34,54,65,75,76]
For I in range (len(marks1)):
Print(marks1[i],marks2[i])
5., python program to access index of list
using for loop
Equality operator upon list(==)
List1 = [1,2,3,4,5,6,7]
List2 = [10,22,33,45,65]
Print(list1 = list2)
Print(list1 != list2)
Functions and Methods present within a
list:
Len()
Lst1 = [1,2,4,4,4,4,4,4,45,6,7,8,9]
Print(“len of list: ” ,len(Lst1))
Count()
Data = 4
Print(f’count of {data} is
{lst1.count(data)}’)
Index()
Data = 4
Print(f’index of {data} is
{lst1.index(data)}’)
Sorting the elements
Lst = [1,21,13,45,6,74,83,9]
Print(lst)
Lst.sort()
Print(lst)
Lst = [1,21,13,45,6,74,83,9]
Print(lst)
Lst.sort(reversed=True)
Print(lst)
Nested list
Lst = [[1,2,3],[4,5,6],[7,8,9]]
Print(Lst[1])
Nested for loop
For I in lst:
For j in i:
Print(j, end=’ ‘)
Print()
Python program to access index of a list
using for loop
Python program to flatten a nested list
Python program to slice list
Python program to check if a list is empty
Python program to copy a file
Python program to concatenate two lists
Python program to split a list into evenly
sized chunks
lst
Python program to get the last element of
the list
Python program read a file line by line into
a list
Python program to randomly select an
element from the list
Python program to count the occurrence of
an item in a list
Python program to remove duplicate
elements from a list