Dictionaries. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, KV 5 Jaipur II Shift

Similar documents
Chapter 8 Dictionaries. Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs that associate keys to values.

Data Handing in Python

Introducing Python Modules

Python Pandas- II Dataframes and Other Operations

Simple Quesries in SQL & Table Creation and Data Manipulation

Understanding Sorting

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Introducing Python Pandas

CIS192 Python Programming

Programming Logic and Design Sixth Edition

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Chapter 14 Tuples, Sets, and Dictionaries. Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Sequences and iteration in Python

PYTHON- AN INNOVATION

LECTURE 3 Python Basics Part 2

EXAMINATIONS 2012 MID-YEAR NWEN 241 SYSTEMS PROGRAMMING. The examination contains 5 questions. You must answer ALL questions

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

Marketing Opportunities

CSc 110, Spring Lecture 24: print revisited, tuples cont.

CS Programming I: Arrays

Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Chapter 2: Lists, Arrays and Dictionaries

hereby recognizes that Timotej Verbovsek has successfully completed the web course 3D Analysis of Surfaces and Features Using ArcGIS 10

A Little Python Part 2

Webinar Series. Introduction To Python For Data Analysis March 19, With Interactive Brokers

CSE 115. Introduction to Computer Science I

Collections. Lists, Tuples, Sets, Dictionaries

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

PYTHON CONTENT NOTE: Almost every task is explained with an example

COMP 204: Dictionaries Recap & Sets

Apache Pig Releases. Table of contents

Table Joins and Indexes in SQL

CSE 115. Introduction to Computer Science I

Data Structures. Lists, Tuples, Sets, Dictionaries

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Sequential Search (Searching Supplement: 1-2)

Introduction to Python

Relation Databases. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, PGT CS II Shift Jaipur

TREES AND MUTATION 5

ENGR (Socolofsky) Week 07 Python scripts

Announcements COMP 141. Lab from Last Time. Dictionaries 11/29/2017. CS1: Programming Fundamentals

Insight into Program execution

CS2304 Spring 2014 Project 3

Python Programming Language

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Advanced Algorithms and Computational Models (module A)

CHIROPRACTIC MARKETING CENTER

/Internet Random Moment Sampling. STATE OF ALASKA Department of Health and Social Services Division of Public Assistance

Read and fill in this page now

MEIN 50010: Python Data Structures

Highline Excel 2016 Class 09: Date Functions

The Practice of Computing Using PYTHON

Lecture-14 Lookup Functions

ACA 1095 Reporting - Editing Multiple Employees. Selecting Employees to Edit

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

Guernsey Post 2013/14. Quality of Service Report

Lecture 7: Lambda & Abstract Data Types

Introduction to Bioinformatics

MAP OF OUR REGION. About

Stat 428 Autumn 2006 Homework 2 Solutions

DATA REPRESENTATION. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift, Jaipur Region. Based on CBSE curriculum Class 11. Neha Tyagi, KV 5 Jaipur II Shift

Apple Inc. US 6,587,904 US 6,618,785 US 6,636,914 US 6,639,918 US 6,718,497 US 6,831,928 US 6,842,805 US 6,865,632 US 6,944,705 US 6,985,981

Python - Variable Types. John R. Woodward

CS 234 Python Review Part 2

Working with Lists 4

Lecture 21. Chapter 12 More Python Containers

Python Programming: Lecture 2 Data Types

MRCPI General Medicine Calendar

Script language: Python Data structures

Arrays. What if you have a 1000 line file? Arrays

MAP OF OUR REGION. About

Lecture Agenda. Objects. But First... Immutable Types and Nesting. Immutable Types and Nesting

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

MRCPI General Medicine Calendar

Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

(IUCAA, Pune) kaustubh[at]iucaa[dot]ernet[dot]in.

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Criterion C: Project schedule

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Sets and Dictionaries

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

numbers by multi-digit whole numbers by using strategies based on place value, the properties of operations and the relationship between

Abstract Data Types. David E. Culler CS8 Computational Structures in Data Science Lecture 7 March 7, 2016

Computer Grade 5. Unit: 1, 2 & 3 Total Periods 38 Lab 10 Months: April and May

JAVASCRIPT LOOPS. Date: 13/05/2012 Page: 1 Total Chars: 4973 Total Words: 967

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

Collections. John Perry. Fall 2011

DSC 201: Data Analysis & Visualization

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Recap from last Python lecture

Sinusoidal Data Worksheet

List of squares. Program to generate a list containing squares of n integers starting from 0. list. Example. n = 12

(CC)A-NC 2.5 by Randall Munroe Python

Transcription:

Dictionaries Based on CBSE Curriculum Class -11 By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region

Introduction Python provides us various options to store multiple values under one variable name. Dictionaries is also a collection like a string, list and tuple. It is a very versatile data type. There is a key in it with a value.(key:value) Dictionaries are mutable data types and it is an unordered collection in the form of key:value In List, index of a value is important whereas in dictionary, key of a value is important.

Dictionary Creation To create a dictionary, it is needed to collect pairs of key:value in { }. <dictionary-name>={ <key1>:<value1>,<key2>:<value2>,<key3>:<value3>... } Example: teachers={ Rajeev : Math, APA : Physics, APS : Chemistry: SB : CS } In above given example : Key-value pair Key Value Rajeev : Math Rajeev Math APA : Physics APA Physics APS : Chemistry APA Chemistry SB : CS SB CS

Dictionary Creation Some examples of Dictionary are- Dict1= { } # this is an empty dictionary without any element. DayofMonth= { January :31, February :28, March :31, April :30, May :31, June :30, July :31, August :31, September :30, October :31, November :30, December :31} FurnitureCount = { Table :10, Chair :13, Desk :16, Stool :15, Rack :15 } By above examples you can easily understand about the keys and their values. One thing to be taken care of is that keys should always be of immutable type. Note: Dictionary is also known as associative array or mapping or hashes.

Dictionary Creation Keys should always be of immutable type. If you try to make keys as mutable, python shown error in it. For example- Here key is a list which is of mutable type. Here error shows that you are trying to create a key of mutable type which is not permitted.

Accessing a Dictionary To access a value from dictionary, we need to use key similarly as we use an index to access a value from a list. We get the key from the pair of Key: value. teachers={ Rajeev : Math, APA : Physics, APS : Chemistry: SB : CS } If we execute following statement from above example- We have selected key Rajeev and on printing it, Math got printed. Another example- If we access a non-key, error will come.

Traversal of a Dictionary To traverse a Dictionary, we use for loop. Syntax is- for <item> in <dictionary>: Here, notable thing is that every key of each pair of dictionary d is coming in k variable of loop. After this we can take output with the given format and with print statement. Assignment : Develop a dictionary of your friends in which key will be your friend s name and his number will be its value.

Traversal of Dictionary To access key and value we need to use keys() and values().for example- d.keys( ) function will display only key. d.values ( ) function will display value only.

Features of Dictionary 1. Unordered set: dictionary is a unordered collection of key:value pairs. 2. Not a sequence: like list, string and tuple, it is not a sequence because it is a collection of unordered elements whereas a sequence is a collection of indexed numbers to keep them in order. 3. Keys are used for its indexing because according to Python key can be of immutable type. String and numbers are of immutable type and therefore can be used as a key. Example of keys are as under- Key of a Dictionary should always be of immutable type like number, string or tuple whereas value of a dictionary can be of any type.

Features of Dictionary 4. Keys should be unique : Because keys are used to identify values so they should be unique. 5. Values of two unique keys can be same. 6. Dictionary is mutable hence we can change value of a certain key. For this, syntax is- <dictionary>[<key>] = <value> 4. Internally it is stored as a mapping. Its key:value are connected to each other via an internal function called hashfunction**. Such process of linking is knows as mapping. **Hash-function is an internal algorithm to link a and its value.

Working with Dictionary Here we will discuss about various operation of dictionary like element adding, updation, deletion of an element etc. but first we will learn creation of a dictionary. Dictionary initialization- For this we keep collection of pairs of key:value separated by comma (,) and then place this collection inside { }. Addition of key:value pair to an empty dictionary. There are two ways to create an empty dictionary- 1. Employee = { } 2. Employee = dict( ) After that use following syntax- <dictionary>[<key>] = <value>

Working with Dictionary 3. Creation of a Dictionary with the pair of name and value: dict( ) constructor is used to create dictionary with the pairs of key and value. There are various methods for this- I. By passing Key:value pair as an argument: The point to be noted is that here no inverted commas were placed in argument but they came automatically in dictionary. II. By specifying Comma-separated key:value pair-

Working with Dictionary III. By specifying Keys and values separately: For this, we use zip() function in dict ( ) constructor- IV. By giving Key:value pair in the form of separate sequence: By passing List By passing tuple of a list By passing tuple of tuple

Adding an element in Dictionary following syntax is used to add an element in Dictionary- Nesting in Dictionary look at the following example carefully in which element of a dictionary is a dictionary itself.

Updation in a Dictionary following syntax is used to update an element in Dictionary- <dictionary>[<existingkey>]=<value> WAP to create a dictionary containing names of employee as key and their salary as value. Output

Deletion of an element from a Dictionary following two syntaxes can be used to delete an element form a Dictionary. For deletion, key should be there otherwise python will give error. 1. del <dictionary>[<key>]- it only deletes the value and does not return deleted value. Value did not return after deletion. 2. <dictionary>.pop(<key>) it returns the deleted value after deletion. Value returned after deletion. If key does not match, given message will be printed.

Detection of an element from a Dictionary Membership operator is used to detect presence of an element in a Dictionary. 1. <key> in <dictionary> it gives true on finding the key otherwise gives false. 2. <key> not in <dictionary> it gives true on not finding the key otherwise gives false. False * in and not in does not apply on values, they can only work with keys.

Pretty Printing of a Dictionary To print a Dictionary in a beautify manner, we need to import json module. After that following syntax of dumps ( ) will be used. json.dumps(<>,indent=<n>)

Program to create a dictionary by counting words in a line Here a dictionary is created of words and their frequency.

Dictionary Function and Method 1. len( ) Method : it tells the length of dictionary. 2. clear( ) Method : it empties the dictionary. 3. get( ) Method : it returns value of the given key. It works similarly as <dictionary>[<key>] On non finding of a key, default message can be given.

Dictionary Function and Method 4. items( ) Method : it returns all items of a dictionary in the form of tuple of (key:value). 5. keys( ) Method : it returns list of dictionary keys. 6. values( ) Method : it returns list of dictionary values.

Dictionary Function and Method 7. Update ( ) Method: This function merge the pair of key:value of a dictionary into other dictionary. Change and addition in this is possible as per need. Example- In the above given example, you can see that change is done in the values of similar keys whereas dissimilar keys got joined with their values.

Thank you please follow us on our blog- www.pythontrends.wordpress.com