0% found this document useful (0 votes)
14 views19 pages

Python Data Types

The document provides an overview of various data types in Python, including Numbers, Strings, Lists, Tuples, Sets, and Dictionaries. Each data type is described with its characteristics, examples, and common operations. The document emphasizes the mutable and immutable nature of certain data types, along with examples of how to manipulate them.

Uploaded by

Deepa Bhoi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
14 views19 pages

Python Data Types

The document provides an overview of various data types in Python, including Numbers, Strings, Lists, Tuples, Sets, and Dictionaries. Each data type is described with its characteristics, examples, and common operations. The document emphasizes the mutable and immutable nature of certain data types, along with examples of how to manipulate them.

Uploaded by

Deepa Bhoi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Python Data Types

 Data stored in memory can be of many types.

 Python has the following data types :

1. Numbers

2. String

3. List

4. Tuple

5. Set

6. Dictionary
NUMBERS
 This data type stores numeric values. Numbers object are created when you assign
a value to them.
 Python supports following numerical types:
 Int
 Float
 Complex

Example :
a,b,c = 1, 1.5, 2+9j
print(“a : ”,a)
print(“b : ”,b)
print(“c : ”,c)
Mathematical Functions on Numbers
Function Description
abs(x) Returns the absolute value of x
sqrt(x) Finds square root of x
ceil(x) Finds the smallest integer not less than x
floor(x) Finds the greatest integer not greater than x
pow(x,y) Finds x raised to y
exp(x) Returns exponential of x
fabs(x) Returns absolute value of x
log(x) Finds natural log of x
log10(x) Finds log to the base 10 of x
max(x1,x2,….,xn) Returns largest of its arguments
min(x1,x2,….,xn) Returns smallest of its arguments
round(x) x will be rounded to n digits if x is decimal
number
Mathematical Functions on Numbers :

Run all these and observe outputs.


STRINGS
 Set of characters represented in the quotation marks.
 Python allows for either ‘ ’ or “ ”.
 Subset of string can be taken from slice operator [:].
 In string ‘+’ indicates concatenation and ‘*’ is the repetition operator.
Example :
str = ‘Welcome to Python Programming ’
print(str) #Prints Complete String
print(str[0]) #Prints first character of string
print(str[11:17]) #Prints characters starting from 11th to 16th
print
print(str[11:]) #Prints starting from 11th character
print(str*2) #Prints string 2 times
print(str + ‘Sesssion’) #Prints concatenated string

Run all these and observe outputs.


LIST

 List is an ordered sequence of items.


 All the items in the list do not need to be of same datat type.
 Items separated by commas are enclosed within brackets [ ].
 The values stored in a list can be accessed using slice operator [:] with
indices starting at 0 in the beginning of the list.
 In list also ‘+’ is concatenation operator and ‘*’ is the repetition
operator.
 List is mutable that means values of list can be update.
Example :
first_list = [‘abcd’, 147,2.43, ’Tom’, 74.9]
second_list = [111, ‘Tom’]

print(first_list) #Prints complete list


print(first_list[0]) # Prints first element of list
print(first_list[1:3]) # Prints elements starting from 1st
position to 2nd position
print(first_list[2:]) # Prints all elements starting
from 2nd position
print(first_list * 2) # Prints first list two times
print(first_list + second_list ) # Prints concatenated lists

Run all these and observe outputs.


 We can update the list by using slice operator.

# Demo of list update

list = [‘abcd’, 147,2.43, ’Tom’, 74.9]


print(“Item at position 2 = ”, list[2])
list[2] = 500
print(“Now item at position 2 = ”, list[2])

Similarly change the item at position 0 and 1.


TUPLE
 Tuple is another sequence datatype similar to list.
 Tuple also consists of values separated by commas enclosed in bracket ( ).
 Tuples are immutable that means the elements of tuple can not be changed.
 Example :

first_tuple = (‘abcd’, 147, 2.43, ’Tom’, 74.9)


second_tuple = (111, ‘Tom’)

print(first_tuple) #Prints complete tuple


print(first_tuple[0]) # Prints first element of tuple
print(first_tuple[1:3]) # Prints elements starting from 1st position to 2nd position
print(first_tuple[2:]) # Prints all elements starting from 2nd position
print(first_tuple * 2) # Prints first tuple two times
print(first_list + second_tuple) # Prints concatenated tuples

Run all these lines and observe output.


 The following code is invalid with tuple whereas this is valid with Lists.

first_list = [‘abcd’, 147, 2.43, ’Tom’, 74.9]


first_tuple = (‘abcd’, 147, 2.43, ’Tom’, 74.9)
tuple[2] = 100 #Invalid
syntax with tuple
list[2] = 100 #
Valid syntax with list
SET
 Set is unordered collection of unique items.
 Set is defined by values separated by commas and enclosed in bracket { }.
 Since they are unordered, we can not access or change an element of set using
indexing or slicing.
 We can perform set operations like union, intersection, difference on two sets.
 Set have unique values. They eliminate duplicates.
 Demo of Set creation

s1 = {1,2,3} # Set1 of integer numbers


print(“Set 1: ”, s1)
s2 = {1, 2, 3, 2, 1, 2} # Set2 contains only unique value
print(“Set 2 : ”, s2)
s3 = {1, 2.4, ‘apple’, ‘Tom’, 3} # Set3 of mixed data types
print(“Set 3 : ”, s3)

# s4 = {1, 2, [3,4]} # Set4 is not valid because set can not have
mutable items
# print(“Set 4 : ”, s4) # Hence not permitted.

s5 = set([1, 2, 3, 4]) # Using set function to create a Set5 from list


print(“Set 5 : ”, s5)

Run all these lines of code and observe output.


DICTIONARY
 Dictionary is unordered collection of key-value pairs.
 It is generally used when we have huge amount of data.
 We must know the key to retrieve the value.
 In Python, dictionaries are defined within the braces { } with each item being a
pair in the form of Key:Value
 Key and Value can be of any type.
 Keys are generally numbers or string. Values on the other hand can be any
arbitrary object.
 Dictionaries are enclosed with { } and values can be assigned and accessed using
square brackets.
 No duplicate key is allowed.
 Keys are immutable. Key does not permits mutable objects like list.
 Example :

dict = {}
dict[‘one’] = “This is One”
dict[2] = “This is Two”
small_dict = {‘Name’:’John’, ‘Code’:’6734’, ‘Dept’:’sales’}

print(dict[‘one’]) # Prints value for key ‘one’


print(dict[2]) # Prints value for key 2
print(small_dict) # Prints complete dictionary small_dict
print(small_dict.keys()) # Prints all the keys of small_dict
print(small_dict.values() # Prints all the values of small_dict

Run all these lines of codes and observe output.


 We can update a dictionary by adding a new key-value pair or modifying
an existing entry :

dict1 = {‘Name’:’Tom’,’Age’:20,’Height’:160}
print(dict1)
dict1[‘Age’] = 25 # Updating
existing key-value pair
print(‘Dictionary after update : ’, dict1)

dict[‘Weight’] = 60 # Adding new


key-value pair
print(‘Dictionary after adding new key-value pair : ’, dict1)

Run all these lines of codes and observe output.


 We can delete the entire dictionary elements or individual elements.
 We can use del statement to delete the dictionary completely.

# Demo of deleting dictionary

dict1 = {‘Name’:’Tom’,’Age’:20,’Height’:160}
print(dict1)
del dict1[‘Age’] # Deleting key-value
pair ‘Age’:20
print(“Dictionary after deletion : ”, dict1)

dict1.clear() # Clearing
entire dictionary
print(dict1)

You might also like