SlideShare a Scribd company logo
1 of 18
Basics of
Python
UNIT 4
Python Dictionary
 Dictionaries are a useful data structure for storing data in
Python because they are capable of imitating real-world data
arrangements where a certain value exists for a given key.
 The data is stored as key-value pairs using a Python dictionary.
 This data structure is mutable
 The components of dictionary were made using keys and values.
 Keys must only have one component.
 Values can be of any type, including integer, list, and tuple.
Creating the Dictionary
 Curly brackets are the simplest way to generate a
Python dictionary, although there are other
approaches as well. With many key-value pairs
surrounded in curly brackets and a colon separating
each key from its value, the dictionary can be built. (:).
The following provides the syntax for defining the
dictionary.
 Syntax:
 Dict = {"Name": "Gayle", "Age": 25}
Python provides the built-in function dict() method
which is also used to create the dictionary.
The empty curly braces {} is used to create empty dictionary.
Accessing the dictionary
values
 To access data contained in lists and tuples,
indexing has been studied. The keys of the dictionary
can be used to obtain the values because they are
unique from one another. The following method can
be used to access dictionary values.
Adding Dictionary Values
 The dictionary is a mutable data type, and
utilising the right keys allows you to change its values.
Dict[key] = value and the value can both be modified.
An existing value can also be updated using the
update() method.
Deleting Elements using del
Keyword
 The items of the dictionary can be deleted by
using the del keyword as given below.
Dictionary Methods
 Dictionary in python is one of the most frequently
used collection data type. It is represented by hey
value pairs. Keys are indexed but values may not be.
There are many python-built in functions that make
using the dictionary very easy in various python
programs. In this topic we will see the three in-built
methods namely cmp(), len() and items().
cmp()
 The method cmp() compares two dictionaries
based on key and values. It is helpful in identifying
duplicate dictionaries as well as doing a relational
comparison among the dictionaries. It is a feature on
only python2 and not available in python 3.
 Syntax
 cmp(dict1, dict2) Where dict1 and dict2 are the two
input dictionaries.In the below example we see pairs of
dictionaries compared with each other. The result is 0 if
they are equal. It is 1 if 1st dictionary has a higher
value and -1 if the first dictionary has a lower value.
len()
 This method gives the total length of the
dictionary which is equal to the number of items. An
item is a key value pair.
 Syntax
 len(dict)In the below example we see the length of
dictionaries.
dict.items()
 Sometimes we may need to print out the key
value pairs of a dictionary as a list of tuple pairs. The
length method gives this result.
 Syntax
 Dictionayname.items()In the below example we
see two dictionaries and get the items in each of them
as a tuple pairs.
Errors and Exceptions in
Python
 Errors are the problems in a program due to
which the program will stop the execution. On the
other hand, exceptions are raised when some internal
events occur which changes the normal flow of the
program.
Two types of Error occurs in python.
 Syntax errors
 Logical errors (Exceptions)
Syntax errors
 When the proper syntax of the language is not followed then a syntax error is thrown.
 # initialize the amount variable
 amount = 10000
 # check that You are eligible to
 # purchase Dsa Self Paced or not
 if(amount>2999)
 print("You are eligible to purchase Dsa Self Paced")



logical errors(Exception)
 When in the runtime an error that occurs after
passing the syntax test is called exception or logical
type. For example, when we divide any number by zero
then the ZeroDivisionError exception is raised, or when
we import a module that does not exist then
ImportError is raised.
 # initialize the amount variable
 marks = 10000
 # perform division with 0
 a = marks / 0
 print(a)
Error Handling
 When an error and an exception are raised then
we handle that with the help of the Handling method.
 Handling Exceptions with Try/Except/Finally :
We can handle errors by the Try/Except/Finally
method. we write unsafe code in the try, fall back code
in except and final code in finally block.
 # put unsafe operation in try block
 try:
 print("code start")

 # unsafe operation perform
 print(1 / 0)
 # if error occur the it goes in except block
 except:
 print("an error occurs")
 # final code in finally block
 finally:
 print("GeeksForGeeks")
Raising exceptions for a
predefined condition
 When we want to code for the limitation of
certain conditions then we can raise an exception.

More Related Content

Similar to python full notes data types string and tuple

Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 

Similar to python full notes data types string and tuple (20)

Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Mapping Data Types In Python.pptx
Mapping Data Types In Python.pptxMapping Data Types In Python.pptx
Mapping Data Types In Python.pptx
 
Python_Unit_1.pdf
Python_Unit_1.pdfPython_Unit_1.pdf
Python_Unit_1.pdf
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
Python 3.x quick syntax guide
Python 3.x quick syntax guidePython 3.x quick syntax guide
Python 3.x quick syntax guide
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python Programming
 
AI_2nd Lab.pptx
AI_2nd Lab.pptxAI_2nd Lab.pptx
AI_2nd Lab.pptx
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Python for katana
Python for katanaPython for katana
Python for katana
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Understanding Python
Understanding PythonUnderstanding Python
Understanding Python
 
trisha comp ppt.pptx
trisha comp ppt.pptxtrisha comp ppt.pptx
trisha comp ppt.pptx
 
Top Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdfTop Most Python Interview Questions.pdf
Top Most Python Interview Questions.pdf
 
GE3151_PSPP_UNIT_2_Notes
GE3151_PSPP_UNIT_2_NotesGE3151_PSPP_UNIT_2_Notes
GE3151_PSPP_UNIT_2_Notes
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
Python ppt_118.pptx
Python ppt_118.pptxPython ppt_118.pptx
Python ppt_118.pptx
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
UNIT1Lesson 2.pptx
UNIT1Lesson 2.pptxUNIT1Lesson 2.pptx
UNIT1Lesson 2.pptx
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 

More from SukhpreetSingh519414

More from SukhpreetSingh519414 (8)

CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and data
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
 
ppt notes for python language variable data types
ppt notes for python language variable data typesppt notes for python language variable data types
ppt notes for python language variable data types
 
C%20ARRAYS.pdf.pdf
C%20ARRAYS.pdf.pdfC%20ARRAYS.pdf.pdf
C%20ARRAYS.pdf.pdf
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
 
finap ppt conference.pptx
finap ppt conference.pptxfinap ppt conference.pptx
finap ppt conference.pptx
 
final security ppt.pptx
final security ppt.pptxfinal security ppt.pptx
final security ppt.pptx
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

python full notes data types string and tuple

  • 2. Python Dictionary  Dictionaries are a useful data structure for storing data in Python because they are capable of imitating real-world data arrangements where a certain value exists for a given key.  The data is stored as key-value pairs using a Python dictionary.  This data structure is mutable  The components of dictionary were made using keys and values.  Keys must only have one component.  Values can be of any type, including integer, list, and tuple.
  • 3. Creating the Dictionary  Curly brackets are the simplest way to generate a Python dictionary, although there are other approaches as well. With many key-value pairs surrounded in curly brackets and a colon separating each key from its value, the dictionary can be built. (:). The following provides the syntax for defining the dictionary.  Syntax:  Dict = {"Name": "Gayle", "Age": 25}
  • 4. Python provides the built-in function dict() method which is also used to create the dictionary. The empty curly braces {} is used to create empty dictionary.
  • 5. Accessing the dictionary values  To access data contained in lists and tuples, indexing has been studied. The keys of the dictionary can be used to obtain the values because they are unique from one another. The following method can be used to access dictionary values.
  • 6.
  • 7. Adding Dictionary Values  The dictionary is a mutable data type, and utilising the right keys allows you to change its values. Dict[key] = value and the value can both be modified. An existing value can also be updated using the update() method.
  • 8. Deleting Elements using del Keyword  The items of the dictionary can be deleted by using the del keyword as given below.
  • 9. Dictionary Methods  Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely cmp(), len() and items().
  • 10. cmp()  The method cmp() compares two dictionaries based on key and values. It is helpful in identifying duplicate dictionaries as well as doing a relational comparison among the dictionaries. It is a feature on only python2 and not available in python 3.  Syntax  cmp(dict1, dict2) Where dict1 and dict2 are the two input dictionaries.In the below example we see pairs of dictionaries compared with each other. The result is 0 if they are equal. It is 1 if 1st dictionary has a higher value and -1 if the first dictionary has a lower value.
  • 11. len()  This method gives the total length of the dictionary which is equal to the number of items. An item is a key value pair.  Syntax  len(dict)In the below example we see the length of dictionaries.
  • 12. dict.items()  Sometimes we may need to print out the key value pairs of a dictionary as a list of tuple pairs. The length method gives this result.  Syntax  Dictionayname.items()In the below example we see two dictionaries and get the items in each of them as a tuple pairs.
  • 13. Errors and Exceptions in Python  Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program. Two types of Error occurs in python.  Syntax errors  Logical errors (Exceptions)
  • 14. Syntax errors  When the proper syntax of the language is not followed then a syntax error is thrown.  # initialize the amount variable  amount = 10000  # check that You are eligible to  # purchase Dsa Self Paced or not  if(amount>2999)  print("You are eligible to purchase Dsa Self Paced")   
  • 15. logical errors(Exception)  When in the runtime an error that occurs after passing the syntax test is called exception or logical type. For example, when we divide any number by zero then the ZeroDivisionError exception is raised, or when we import a module that does not exist then ImportError is raised.  # initialize the amount variable  marks = 10000  # perform division with 0  a = marks / 0  print(a)
  • 16. Error Handling  When an error and an exception are raised then we handle that with the help of the Handling method.  Handling Exceptions with Try/Except/Finally : We can handle errors by the Try/Except/Finally method. we write unsafe code in the try, fall back code in except and final code in finally block.
  • 17.  # put unsafe operation in try block  try:  print("code start")   # unsafe operation perform  print(1 / 0)  # if error occur the it goes in except block  except:  print("an error occurs")  # final code in finally block  finally:  print("GeeksForGeeks")
  • 18. Raising exceptions for a predefined condition  When we want to code for the limitation of certain conditions then we can raise an exception.