Untitled Quiz
23 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be the output of the following code? total = 0 for count in [1,2,3]: total = total + count print(total)

  • 6 (correct)
  • 5
  • 1
  • 1 4

Which of the following correctly describes a list in Python?

  • A comma-separated sequence of data items enclosed in square brackets. (correct)
  • A collection of items that prevents duplicates.
  • A variable holding a single value.
  • A sequence of items enclosed in curly braces.

What does the following code return? def pass_it(x, y): z = str(x) + ", " + str(y) answer = pass_it(4, 8) print(answer)

  • 48
  • None (correct)
  • 8, 4
  • 4, 8

What is the valid index for extracting 'New York' from the string: city = 'New York city'?

<p>All of the above (D)</p> Signup and view all the answers

In Python, what will the expression phones['John'] evaluate to after this code executes? phones = {'John' : '5555555', 'Julie' : '5557777'} phones['John'] = '5556666'

<p>'5556666' (B)</p> Signup and view all the answers

Which of the following statements about dictionaries is true?

<p>Dictionaries are not indexed by number. (D)</p> Signup and view all the answers

What is the correct way to read data from the keyboard in Python?

<p>input() (B)</p> Signup and view all the answers

What will happen if line 1 is removed, and 'divTags' is replaced with 'soup' in lines 2 and 3?

<p>The program will still produce the same dataframe df. (D)</p> Signup and view all the answers

What is the purpose of the 'try' and 'except' block in the error handling code?

<p>To append 'NA' if a description does not exist. (B)</p> Signup and view all the answers

What will the print statement inside the 'for' loop located at line 6 display?

<p>The tag objects for each food item. (D)</p> Signup and view all the answers

What is the outcome of the provided code snippet that extracts the first tag?

<p>It will not find any tag due to the wrong attribute name. (D)</p> Signup and view all the answers

Given the URL format for searching on Amazon, which aspect needs to change to create a search link for 'data science'?

<p>Only the keyword after '?k=' should change. (D)</p> Signup and view all the answers

How does the 'p.get_text().strip()' function assist in data cleaning?

<p>It removes unnecessary whitespace from the content. (A)</p> Signup and view all the answers

Why might a data scientist use a loop to create search URLs for Amazon?

<p>To generate a unique URL for each search term systematically. (B)</p> Signup and view all the answers

What will the variable 'data1' store after lines 4 to 11 are executed?

<p>A dictionary with food and descriptions as keys. (C)</p> Signup and view all the answers

Which code correctly extracts the value of the class attribute of all the

tags in the given html_code?

<p>for p in soup.find('div').find_all('p'): print(p.attrs['class']) (A)</p> Signup and view all the answers

What is the output of the code 'soup.find_all(class_="food", limit=1)' in relation to extracting

tags?

<p>It retrieves the first <p> tag with class equal to 'food'. (C)</p> Signup and view all the answers

Which statement is true about 'soup.find('h1', id="top", class="food")'?

<p>It retrieves the <h1> tag if id is 'top' and class is 'food'. (C)</p> Signup and view all the answers

How many

tags will 'soup.body.find_all("p", recursive=False)' return?

<p>1 (D)</p> Signup and view all the answers

What does the code 'soup.find_all("p", string="Cauliflower Fritters")' do?

<p>It retrieves <p> tags with 'Cauliflower Fritters' as their string content. (A)</p> Signup and view all the answers

Which code does not print the

tag object that contains 'Cauliflower Fritters'?

<p>soup.find_all(class_='food', limit=1) (C)</p> Signup and view all the answers

What is the expected output from 'print(soup.find("h1", attrs ={ "id":"top" ,"class":"sister" }))'?

<p>None (A)</p> Signup and view all the answers

Which of these statements describes the structure of the HTML provided?

<p>The HTML includes <h1>, <p>, and <div> tags collectively. (A)</p> Signup and view all the answers

Flashcards

Python Math Expressions

In Python, math expressions are evaluated following the order of operations (PEMDAS/BODMAS), not strictly from left to right.

Loop for Repetition

A loop in Python allows you to execute a block of code multiple times, efficiently repeating a task.

Function Arguments

Functions in Python can accept multiple arguments, allowing flexibility and code reusability.

String Concatenation

Joining two strings together using the '+' operator in Python.

Signup and view all the flashcards

'if' Clause for Comparison

The 'if' clause in Python lets you execute code only if a certain condition is true, allowing conditional logic.

Signup and view all the flashcards

Input Function in Python

The 'input()' function reads text typed by the user from the keyboard and returns it as a string.

Signup and view all the flashcards

List in Python

A list in Python is a collection of items enclosed in square brackets, where each item can be of different datatypes and accessed by index.

Signup and view all the flashcards

Dictionary in Python

A dictionary in Python is a data structure that stores key-value pairs, allowing you to retrieve information based on specific keys.

Signup and view all the flashcards

BeautifulSoup for HTML Parsing

BeautifulSoup is a Python library used to extract data from HTML and XML content. It makes it easy to navigate and manipulate HTML structures.

Signup and view all the flashcards

.find_all() for Multiple Matches

The find_all() method in BeautifulSoup finds all elements that match a given criteria, returning a list of matching tags.

Signup and view all the flashcards

.get_text() for Text Content

The .get_text() method extracts the text content of an HTML tag, removing any HTML tags and whitespace.

Signup and view all the flashcards

Error Handling with try-except

The try-except block allows you to handle potential errors in your code, gracefully handling unexpected situations.

Signup and view all the flashcards

Appending Data to Lists

The .append() method adds an item to the end of a list, growing the list's size.

Signup and view all the flashcards

Pandas DataFrame

A Pandas DataFrame is a powerful data structure in Python, representing data as rows and columns, similar to a spreadsheet.

Signup and view all the flashcards

.strip() for Clean Text

The .strip() method removes leading and trailing whitespace from a string, producing clean text.

Signup and view all the flashcards

URLs with Search Keywords

Websites like Amazon use URLs that include search keywords to filter their results. The pattern often includes the search term within the URL using a key like 'k='.

Signup and view all the flashcards

Soup.h1

This code extracts the first 'h1' tag from the html_code using BeautifulSoup.

Signup and view all the flashcards

Soup.find('h1')

This code finds all 'h1' tags within the HTML code using BeautifulSoup.

Signup and view all the flashcards

Soup.find_all('h1')

This code finds all 'h1' tags within the HTML code using BeautifulSoup.

Signup and view all the flashcards

Soup.find(id='top')

This code finds the first tag with the attribute 'id' set to 'top' using BeautifulSoup.

Signup and view all the flashcards

Soup.find('h1', id='top', class='food')

This code searches for the first 'h1' tag with both 'id' set to 'top' and 'class' set to 'food'.

Signup and view all the flashcards

Soup.find_all('div').find_all('p')

This code finds all 'p' tags within all 'div' tags using BeautifulSoup.

Signup and view all the flashcards

Soup.find('div').find('p')

This code finds the first 'p' tag within the first 'div' tag using BeautifulSoup.

Signup and view all the flashcards

Soup.body.find_all('p', recursive=False)

This code finds all 'p' tags directly within the 'body' tag, not including any nested 'p' tags.

Signup and view all the flashcards

Study Notes

True or False Questions

  • Math expressions in Python are not always evaluated from left to right. They follow operator precedence rules.
  • Repeating operations in Python use loops.
  • Python supports multiple arguments.
  • Code snippet displaying "yes + no" is incorrect.
  • Code checking if choice is not 10 is correct.

Multiple Choice Questions

  • The input() function gets input from the keyboard.
  • input() returns a string.
  • Output of print(valuel * value2) is 24.0 , given valuel = 2.0 and value2 = 12.
  • A comma-separated sequence of data items enclosed in square brackets is a list.
  • Output of total = 0; for count in [1, 2, 3]: total = total + count; print(total) is 6.
  • Output of def pass_it(x, y): z = str(x) + ", " + str(y); num1 = 4; num2 = 8; answer = pass_it(num1, num2); print(answer) is "4, 8".
  • Valid index values for extracting 'New York' from 'New York city' include 0:8, :8, and 0:-5.
  • Dictionaries are not indexed by number.
  • Output of phones = {'John':'5555555','Julie':'5557777'}; phones['John']='5556666'; print(phones) is {'John':'5556666','Julie':'5557777'}.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Untitled Quiz
6 questions

Untitled Quiz

AdoredHealing avatar
AdoredHealing
Untitled Quiz
37 questions

Untitled Quiz

WellReceivedSquirrel7948 avatar
WellReceivedSquirrel7948
Untitled Quiz
55 questions

Untitled Quiz

StatuesquePrimrose avatar
StatuesquePrimrose
Untitled Quiz
50 questions

Untitled Quiz

JoyousSulfur avatar
JoyousSulfur
Use Quizgecko on...
Browser
Browser