PYTHON FINAL FUNDAMENTALS

Ace your homework & exams now with Quizwiz!

Match the following operators:

== Equal to != Not Equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to

According to the Schweigart (2019) textbook, select 6 things that a for statement that will always consist of?

A call to the range() function The in keyword a variable name for clause A colon The for keyword

What are the 4 things that a while statement will always consist of?

A condition (that is, an expression that evaluates to True or False) while clause The while keyword A colon

By default, the while loop is an:

A while loop is an entry controlled loop because in the while loop, the test condition is checked before entering the loop body.

What is the % operator in Python?

Modulus

If a dictionary is defined as: info = {"name":"Sandy", "age":17} The expression, info.get("hobbies", None), evaluates to:

None

What value does a Python function return if no return statement is specified in the function?

None

What is the purpose of the break statement?

Stop a loop before it has looped through all the items

Which of the following are immutable data structures? (select all that apply)

Strings, tuples

Given the following variable: studentGrades = [10, 10, 10, 85, 85, 90, 100, 100,100] Write the code that would return the sub list for the 4th through the last grade in the list.

studentGrades[-4:]

Write the code to find the second item in a list called, studentInfo.

studentInfo[1]

What two statements can be used to handle exceptions in our Python code?

try, except

Name an immutable datatype we've discussed in class (notes, readings, in-class discussion, etc)

tuple Tuple string String

Type the Python function that shows the data type of a variable named, var2. Don't include spaces in your response.

type(var2)

An expression can always evaluate down to a single:

value

Fill in the blanks to complete the definition of an expression: An expression in Python consists of

values and operators and can alwauys evaluate down to one value

Given the following code, fill in the correct answers in the notes to describe whether a variable is global or local. x = "variable1" def example(): global x y = "variable2" #this is a variable x = x * 2 print(x) print(y) example()

x= local y = global

Given the following Python code: var1 = 25 * 2 var1 = var1 * 5 What will be the value of var1 when the code runs?

250

Given the following variable: studentGrades = [10, 10, 10, 85, 85, 90, 100, 100,100] What would be returned by the following code: studentGrades.index(85)

3

What does the expression, 23 // 7 , evaluate to?

3

Consider the following code segment: x = 5 y = 4 if x > y: print(y) else: print(x) What value does this code segment print?

4

Given the following Python code: spam = 40 spam + 1 What will be the value of spam when the code runs

40

Given the following Python code: spam = 40 spam = spam + 1 What will be the value of spam when the code runs?

41

Consider the following code segment: count = 5 while count > 1: print(count, end = " ") count -= 1 What is the output produced by this code?

5 4 3 2

Given the following Python code: var1 = 25 * 2 var1 * 5 What will be the value of var1 when the code runs?

50

data = "No way!" The expression len(data) evaluates to: The expression data[1] evaluates to: The expression, data[-1], evaluates to: The expression, data[3:6], evaluates to:

7 The expression data[1] evaluates to: 'o' The expression, data[-1], evaluates to: '!' The expression, data[3:6], evaluates to: 'way'

ype the Python function to evaluate the number of characters in a variable named, var1. Don't include spaces in your response.

len(var1)

Look up the append() function and select the general syntax for this function in the space below.

list.append()

Given the following variable: studentGrades = [10, 10, 10, 85, 85, 90, 100, 100,100] What would be returned by the following code: studentGrades[3]

8v5

What does Python return when the following function is executed? float('245')

245.0

What does Python return when the following function is executed? str(245)

'245'

We wish for Python to return the following: 'I have eaten 225 cans of spam' The following code will give us an error: 'I have eaten ' + 225 + 'cans of spam' Write the three correct values to return our desired string:

'I have eaten' + str(225) + 'cans of spam'

What does the following expression evaluate to: 'spam' * 3

'spamspamspam'

What does the following expression evaluate to: 'spam' + 'spamspam'

'spamspamspam'

Which of the following are operators and which are values?

* Operator - Operator 'hello' Value -88.8 Value - Operator / Operator + Operator 5 Value

Which of the following are examples of augmented assignment statements (select all that apply):

-= *= /= %= +=

What does the expression, 9 % 3, evaluate to?

0

What is the output for the following code: for count in range(5): print(count, end = " ")

0 1 2 3 4

Match the following inputs to outputs:

10 == 9 and 8==8 False '10' == 9 or 8==8 True 2 == 1 + 1 and not 10==2*5 or 5==2*(2+1) False

How many times does a loop with a header for count in range (10): execute the statements in its body?

10 times

Given the following variable: studentInfo = ['firstName', 'lastName', 'favBand', 'major'] What would be returned by the following code: studentInfo.index('favBand')

2

How many possible values does the Boolean data type have?

2

Given the following Python code, enter the value of the variable, buffs, after the code executes: buffs = 20 buffs + 1

20

Given the following Python code, enter the value of the variable, buffs, after the code executes: buffs = 20 buffs = buffs + 1

21

What does Python return when the following function is executed? int('245')

245

Consider the following code segment: theSum = 0.0 while True: number = input("Enter a number: ") if number == "": break theSum += float(number) How many times will the loop run?

At least once

Consider the following code segment: theSum = 0.0 while True: number = input("Enter a number: ") if number == "": break theSum += float(number) How many iterations does this loop perform?

At least one

What questions should you ask yourself to see if you want to use a list in Python (select all that apply)

Do you want an ordered list of something? Do you want to store the ordered list? Do you want to access the things in the list randomly or linearly?

By default, the while loop is an:

Entry controlled loop

What will the code below return given the list above, pizzatop. pizzatop = ['peperoni', 'cheese', 'pineapple', 'sausage', 'bell peppers', 'mushrooms'] cheese in pizzatop

Error code

In Python, what value would be returned by the following code? 99 == '99'

False

The range() function works with integers, float, and string data types.

False

True and True and True and False evaluates to:

False

What is the // operator in Python?

Integer Division/Floored Quotient

data = [10, 20, 30] The expression, data[1], evaluates to: The expression, data[1:3] evaluates to: The expression, data.index(20) evaluates to:

The expression, data[1], eva The expression, data[1:3] evaluates to: [20, 30] The expression, data.index(20) evaluates to: 1

Consider the following code segment: count = 1 while count <=10: print(count, end = " ") Which of the following describes the error in this code?

The loop is infinite

Consider the following code segment: count = 1 while count <=10: print(count, end = " ") Which of the following describes the error in this code?

The while loop is infinite because there is no operation performed on the count variable. So, the count value will always remain 1 and because of this the condition in the while loop is forever True and this leads to the infinite loop.

Select the data types we have discussed in class.

There are other datatypes but the ones we covered so far in class are: Strings, integers, floating-point, boolean.

In Python, what value would be returned by the following code? x = 10 y = 15 z= 5 x <= y and y > z

True

True and True and True or False evaluates to:

True

What will the code below return given the list, pizzatop? pizzatop = ['peperoni', 'cheese', 'pineapple', 'saussage', 'bell peppers', 'mushrooms'] 'cheese' in pizzatop

True

Match the following inputs to outputs

True and True True False and True False not not not True False True and True and True or False True True or False or True and True True True or False or True and True and not False True

Describe what would go in a, try, clause:

We put the code that could potentially have an error in the try clause. A common example of this is dividing by zero.

Describe what would be placed in an except clause:

We use the except clause to handle what happens if the code in the try clause returns an error.

If a dictionary is defined as: info = {"name":"Sandy", "age":17} The expression, list(info.keys()), evaluates to:

["name", "age"]

The expression, studentGrades[4] = 86, evaluates to:

[10, 10, 10, 86, 85, 90, 100, 100, 101]

After the statement, data.insert(1, 15), the original data evaluates to:

[10, 15, 20, 30]

The expression, data + [40, 50] evaluates to:

[10, 20, 30, 40, 50]

After the statement, data[1] = 5, data evaluates to:

[10, 5, 30]

Given the following variable: studentGrades = [10, 10, 10, 85, 85, 90, 100, 100,100] Write the sub list that would be returned by the following code: studentGrades[2:5]

[10, 85, 85]

Given this Python code: fruits = ['apple' , 'banana', 'cherry']def fruit(food):for x in food:print(x)What will be the output when this next line of code is executed? fruit(fruits)

apple banana cherry; printed vertically on separate lines

When the function range receives two arguments, what does the second argument specify?

b. The last value of a sequence of integers plus 1

Write Python code to define a function called, multiply, that will multiply two numbers. Use two parameters in your function named: a and b To indent in the essay box, use two spaces for each indention

def multiply(a, b): return a * b

A Boolean expression using the and operator returns True when:

both operands are true

A for loop is convenient for (select all that apply):

counting through a sequence of numbers. running a set of statements a predictable number of times.

Select the choices that are valid variable names:

currentBalance account5 _100 balance

Write a function called, add, that will add two numbers together. Use two parameters in your function named: a and b To indent in the essay box, use two spaces for each indention Your Answer:

def add(a,b): return a+b

Write the first line of code to create a new function called, hello, with two arguments, a and b.

def hello(a,b)

Write the first line of code to create a new function called, hello, with two arguments, a and b. Only include the minimum number of spaces for the syntax to be correct.

def hello(a,b):

Write Python code to define a function called, subtract, that will subtract two numbers. Use two parameters in your function named: a and b To indent in the essay box, use two spaces for each indention Your Answer: def subtract(a,b):return a-b

def subtract(a, b): return a - b

Which of these are assignments and which are expressions?

eggs = 2 assignment 2 expression ham = 5 + 8 assignment 5 + ham expression 2 + 2 expression

What are the three main flow control statements?

else, elif, if

The expression, data.find("way!"), evalutes to:

find() will return the index position of the first character in the string that is being searched. In this case, "way!" is found in the string and therefore 3 is returned because it is the first index position of 'w' in the matched string.

Select the correct Python function to convert a value to a floating-point number. We only want the general function without any values passed to it Don't add any spaces when writing the function

float()

Write the line of code to import a Python module called, pandas.

import pandas

Type the first line of code to import the pandas module and alias it as pd.

import pandas as pd

Write the code to import the random module

import random

Select the correct Python function to convert a value to an integer: We only want the general function without any values passed to it Don't add any spaces when writing the function

int()

The values inside a list are also called .

items elements

A list is a value that contains multiple values in an sequence.

ordered

Describe what it means to "pass" values to a function?

passing in values called arguments when you input something into your function

Type the code that will read in a csv file given below to a pandas DataFrame. Assume the file is in the same working directory as your jupyter notebook so that you don't need to include the entire path of the file - pass the file name to the DataFrame function. File name: students.csv Use single quotes around your file Use the common alias for the pandas module in your code

pd.read_csv('students.csv')

Type the code (must be exact) to display "Hello World" on the screen:

print('Hello World')

Write the Python code to call the print function and pass the value 'Welcome to Python!' to it.

print('Welcome to Python!')

Fill in the blanks for the general syntax (discussed in class) for the range() function: range( start ,

range(start, stop, step)

Write the one line of code that would be within a function to return the value of the quotient of two numbers, a and b.

return a/b

Fill in the blanks to complete the definition of the the range() function discussed in class: The range() function returns a

sequence of numbers, starting from 0 by default and increments by 1 by default and ends at a specified number

Which of the following are variables and which are strings?

spam Variable 'spam' String 'eggs' String ham Variable

Select the correct Python function to convert a value to a string. We only want the general function without any values passed to it Don't add any spaces when writing the function

str()

Type the code that would return a tuple giving you the number of rows and columns in a DataFrame. Assume the DataFrame was assigned to a variable named, stu For example, the tuple returned should look like: (1000, 10)Where 1000 is the number of rows and 10 is the number of columns

stu.shape


Related study sets

Chapter 1: History, Theory and Research Strategies

View Set

Chapter 30 - Environmental Emergencies

View Set

Micro econ old tests, quizzes, and homeworks

View Set

Social Studies (The Spanish American War and Florida (mid 1800s))

View Set

Entrepreneurship Unit 4 Study Guide

View Set