100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Class notes

Python module 3 notes for 1st year students,

Rating
-
Sold
-
Pages
21
Uploaded on
11-06-2025
Written in
2024/2025

Python programming language along with synonyms for students is covered in my document uploaded.For 1st year students this document will help and a CAED notes also uploaded

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
June 11, 2025
Number of pages
21
Written in
2024/2025
Type
Class notes
Professor(s)
Kamala cr
Contains
All classes

Subjects

Content preview

Module 3 Python



CHAPTER 5: DICTIONARIES AND STRUCTURING DATA
1. The Dictionary Data Type
2. Pretty Printing
3. Using Data Structures to Model Real-World Things.


The Dictionary Data Type
 A dictionary is a collection of many values. Indexes for dictionaries can use many different data
types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value
is called a key-value pair.
 A dictionary is typed with braces, {}.



 This assigns a dictionary to the myCat variable. This dictionary’s keys are 'size', 'color', and
'disposition'. The values for these keys are 'fat', 'gray', and 'loud', respectively. You can access these
values through their keys:




 Dictionaries can still use integer values as keys, but they do not have to start at 0 and can be any
number.

, Module 3 Python

Dictionaries vs. Lists
 Unlike lists, items in dictionaries are unordered.
 The first item in a list named spam would be spam[0]. But there is no “first” item in a dictionary.
While the order of items matters for determining whether two lists are the same, it does not matter
in what order the key-value pairs are typed in a dictionary.




 Trying to access a key that does not exist in a dictionary will result in a KeyError error message,
much like a list’s “out-of-range” IndexError error message.




 We can have arbitrary values for the keys that allows us to organize our data in powerful ways.
 Ex: we want to store data about our friends’ birthdays. We can use a dictionary with the names as
keys and the birthdays as values.




Program Output
 We create an initial dictionary and store it in birthdays 1.
 We can see if the entered name exists as a key in the dictionary with the in keyword 2.
 If the name is in the dictionary, we access the associated value using square brackets 3; if not, we
can add it using the same square bracket syntax combined with the assignment operator 4.

, Module 3 Python

The keys(), values(), and items() Methods
 There are three dictionary methods that will return list-like values of the dictionary’s keys, values,
or both keys and values: keys(), values(), and items().
 Data types (dict_keys, dict_values, and dict_items, respectively) can be used in for loops.




 A for loop can iterate over the keys, values, or key-value pairs in a dictionary by using keys(),
values(), and items() methods.
 The values in the dict_items value returned by the items() method are tuples of the key and value.




 If we want a true list from one of these methods, pass its list-like return value to the list() function.




 The list(spam.keys()) line takes the dict_keys value returned from keys() and passes it to list(),
which then returns a list value of ['color', 'age'].
 We can also use the multiple assignment trick in a for loop to assign the key and value to separate
variables.
CA$12.15
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
sidduteli

Get to know the seller

Seller avatar
sidduteli Bapuji institute of engineering and technology
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
7 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions