Instantly share code, notes, and snippets.

@sdmuzel

sdmuzel / Assignments 8.5.txt

  • Download ZIP
  • Star 0 You must be signed in to star a gist
  • Fork 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 sdmuzel/d72636240d0631e567c6d981dfdbc5a0 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
  • 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

8.5: List Slices

  • Last updated
  • Save as PDF
  • Page ID 3136

  • 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}}}} \)

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.

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:

Python Ex 8.5: in python4e

Hay , hope you are doing well,

why

it’s right answer for that quiz, but I feel that the quiz asked for a specific answer need help , and the line : if len(words) == 0 : continue can u tell me another way to do the same ?

link of quiz : https://www.py4e.com/tools/pythonauto/?PHPSESSID=f42fa792ce788dcae11b092ae158c67e

:cry:

Well, the most similar to continue is pass However there are differences, continue transfers the control of the loop to the beginning. While pass is a definitive pass (ignore the failed loop) There is another commonly misunderstood, break is the contrary to pass if the condition in the loop is true, then it will break the flow of execution. For more info:

Hope this helps

:blush:

Hi @Mahmmmoud ,

You could streamline your code by first finding a line that starts with 'From ’ and then splitting and printing words[1].

Hope this helps!

:star_struck:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.

python for everybody assignment 8.5

  • 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
  • Peer Instruction: Files Multiple Choice Questions
  • Mixed-up Code Questions
  • Peer Instruction: Text Files Multiple Choice Questions
  • 8.1 Persistence
  • 8.2 Opening files
  • 8.3 Text files and lines
  • 8.4 Reading files
  • 8.5 Searching through a file
  • 8.6 Letting the user choose the file name
  • 8.7 Using try, except, and open
  • 8.8 Writing files
  • 8.9 Debugging
  • 8.10 Summary
  • 8.12 Multiple Choice Questions
  • 8.13 Mixed-up Code Questions
  • 8.14 Write Code Questions
  • 8.15 Group Work: Reading from Files
  • 8.16 Group Work: Reading from CSV Files
  • 8.17 Group Work: Using a CSV reader/writer
  • 8.3. Text files and lines" data-toggle="tooltip">
  • 8.5. Searching through a file' data-toggle="tooltip" >

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

8.4. Reading files ¶

While the file handle object does not contain the actual data in the file, it is quite easy to construct a for loop to read through and count each of the lines in a file:

We can use the file handle object in a for loop. Our for loop simply counts the number of lines in the file and prints them out. The rough translation of the for loop into English is, “for each line in the file represented by the file handle object, add one to the count variable.”

The reason that the open function does not read the entire file is that the file might be quite large with many gigabytes of data. The open statement takes the same amount of time regardless of the size of the file. The for loop actually causes the data to be read from the file.

When the file is read using a for loop in this manner, Python takes care of splitting the data in the file into separate lines using the newline character. Python reads each line through the newline and includes the newline as the last character in the line variable for each iteration of the for loop.

Because the for loop reads the data one line at a time, it can efficiently read and count the lines in very large files without running out of main memory to store the data. The above program can count the lines in any size file using very little memory since each line is read, counted, and then discarded.

Q-2: When using a for loop to read lines, how many lines are read at a time?

If you know the file is relatively small compared to the size of your main memory, you can read the whole file into one string using the read method on the file handle.

In this example, the entire contents (all 94,626 characters) of the file mbox-short.txt are read directly into the variable inp . We use string slicing to print out the first 20 characters of the string data stored in inp .

When the file is read in this manner, all the characters including all of the lines and newline characters are one big string in the variable inp . It is a good idea to store the output of read as a variable because each call to read exhausts the resource:

Remember that this form of the open function should only be used if the file data will fit comfortably in the main memory of your computer. If the file is too large to fit in main memory, you should write your program to read the file in chunks using a for or while loop.

Put the following code in order so that it uses a for each loop to read the file and print out the total number of lines.

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 .

Search This Blog

Ashvini sharma.

Hello dosto! Welcome to my Blog . I am so happy to visit my blog. In this blog you can find your maximum information as like related to Coursera in, you find all quiz and assignment weekly as your course embedded.

Python data structures: Assignment 8.5

Post a comment.

Comments here for more information.........

Popular posts from this blog

Python data structures: assignment 7.1, python data structure: assignment 8.4, programming for everybody (python) assignment 5.2, coursera:web application technologies and django.

Image

Python data structures Chapter 6 Quiz

Image

IMAGES

  1. Python For Everybody Assignment 8.5

    python for everybody assignment 8.5

  2. [PDF] Download Python for Everybody by Charles Severance Book pdf

    python for everybody assignment 8.5

  3. "Python for Everybody" Chapter 8

    python for everybody assignment 8.5

  4. Programming for Everybody (Getting Started with Python)

    python for everybody assignment 8.5

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

    python for everybody assignment 8.5

  6. Python for Everybody

    python for everybody assignment 8.5

VIDEO

  1. Assignment

  2. ЕГЭ по информатике

  3. Assignment

  4. Assignment 9.4 Python Data Structures

  5. Python Programming Exercises: Personal Information

  6. Assignment

COMMENTS

  1. python-for-everybody/wk8

    wk8 - assignment 8.5.py. Cannot retrieve latest commit at this time. History. Code. 15 lines (15 loc) · 470 Bytes. fname = input ("Enter file name: ") if len (fname) < 1 : fname = "mbox-short.txt" #opening the file fh = open (fname) count = 0 #to store the lines data= [] for each in fh: # To check whether the line have more than two elements ...

  2. python-for-everybody/8_exercise_8_5.py at master

    When you find a line that starts with 'From ' like the following line: From [email protected] Sat Jan 5 09:14:16 2008 You will parse the From line using split () and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.

  3. Python For Everybody Assignment 8.5

    Code link: https://drive.google.com/file/d/1FW2C6XQRPrsMvR0SkIWYMaoVwu476AXR/view?usp=sharingCoursera: Python For Everybody Assignment 8.5 program solution |...

  4. Python Data Structures Assignment 8.5 Solution [Coursera ...

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

  5. 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

  6. Pyhton for Everybody

    Pyhton for Everybody - Assignments. Open the file mbox-short.txt and read it line by line. You will parse the From line using split () and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end. Hint: make sure not to include the lines that start with 'From:'.

  7. 8.5: List Slices

    So if you omit both, the slice is 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: 8.5: List Slices. The slice operator also works on lists.

  8. 8.5. Searching through a file

    Activity: 8.5.3 Multiple Choice (file-search-mc-rstrip) As your file processing programs get more complicated, you may want to structure your search loops using continue. The basic idea of the search loop is that you are looking for "interesting" lines and effectively skipping "uninteresting" lines. And then when we find an interesting ...

  9. GitHub

    This contains all the practices for the lectures, custom answers to the assignments and additional inline notes for "Python for Everybody Specialization" on Coursera by the University of Michigan. 41 stars 54 forks Branches Tags Activity

  10. Interactive

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

  11. Python for Everybody Specialization [5 courses] (UMich)

    Specialization - 5 course series. This Specialization builds on the success of the Python for Everybody course and will introduce fundamental programming concepts including data structures, networked application program interfaces, and databases, using the Python programming language. In the Capstone Project, you'll use the technologies ...

  12. Python Ex 8.5: in python4e

    Well, the most similar to continue is pass. However there are differences, continue transfers the control of the loop to the beginning. While pass is a definitive pass (ignore the failed loop) There is another commonly misunderstood, break is the contrary to pass if the condition in the loop is true, then it will break the flow of execution.

  13. Python/Assignment 8.5 at master · uniqueDevelop/Python · GitHub

    Coursera - Programming for everybody (Python). Contribute to uniqueDevelop/Python development by creating an account on GitHub.

  14. Programming for Everybody (Getting Started with Python)

    There are 7 modules in this course. This course aims to teach everyone the basics of programming computers using Python. We cover the basics of how one constructs a program from a series of simple instructions in Python. The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should ...

  15. PY4E

    Download course material as an IMS Common Cartridge®, to import into an LMS like Sakai, Moodle, Canvas, Desire2Learn, Blackboard, or similar. After loading the Cartridge, you will need an LTI key and secret to provision the LTI-based tools provided in that cartridge. If your LMS supports Learning Tools Interoperability® Content-Item Message ...

  16. coursera-python-for-everybody-specialization/Course_2_Python_Data

    Current repository contains all assignments, notes, quizzes and course materials from the "Python for Everybody Specialization" provided by Coursera and University of Michigan. - sersavn/...

  17. 8.4. Reading files

    6. Activity: 8.4.1 Opening and counting the lines in a file (fileLines) We can use the file handle object in a for loop. Our for loop simply counts the number of lines in the file and prints them out. The rough translation of the for loop into English is, "for each line in the file represented by the file handle object, add one to the count ...

  18. 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.

  19. "Python for Everybody" Chapter 8

    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...

  20. Python data structures: Assignment 8.5

    Coursera: Programming for everybody with Python Assignment 5.2 Week 7 Question:- 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put ...

  21. Python-For-Everybody-Specialization/Course-2-Python-Data ...

    Coursera's Python For Everybody Specialization Solutions - Python-For-Everybody-Specialization/Course-2-Python-Data-Structures/Week 4/assignment 8.5.py at master ...

  22. Python Data Structures Assignment 8.5 Solution [Coursera ...

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

  23. Assignment solutions for python for everybody

    Python 100.0%. Assignment solutions for python for everybody. Contribute to sweehors/python-for-everybody development by creating an account on GitHub.