Instantly share code, notes, and snippets.

@DaveSeahYS

DaveSeahYS / assingment 9.4.py

  • Download ZIP
  • Star ( 0 ) 0 You must be signed in to star a gist
  • Fork ( 0 ) 0 You must be signed in to fork a gist
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.
  • Learn more about clone URLs
  • Save DaveSeahYS/14ade43c83c993bc8252890513eb32f2 to your computer and use it in GitHub Desktop.

Library homepage

  • school Campus Bookshelves
  • menu_book Bookshelves
  • perm_media Learning Objects
  • login Login
  • how_to_reg Request Instructor Account
  • hub Instructor Commons

Margin Size

  • Download Page (PDF)
  • Download Full Book (PDF)
  • Periodic Table
  • Physics Constants
  • Scientific Calculator
  • Reference & Cite
  • Tools expand_more
  • Readability

selected template will load here

This action is not available.

Engineering LibreTexts

9.4: Looping and Dictionaries

  • Last updated
  • Save as PDF
  • Page ID 3162

  • Chuck Severance
  • University of Michigan

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\id}{\mathrm{id}}\)

\( \newcommand{\kernel}{\mathrm{null}\,}\)

\( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\)

\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\)

\( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

\( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vectorC}[1]{\textbf{#1}} \)

\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

If you use a dictionary as the sequence in a for statement, it traverses the keys of the dictionary. This loop prints each key and the corresponding value:

Code 9.4.1 (Python)

Here's what the output looks like:

Again, the keys are in no particular order.

We can use this pattern to implement the various loop idioms that we have described earlier. For example if we wanted to find all the entries in a dictionary with a value above ten, we could write the following code:

The for loop iterates through the keys of the dictionary, so we must use the index operator to retrieve the corresponding value for each key. Here's what the output looks like:

We see only the entries with a value above 10.

If you want to print the keys in alphabetical order, you first make a list of the keys in the dictionary using the keys method available in dictionary objects, and then sort that list and loop through the sorted list, looking up each key and printing out key-value pairs in sorted order as follows:

First you see the list of keys in unsorted order that we get from the keys method. Then we see the key-value pairs in order from the for loop.

Python for Everybody

Exercise 9.1, exercise 9.2, exercise 9.3, exercise 9.4, exercise 9.5.

Python for Everybody

  • Coursera: Python for Everybody Specialization
  • edX: Python for Everybody
  • FutureLearn: Programming for Everybody (Getting Started with Python)
  • FreeCodeCamp
  • Free certificates for University of Michigan students and staff

If you log in to this site you have joined a free, global open and online course. You have a grade book, autograded assignments, discussion forums, and can earn badges for your efforts.

We take your privacy seriously on this site, you can review our Privacy Policy for more details.

If you want to use these materials in your own classes you can download or link to the artifacts on this site, export the course material as an IMS Common Cartridge®, or apply for an IMS Learning Tools Interoperability® (LTI®) key and secret to launch the autograders from your LMS.

The code for this site including the autograders, slides, and course content is all available on GitHub . That means you could make your own copy of the course site, publish it and remix it any way you like. Even more exciting, you could translate the entire site (course) into your own language and publish it. I have provided some instructions on how to translate this course in my GitHub repository.

And yes, Dr. Chuck actually has a race car - it is called the SakaiCar . He races in a series called 24 Hours of Lemons .

assignment 9.4 python for everybody

  • Table of Contents
  • Course Home
  • Assignments
  • Peer Instruction (Instructor)
  • Peer Instruction (Student)
  • Change Course
  • Instructor's Page
  • Progress Page
  • Edit Profile
  • Change Password
  • Scratch ActiveCode
  • Scratch Activecode
  • Instructors Guide
  • About Runestone
  • Report A Problem
  • Write Code Questions
  • Peer Instruction: Lists Multiple Choice Questions
  • Mixed-up Code Questions
  • 9.1 A list is a sequence
  • 9.2 Lists are mutable
  • 9.3 Traversing a list
  • 9.4 List operations
  • 9.5 List slices
  • 9.6 List methods
  • 9.7 Deleting elements
  • 9.8 Lists and functions
  • 9.9 Lists and strings
  • 9.10 Parsing lines
  • 9.11 Objects and values
  • 9.12 Aliasing
  • 9.13 List arguments
  • 9.14 Debugging
  • 9.15 Glossary
  • 9.16 Multiple Choice Questions
  • 9.17 Mixed-Up Code Questions
  • 9.18 Write Code Questions
  • 9.19 Group Work: Lists
  • 9.4. List operations" data-toggle="tooltip">
  • 9.6. List methods' data-toggle="tooltip" >

9.5. List slices ¶

The slice operator also works on lists:

If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end. So if you omit both, the slice is a copy of the whole list.

Q-2: What is printed by the following statements?

  • [ [ ], 3.14, False]
  • Yes, the slice starts at index 4 and goes up to and including the last item.
  • [ [ ], 3.14]
  • By leaving out the upper bound on the slice, we go up to and including the last item.
  • [ [56, 57, "dog"], [ ], 3.14, False]
  • Index values start at 0.

Q-3: What is printed by the following statements?

  • An empty slice like this would not produce an empty list. Think about how the slice indexes.
  • This would be correct if the slice was [:1]
  • [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
  • Omitting both indexes in a slice will create a copy of the whole list.

Since lists are mutable, it is often useful to make a copy before performing operations that fold, spindle, or mutilate lists.

A slice operator on the left side of an assignment can update multiple elements:

Q-5: What is printed by the following statements?

  • The list begins with the second item of L and includes everything up to but not including the last item.
  • Yes, there are 3 items in this slice.

IMAGES

  1. Programming for Everybody (Getting Started with Python)

    assignment 9.4 python for everybody

  2. Coursera: Python For Everybody Complete Course Assignments Solution |Python For Everybody Assignment

    assignment 9.4 python for everybody

  3. Python for Everybody

    assignment 9.4 python for everybody

  4. exercise 5. 2 python programming 4 everybody

    assignment 9.4 python for everybody

  5. Coursera:Python for Everybody Peer-Graded Assignment Solution

    assignment 9.4 python for everybody

  6. Python Tutorial

    assignment 9.4 python for everybody

VIDEO

  1. Python Assignment Operators || ( /= ), ( %= ), ( //= ) and ( ** = )

  2. Python Assignment Operator #coding #assignmentoperators #phython

  3. python assignment operator

  4. Assignment 9.4 Python Data Structures

  5. 3#Assignment Operators in Python #pythonprogramming #computerlanguage

  6. Python Week 1 Graded Assignment(IITM)

COMMENTS

  1. python-for-everybody/wk9

    wk9 - assignment 9.4.py. Cannot retrieve latest commit at this time. History. Code. 37 lines (26 loc) · 1.02 KB. __author__ = 'edwardlau' """ 9.4 Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines ...

  2. ex 9.4 python coursera

    Currently working on this for Coursera's Python For everybody class. This is ex 9.4. For some reason the emcount is double what it is supposed to be when ran. The problem seems to start early on since line has double the amount it should. ... One more thing, the assignment says to find all lines with "From ", not lines with "From". See the ...

  3. Python For Everybody Assignment 9.4

    Coursera: Python For Everybody Assignment 9.4 program solution | Graded Assignment 9.4 Python For EverybodyQ.) 9.4 Write a program to read through the mbox-s...

  4. Python for Everybody Answers

    The video is about the solution of the mentioned assignment of the python course named 'PYTHON FOR EVERYBODY' on coursera by Dr. Chuck

  5. 9.4. List operations

    9; Repetition does not multiply the lengths of the lists. It repeats the items. [1, 1, 1, 3, 3, 3, 5, 5, 5] Repetition does not repeat each item individually.

  6. Python Data Structures Assignment 9.4 Solution [Coursera ...

    Python Data Structures Assignment 9.4 Solution [Coursera] | Assignment 9.4 Python Data StructuresCoursera: Programming For Everybody Assignment 9.4 program s...

  7. Python for everybody assignment 9.4 in python 3 · GitHub

    Python for everybody assignment 9.4 in python 3. GitHub Gist: instantly share code, notes, and snippets.

  8. 9.4: Looping and Dictionaries

    This page titled 9.4: Looping and Dictionaries is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request. If you use a dictionary as the sequence in a for ...

  9. Interactive

    Python for Everybody - Interactive¶ Assignments¶ Assignments; Table of Contents ...

  10. Projects

    Exercise 9.2""" Exercise 9.2: Write a program that categorizes each mail message by which day of the week the commit was done. To do this, look for lines that start with "From", then look for the third word and keep a running count of each of the days of the week. At the end of the program, print out the contents of your dictionary (order does not matter).

  11. 9. Lists

    You have attempted of activities on this page | Back to topBack to top © Copyright 2020 - Based on Python for Everybody. Created using Runestone.Runestone.

  12. Assignment 9.4 Python Data Structures

    Hello EveryOne. Welcome to #mythoughts...-----Thanks for watching!!-----PythonData Structures Assignment9....

  13. PY4E

    Coursera: Python for Everybody Specialization; edX: Python for Everybody; FreeCodeCamp; Free certificates for University of Michigan students and staff; If you log in to this site you have joined a free, global open and online course. You have a grade book, autograded assignments, discussion forums, and can earn badges for your efforts.

  14. Python Data Structures Course by University of Michigan

    We will move past the basics of procedural programming and explore how we can use the Python built-in data structures such as lists, dictionaries, and tuples to perform increasingly complex data analysis. This course will cover Chapters 6-10 of the textbook "Python for Everybody". This course covers Python 3.

  15. 9.5. List slices

    A slice operator on the left side of an assignment can update multiple elements: Q-5: What is printed by the following statements? L = [0.34, '6', 'SI106', 'Python',-2] print (len (L [1:-1])) 2; The list begins with the second item of L and includes everything up to but not including the last item. ... Based on Python for Everybody.

  16. "Python for Everybody" Chapter 9

    Order the book on Amazon: https://amzn.to/3huCub6If you want to support the channel, any donation in PayPal helps: https://bit.ly/2Ss5i90Here is the code for...

  17. Coursera Python Data Structures Assignment 9.4 : r/learnpython

    I am having an issue with my output for assignment 9.4 in the Python Data Structures course on Coursera. I tried asking for help in the discussion forums over there but I still can't seem to narrow down exactly what is throwing the word count off. The program takes a file called mbox-short.txt and counts the most prolific email sender from the ...