TEST 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

name = "Emily" print( name[3] )

"l" Index numbers start from 0. Index 3 will be the fourth character.

Unlimited Parameters To specify unlimited number of parameters in a function, use _____

( *lots_of_parameters )

specify unlimited parameters using an

* (means many)

Range (4) will give the range of numbers:

0, 1, 2, 3

Range (0, 5, 2) will give the range of numbers ___.

0, 2, 4

how many times can you return

1

how many values can a function return

1

What is the output? number1 = 5 number1 = 10 #number1 = 2 print (1 + number1)

11

What numbers would be generated by range(11, 5, -2)?

11, 9, 7

a boolean value can have up to __ different values

2

number1 = "2.5" number2 = "40" print(number1 + number2)

2.540 The values are enclosed in quotes. Therefore, they are strings. Plus operator does concatenation.

number_list = [5, 10, 15, 20] print(number_list[3]) What would be the output of the statements above?

20

line = "Mary had a little lamb" partitions = line.partition(" ")

3

numbers = ​[3, 2, 1] for i in range (0, len(numbers) ): print( numbers ​[i] ) What would get printed out?

3 2 1

list_of_numbers = [ 34, 56, 67, 53, 24] print( list_of_numbers[ 2: ] ) Which numbers would be selected?

67, 53, 24

How many numbers would be generated by the following range? range (0, 7, 1)

7

def power(base, exponent): base = 2 exponent = 3 return base ** exponent result = exponent ** base return result answer = power (3, 2) print(answer)

8

number_list = [5, 10, 15, 20] print(number_list[0] + 4) What would be the output of the statements above?

9

2 places of decimals as a float

:.2f

Multiple Return Values Which of the following are true about return values?

A function can return only one value, which can in turn contain multiple values

dictionaries

A structure that stores values alongside different keys {} complex data

Which of the following is the ideal candidate for using dictionaries? A series of scores for an uncertain number of students Data about one student Data about a number of students Series of logically related values that can be described with a name or a lame

Data about one student Series of logically related values that can be described with a name or a label

Variables

Do not have data types

When looping through a dictionary

Focus is always the key

person = {"name": "Fred", "id": "23423" } for each_item in person: print( person​[each_item] )

Fred 23423

Values

Have data types

For the code below, what would be printed out? person = {"name": "John", "city": "Chicago",} for each_item in person: print(person[each_item])

John Chicago

it is possible to give arguments out of order using...

KWARGS

title = "Netflix and Chill" print ( title​​[0:3] )

Net

==

The equality operator (sometimes read: "equal equal") is used to compare two values, and returns a Boolean (true/false). Avoid confusion with the assignment operator "=",

A TUPLE _____, whereas a LIST ____.

cannot be modified , can be modified

union with sets

combined_set = set1.union(set2)

count down from 5 to 1

count down from 5 to 1 i - integer for index numbers for i in range(5, 0, -1): print(i)

Which of the following keywords creates a new function?

def

define a function using...

def KWARG

Assume, you have the following data about one Amazon product: Amazon ID Number, Product Name, Product Rating, Number of Reviews What data structure is this data best suited to?

dictionary

structure of a dictionary

dictionary_name = {"Name" : "Selena", "City" : "PhillY}

Remove a key (essenially removes key and value from dictionary)

dictionary_name.pop()

find the duplicates in sets

duplicated_items = set1.intersection(set2)

To link multiple if conditions, use an ___

elif

You can test multiple conditions in Python conditional statements using __ keyword

elif

What is the output? number1 = 6 number2 = 11 print (Number1 + number2)

error

name = "Smurfette" print (name[4:]) What would be the output of the above set of statements in Python?

fette

34.23

float

to print out keys in dictionary

for each_key in person.keys(): print(each_key)

to print out values in dictionary (two ways)

for each_value in person.values(): print(each_value) for each_key in person: print(person[each_key])

any type of repeating code

function

To test if a character in contained within a string, the best and easiest way is to use the keyword ___.

in

arguments are copied into parameters...

in sequence

ask for answer

int(input("string"))

When a function returns a value, it ______.

is given back to the caller function invisibly

dictionaries have

key value pairs

To find out how many characters a cell text value has, use ___.

len()

to create an empty list

list = list()

to add a dictionary to the list making it a list of dictionaries

list.append(x)

If you want to be able to have items in a data structure ordered by index numbers and be able to remove some elements later on as well, you would choose ____.

lists

lists

lists - uses [] can be modified, changes, updates, delete elements

movie = "Lord of the Rings" How would we extract "Lord" from the string?

movie​[:4] movie​[0:4]

Which of the following would erroneous in Python?

my_tuple = (1, 3, 2) my_tuple.append( 5)

person = {"name": "John", "city": "Chicago",} for each_item in person: print(each_item)

name city

string slicing

name = 'Matthew Andrews' # print only last name print(name[7:])

Which of the following properly sets up a dictionary?

name = { 'name': 'Adam', 'id': '2300' }

Should the return key word be in a loop?

no

Coding starts on the right hand side: Power function is passed number1 and number2 as arguments arguments get copied into the parameters function does computation and returns a value the function "EVALUATES to" the return value that return values is store in the left hand side variable

number1 = 3 number2 = 2 computed_value = power(number1, number2) print(computed_value)

Which of the following would generate an error? numbers = ​[1, 2, 3] numbers.append(4) numbers = (1, 2, 3) numbers.append(4) numbers = ​[1, 2, 3] numbers.remove(3) numbers = (1, 2, 3) numbers.remove​[3]

numbers = (1, 2, 3) numbers.append(4) numbers = (1, 2, 3) numbers.remove​[3]

average in a list

numbers = [1, 2, 3, 4, 5] total = 0 for each_number in numbers: total = total + each_number print(f" After adding, {each_number}, the running total is {total}") average = total / len(numbers) print(f'The average is {average:.2f}')

Which of the following creates a set?

numbers = {1, 2, 3}

def function_name ( *lots_of_values ):lots_of_values is a _____.

parameter

define a function with an argument

parameters

partitions

partition is for dividing strings into 3 parts partitions = string.partition()

person = {"name": "John", "city": "Chicago",} To get a list containing "name" and "city", use ___.

person.keys()

person = {"name": "John","city": "Chicago",} To get a list containing "John" and "Chicago", use ___.

person.values()

phrase = "AZ AR AK CO IL WA PA" The ___ function call returns a total of 3 elements in Python.

phrase.partition(" ")

Using the following code, how will you print only the name? data = { 'name': 'Adam', 'id': '2300' }

print ( data​['name'])

acsess one value

print(dictionary_name[key_name])

Which of the following is a correct way of using format strings or f-strings?

print(f"Hello {name}")

Which of the following generates range numbers from 0 to 4 (inclusive of 4)?

range (0, 5) range(5)

To get numbers 4, 2, 0 use:

range (4, -1, -2)

numbers = ​[1, 2, 3] for i in _________: print( numbers ​[i] )

range( 0, len(numbers) ) range( len(numbers) )

For ranges

range(5) for each_number in range(5): print(each_number) print number from 1- 10 skipping 3 step value of 3 for each_number in range(1, 11, 3): print(each_number)

parameters are: receive value sending value

receive value

To remove one item from a list, use the method ____.

remove

what side does coding start on

right hand side

superset VS subset # does set one contain set two in its entirety

set 1 is a superset of set 2 set 2 is a subset of set 1 if set1.issuperset(set2): print('set 1 containes set 2') if set2.issubset(set1): print('set 2 is contained within set 1')

update set

set1.update(set2)

You are importing customer data from 4 sources. If you want a non-repetitive listing of only those states that your customers are in, you should import or copy customer data into ____.

sets

split

splits into multiple parts between separators split = phrase.split()

How do we get the character with index 4 from the string? star = "Maverick"

star​[4]

state = "Pennsylvania" Which of the following will return the substring "Penn" in Python?

state[0:4]

state = "Massachussetts" Which of the following will return the substring "Mas" in Python?

state[:3]

=

stores value/ assigns value assignment operator

'34.23'

string

The type for the value of "99.32" is

string

In Python to extract leading/trailing spaces, use ___.

strip

name = Peter print ( name​[2:] )

ter

list of products = ​[ { 'product': 'Paper', 'id': '243098', 'rating': '4.5' }, { 'product': 'Pens', 'id': '7678', 'rating': '3.2' },{ 'product': 'Markers', 'id': '43263', 'rating': '3.8' } ] for each_product in list_of_products: <tab> print( each_product ) What will the for loop print in the first iteration? (first time it loops)

the first dictionary literal

Kwarg The purpose of keyword arguments is _____.

to allow specification of arguments out of order by using argument names

A set can have elements added to it.

true

tuples

tuples- use () typically functions use them for returning vaules they cannot be modified, read only

Two sets are being joined, Set 1 and Set 2. What method would join these sets such that it includes all elements of Set 1 and Set 2?

union

How would we find out if an item is contained in a list?

use an "if" with an "in"

Parameters allow you to...

write code incomplete based on future values and variables

Can you modify sets?

yes add.() remove.()

to remove duplicates from a list...

you can make it a set set_name = set(list_name) overwrite original list list_name = list(set_name) print(list_name)


Ensembles d'études connexes

ATI Chapter 11: Infection Control Application Exercise

View Set

Chapter 49 - Listening Guide Quiz 41: Debussy: Prelude to "The Afternoon of a Faun"

View Set

Perry Chapter 30: Pain Assessment and Management in Children

View Set

Basic IUPAC Organic Nomenclature

View Set

Endocrine and Metabolic Disorders

View Set