Dictionary.

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

Dictionaries: Keeping track of pairs
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Data types: Complex types (Dictionaries)
LING 388: Computers and Language
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
CISC101 Reminders Slides have changed from those posted last night…
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
Coding Concepts (Data Structures)
Recitation Outline C++ STL associative containers Examples
String and Lists Dr. José M. Reyes Álamo.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Datatypes in Python (Part II)
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Dictionaries: Keeping track of pairs
CHAPTER 4: Lists, Tuples and Dictionaries
Python Sets and Dictionaries
15-110: Principles of Computing
Bryan Burlingame Halloween 2018
Python Review
CSE 231 Lab 8.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sample lecture slides.
Python List.
Tuple.
Introduction to Computer Science
Presentation transcript:

dictionary

What is dictionary Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’. A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and tuples, but the key-values can be repeated and be of any type.

How to create dictionary Creating a dictionary is as simple as placing items inside curly braces {} separated by comma. An item has a key and the corresponding value expressed as a pair, key: value. While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique. EXAMPLES:-(# It is for comment not in the code)   my_dict = dict([(1,'apple'), (2,'ball')])

How to access elements from a dictionary While indexing is used with other container types to access values, dictionary uses keys. Key can be used either inside square brackets or with the get() method. The difference while using get() is that it returns None instead of KeyError, if the key is not found. EXAMPLE:-(# is for comment not in the code.) my_dict = {'name':'Jack', 'age': 26} # Output: Jack print(my_dict['name']) # Output: 26 print(my_dict.get('age'))

How to change or add elements in a dictionary Dictionary are mutable. We can add new items or change the value of existing items using assignment operator. If the key is already present, value gets updated, else a new key: value pair is added to the dictionary. EXAMPLES:-(# is for comment not in the code.)  my_dict = {'name':'Jack', 'age': 26}  # update value  my_dict['age'] = 27  #Output: {'age': 27, 'name': 'Jack'}   print(my_dict)  # add item  my_dict['address'] = 'Downtown'    # Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}

How to delete elements from a dictionary We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value. The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. All the items can be removed at once using the clear() method. We can also use the del keyword to remove individual items or the entire dictionary EXAMPLE:-(# is for comment not in the code.)  # remove a particular item  print(squares.pop(4))  # Output: {1: 1, 2: 4, 3: 9, 5: 25}  # remove an arbitrary item  print(squares.popitem())  # Output: {2: 4, 3: 9, 5: 25}  print(squares)  # delete a particular item  del squares[5]  # remove all items  squares.clear()  # delete the dictionary itself  del squares

THE END MADE BY:-CHETANYA SHARMA