0% found this document useful (0 votes)
67 views

Python Data Types

This document discusses Python's standard data types including numbers, strings, lists, tuples, dictionaries, and sets. It provides examples and explanations of how to work with each data type, such as defining numerical types like integers and floats, using single and double quotes for strings, appending and deleting items from lists, accessing tuple elements and using operators like addition and multiplication on tuples, retrieving and updating dictionary values using keys, and creating empty and populated sets.

Uploaded by

monisha.jalin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Python Data Types

This document discusses Python's standard data types including numbers, strings, lists, tuples, dictionaries, and sets. It provides examples and explanations of how to work with each data type, such as defining numerical types like integers and floats, using single and double quotes for strings, appending and deleting items from lists, accessing tuple elements and using operators like addition and multiplication on tuples, retrieving and updating dictionary values using keys, and creating empty and populated sets.

Uploaded by

monisha.jalin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Python Fundamentals

STANDARD DATA TYPES


Python Standard Data Types

• Data Type define the type of data stored in the Memory.

• Python has standard 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

1. int (signed integers)


2. float (floating point real values)
3. complex (complex numbers)

Int_eg = 100
float_eg = 3.14
comp_eg = 3.14j
Python Fundamentals

STRINGS
Strings
• A string is a series of characters.

• Anything inside quotes is considered a string in Python

• We can use single or double quotes around your strings like this

"A sample string."


'Another sample string.'
'I informed him that, "Python is very easy" '
Python Fundamentals

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)

# All list access formats can be used for del too.


Python Fundamentals

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.

months = ("Jan", "Feb", "Mar", "Apr", "May",


"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

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)

• Len() – number of items in tuple


print (len(myTuple))

• In – Checks if an item is in the tuple


print('world' in myTuple)

• Del() – Delete the entire tuple


del myTuple
print(myTuple)
Tuples – addition + and multiplication *
myTuple = ('hello', 'world', 2022)

• Addition Operator: + Concatenate Tuples


(+ and * will not alter the original tuple)
print (myTuple + ('how', 'are', 'you'))

• Multiplication Operator: * Duplicate a tuple and concatenate it


print(myTuple*3)
Python Fundamentals

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

myStudents = {"Abhi":30, "Sibi":28, "Subi":"not updated"}

• Or you can also declare using dict() method


• use round brackets ( ) instead of curly brackets { } no quotes for dictionary keys.
myStudents = dict(Abhi=30, Sibi=28, Subi="not updated“)

print(myStudents["Abhi"])
myStudents["Subi"] = 25
myStudents["Bibi"] = 22
print(myStudents)
Dictionary – get(), items(), keys(), values()
myStudents = {"Abhi":30, "Sibi":28, "Subi":"not updated"}

• Get() : Returns value of given key


print(myStudents.get("Abhi")

• Items() : Returns a list of dictionary's pairs as tuples


print(myStudents.items()
Output: dict_items([('Abhi', 30), ('Sibi', 28), ('Subi', 'not updated')])

• keys() : Returns a list of dictionary's keys


myStudents.keys()
Output: dict_keys(['Abhi', 'Sibi', 'Subi'])

• values() : Returns a list of dictionary's values


myStudents.values()
Output: dict_values([30, 28, 'not updated'])
Dictionary – in, len(), update()
myStudents = {"Abhi":30, "Sibi":28, "Subi":"not updated"}

• In : Check if an item is in a dictionary


"Abhi" in myStudents
28 in myStudents.values()

• len( ) : Find the number of items in a dictionary


Print(len(myStudents))

• 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

myStudents = {"Abhi":30, "Sibi":28, "Subi":"not updated"}

• Clear() : Deletes all items in dictionary


myStudents.clear()
print(myStudents)

• del: Deletes the entire dictionary


del myStudents
print(myStudents)
Python Fundamentals

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 = {"January", "February", "March", "April"}


print(months)
print(type(months))

print("the set elements are:")


for i in months:
print(i)
Python Set – Creation
• Creating set using set() method

months = set(["January","February","March","April","April"])

print(months) #duplicate elements will be discarded


print(type(months))

print("the set elements are:")


for i in months:
print(i)
Python Set – Empty set Creation
• Creating empty set using set() method

# Empty curly braces will actually create a dictionary


days = {}
print(type(days))

# Empty set using set() function


days = set()
print(type(days))

You might also like