Python Datatypes (1)
Python Datatypes (1)
M04
By
Sachin Vasantrao
Inkane PGT,
AECS,Indore
Python Built-in Core Data Types
Python offers following built-in core data types :
i) Numbers ii) String iii) List iv) Tuple v) Dictionary
Floating-Point Complex
Integers
Numbers Numbers
Integers(Signed)
Boolean
Integers
Output:
Enter the value of a: 45
Enter the value of b:
67
The sum of two
integers= 112
Floating Point Numbers
A number having fractional part is a floating point
number. It has a decimal point. It is written in two forms :
i) Fractional Form : Normal decimal notation e.g. 675.456
ii) Exponent Notation: It has mantissa and
exponent. e.g. 6.75456E2
Advantage of Floating point numbers:
They can represent values between the integers.
They can represent a much greater range of values.
Disadvantage of Floating point numbers:
Floating-point operations are usually slower than
integer operations.
Demonstration of Floating Point Data Type
#Demonstration of Float Number- Calculate Simple
Interest princ=float(input("Enter the Principal Amount:"))
rate=float(input("Enter the Rate of
interest:")) time=float(input("Enter the Time
period:")) si=(princ*rate*time)/100
print("The Simple Interest=",si)
Output:
Enter the Principal
Amount:5000 Enter the Rate of
interest:8.5 Enter the Time
period:5.5 Simple Interest=
2337.5
Complex Number
Python represents complex numbers in the form a+bj.
Forwar d indexing 0 1 2 3 4 5 6 7
Subj C o m p u t e r
-8 -7 -6 -5 -4 -3 -2 -1 B
ackward indexing
String example
#Demonstration of String- To input string & print
it my_name=input("What is your Name? :")
print("Greetings!!!")
print("Hello!",my_name)
print("How do you
do?")
Output :
What is your Name? :Ananya
Inkane Greetings!!!
Hello! Ananya
Inkane How do you
do?
List
The List is Python’s compound data type. A List in Python
represents a list of comma separated values of any data type
between square brackets. Lists are Mutable.
#Demonstration of List- Program to input 2 list & join
it List1=eval(input("Enter Elements for List 1:"))
List2=eval(input("Enter Elements for List 2:"))
List=List1+List2
print("List 1 :",List1)
print("List 2 :",List2)
print("Joined List :",List)
Output:
Enter Elements for List 1:[12,78,45,30]
Enter Elements for List 2:[80,50,56,77,95]
List 1 : [12, 78, 45, 30]
List 2 : [80, 50, 56, 77, 95]
Joined List : [12, 78, 45, 30, 80, 50, 56,
77, 95]
Tuple
The Tuple is Python’s compound data type. A Tuple in Python
represents a list of comma separated values of any data type
Within parentheses. Tuples are Immutable.
#Demonstration of Tuple- Program to input 2 tuple &
join it
tuple1=eval(input("Enter Elements for Tuple 1:"))
tuple2=eval(input("Enter Elements for Tuple
2:"))
Tuple=tuple1+tuple2
print(“Tuple 1 :“,tuple1)
print(“Tuple 2 :“,tuple2)
print("Joined Tuple :“,Tuple)
Output:
Enter Elements for Tuple 1:(12,78,45,30)
Enter Elements for Tuple 2:(80,50,56,77,95)
List 1 : (12, 78, 45, 30)
List 2 : (80, 50, 56, 77, 95)
Dictionary
Dictionaries are unordered collection of elements in curly braces in the form
of a key:value pairs that associate keys to values. Dictionaries are Mutable.
As dictionary elements does not have index value ,the elements are accessed
through the keys defined in key:value pairs.
#Demonstration of Dictionary- Program to save Phone nos. in dictionary
& print it
Phonedict={“Madhav”:9876567843,”Dilpreet”:7650983457,”Murugan”:9067
2 08769,”Abhinav”:9870987067}
print(Phonedict)
Output:
{'Madhav': 9876567843, 'Dilpreet': 7650983457, 'Murugan': 9067208769,
'Abhinav': 9870987067}