Data Types1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

DATA HANDLING :DATATYPE

CHAPTER : 3
DATA TYPE

 Data type in Python specifies the type of


data we are going to store in any Python supports following data types:
variable, the amount of memory it will  Numbers ( int, float, complex)
take and type of operation we can
perform on a variable.  Sequence(String, List, Tuple)/Order
 Data can be of many types e.g. character,  Dictionary /Sets: unorder
integer, real, string etc.
NUMBERS

Number data types are used to store numeric values. Numbers in Python can be of following
types:
(i)Integers
a) Integers(signed)
b) Booleans
(ii)Floating point numbers
(iii)Complex Numbers
INTEGERS

Integers allows to store whole numbers only and there is no fraction parts.
 Integers can be positive and negative e.g. 100, 250, -12, +50
There are two integers in Python:
1) Integers(signed) : it is normal integer representation of whole numbers.
Integers in python can be on any length, it is only limited by memory available.
2)Booleans: it allows to store only two values True and False.
 The internal value of boolean value True and False is 1 and 0 resp.
 We can get boolean value from 0 and 1 using bool() function.
>>>bool(1) True
>>>int(False) 0

>>>str(False)

”False”
FLOATING POINT NUMBERS

 It allows to store numbers with decimal points. For e.g. 2.14.


 The decimal point indicate that it is not an integer but a float value.
 100 is an integer but 100.5 is a float value.

1. Fractional Form : 200.50, 0.78, -12.787


2. Exponent Form : it is represented with mantissa and exponent. For e.g
FLOATING POINT NUMBERS

>>>x = 1.5E2 # means 1.5 x 102 which is 150


>>>print(x)
>>>y=12.78654E04
>>>print(y)
FLOATING POINT NUMBERS

 Floating point number are mainly used for storing values like distance, area,
temperature etc. which have a fractional part.
Floating point numbers have two advantage over integers:
 they can represent values between the integers
 they can represent a much greater range of values
But floating point numbers suffers from one disadvantage also:
 Floating point operations are usually slower than integer operations.
COMPLEX NUMBERS

 Python represent complex numbers in the form A+Bj.


 a = 0 + 6j
 b = 2.5 + 3J
>>>a=4+5j
 Python allows to retrieve real and imaginary part of complex number using attributes: real and imag
If the complex number is a then we can write a.real or a.imag
Example

>>>a=1+3.54j
Sequence

 A Python sequence is an ordered collection of items, where


each item is indexed by an integer value
 It can be classified into
 String
 List
 Tuple
String

 String is a group of characters.


 These characters may be alphabets, digits or special characters including spaces.
 String values are enclosed either
 in single quotation marks (for example ‘Hello’) or
 in double quotation marks (for example “Hello”).
 The quotes are not a part of the string, they are used to mark the beginning and end of
the string for the interpreter.
 For example
 >>> str1 = ‘Good Morning’
 >>> str2 = "452"
String Types in Python

 Python allows you to have two string types:


1. Single Line Strings
 The string we create using single or double quotes are normally single-line
string i.e. they must terminate in one line.
 For e.g if you type as
 Name="KV” and press enter
 Python we show you an error “EOL while scanning string literal”
 The reason is quite clear, Python by default creates single-line string
with both single quotes and it must terminate in the same line by quotes
2.Multiline String
 Some times we need to store some text across multiple lines. For that Python offers
multiline string.
 To store multiline string Python provides two ways:
A.By adding a backslash at the end of normal Single / Double
 quoted string. For e.g.
 >>> Name="1/6 Mall Road \ Kanpur"
 >>> Name
 '1/6 Mall RoadKanpur'
 >>>
2.Multiline String
(b) By typing text in triple quotation marks
 for e.g.

>>> Address="""1/7 Preet Vihar


New Delhi
India"""
 >>> print(Address) will give
1/7 Preet Vihar
New Delhi
India
STRING

 In Python string is a sequence of characters and each character can be individually access using index.
 From beginning the first character in String is at index 0 and last will be at len-1.
 From backward direction last character will be at index -1 and first character will be at –len.

Forward 0 1 2 3 4 5 6
indexing W E L C O M E
messag
e -7 -6 -5 -4 -3 -2 -1
Backward indexing
STRING

To access individual character of String (Slicing). we can use the syntax:
 StringName[index position]
>>>stream=“Science”

>>>print(stream[0]) S
>>>print(stream[3]) e
>>>print(stream[-1]) e
STRING

What will be the output:


>>>stream=“Science”

>>>print(stream[5])
>>>print(stream[-4])
>>>print(stream[-len(stream)])
>>>print(stream[8])
LIST

 List is a sequence of items separated by commas and items are enclosed in


square brackets [ ].
 Note that items may be of different data types.
 #To create a list
>>> list1 = [5, 3.4, "New Delhi", "20C", 45] #print the elements of the list list1
>>> list1
[5, 3.4, 'New Delhi', '20C', 45]
ACCESSING ELEMENTS IN A LIST

 Each element in list is accessed using value called index as we do in string(forward and backward).
 The fist index value is 0, the second index is 1 and so on.
 To access an element, use square brackets with the index [] value of that element.

 >>> list1 = [2,4,6,8,10,12]


 >>> list1[0] #returns first element of list1
 2
 >>> list1[3] #returns fourth element of list1
 8
 #Out of range index value for the list returns error
 >>> list1[15]
TYPES OF LIST

1. empty list: l = list()


2.long list l=[1,2,3,4,5,6,7,8,9,22,34]
3. nested list : l=[3,4,[1,2]]
ACCESSING ELEMENTS IN A LIST

 >>> list1[1+4]
 12
 >>> list1[-1] #return first element from right
 12
 #length of the list1 is assigned to n
 >>> n = len(list1)
 >>> print(n)
 6
 #Get the last element of the list1
 >>> list1[n-1]
 12
TUPLE

 Tuple is a sequence of items separated by commas and


items are enclosed in parenthesis ( ).
 Once created, we cannot change items in the tuple.
Similar to List, items may be of different data types.
 #create a tuple tuple1
>>> tuple1 = (10, 20, "Apple", 3.4, 'a')
 #print the elements of the tuple tuple1
>>> print(tuple1)
(10, 20, "Apple", 3.4, 'a')
MAPPING

 Mapping is an unordered data type in Python.


 Example:Dictionary
 Dictionary in Python holds data items in key-value pairs and
Items are enclosed in curly brackets { }.
 Dictionaries permit faster access to data.
 Every key is separated from its value using a colon (:) sign.
 The key value pairs of a dictionary can be accessed using the key.
 Keys are usually of string type and their values can be of any
data type.
 In order to access any value in the dictionary, we have to specify
its key in square brackets [ ].
EXAMPLE

 create a dictionary
 >>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
 >>> print(dict1)
 {'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
 #getting value by specifying a key
 >>> print(dict1['Price(kg)'])
 120
HOME WORK

 What are data types? What are python built-in datatypes Which data type of python handles
Numbers?
 Why Boolean considered a subtype of integer? Identify the data types of the values:
 5, 5j, 10.6, “100‟, “100”, 2+4j, [10,20,30], (“a”,”b”,”c”),
{1:100,2:200}
 What is the difference in output of the following ?
 print(len(str(19//4)))
 print(len(str(19/4))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy