Python Data Types
Python Data Types
Python Data Types
1. Numbers
2. Strings
3. List
4. Tuple
5. Dictionary
6. Set
Python Fundamentals
NUMBERS
Python Numbers Overview
• Number data types store numeric values.
• It is created when we assign a value to them.
• Using del, we can remove the reference to a variable
A = 10
B = 20
print(A)
del A,B
print(A)
Python Numerical Types
• Python supports three numerical types
Int_eg = 100
float_eg = 3.14
comp_eg = 3.14j
Python Fundamentals
STRINGS
Strings
• A string is a series of characters.
• We can use single or double quotes around your strings like this
LISTS
LISTS
• Collection of data which are normally related Instead of storing these as
separate variables
• Lists are one of 4 built-in data types in Python used to store collections
of data, the other 3 are Tuple, Dictionary and Set
• Each of them is different in its qualities and usage.
• Example of a list:
studentsAge = [18, 21, 23, 20, 21]
print(studentsAge)
print(studentsAge[0])
LISTS - append() and del
studentsAge = []
studentsAge.append(20)
studentsAge.append("hi")
Print(studentsAge)
del studentsAge[1]
Print(studentsAge)
TUPLES
Tuples
• Tuples are like lists, but unlike lists, we cannot modify their initial values.
• Eg: to store the names of the months of the year.
• we use round brackets ( ) when declaring a tuple.
print(months[0])
print(months[-1])
months[0] = "test"
TypeError: 'tuple' object does not support item assignment
Tuples – del(), in , and len()
myTuple = ('hello', 'world', 2022)
DICTIONARY
Dictionary
• Dictionary is a collection of related data PAIRS.
• dictionaryName = {dictionarykey : data}
• (dictionary keys must be unique in one dictionary)
• For example, if we want to store the name and age of students
print(myStudents["Abhi"])
myStudents["Subi"] = 25
myStudents["Bibi"] = 22
print(myStudents)
Dictionary – get(), items(), keys(), values()
myStudents = {"Abhi":30, "Sibi":28, "Subi":"not updated"}
• Update(): Adds one dictionary's key-values pairs to another. Duplicates are removed.
day1 = {1: 'monday', 2: 'tuesday'}
day2 = {1: 'wednesday', 3: 'thursday'}
day1.update(day2)
print (day1)
{1: 'wednesday', 2: 'tuesday', 3: 'thursday'}
Dictionary – clear(), del
Python Set
Python Set – Creation
• set is the collection of the unordered items.
• Each element in the set must be unique.
• There is no index to the elements of the set so we cannot access them directly with index
• We can create set using {} or using set() method
months = set(["January","February","March","April","April"])