Python101(Pt3)

Ace your homework & exams now with Quizwiz!

What keyword catches anything which isn't caught by the preceding conditions

'Else' keyword

What keyword is a logical operator, and is used to combine conditional statements

'and' keyword

What function is used to count the number of times an item occurs in a tuple?

.count ( )

What function is used to remove the last item inserted

.popitem( )

The range () function defaults to increment the sequence by ?

1

What loop is used for iterating over a sequence that is either a list, a tuple, a dictionary, a set, or a string?

A For Loop

What kind of loop is best for running a certain amount of code over a sequence that is given (ex. list, dictionary, string, group of numbers)

A For loop

What kind of loop is best for running while something is true code will be executed until it is no longer true? (Ex. as long as age is <71)

A While loop

What loop can execute a set of statements as long as a condition is true

A While loop

What defines the blueprint of an object

A class

What type of block will let you execute code, regardless of the result of the try- and except blocks

A finally block

What block type will let you test a block of code for errors?

A try block

What block type will let you handle the error?

An except block

While using the range () function how would you specify the increment value

By adding a third parameter (ex: (5, 15, 2) -> 5..7..9..11..13)

What is a.k.a the blueprint of the code?

Class

What provides storage location used by all methods in an instance of the class

Class variables

What defines a class or instance variable used to hold data associated with a class and its objects

Data member

What are examples of iterables but not sequences?

Dictionary, set and file objects

What keyword is pythons way of saying "if the previous conditions were not true, then try this condition"

Elif

What keyword can be used to define a block of code to be executed if no errors were raised?

Else

the ____ keyword in a 'for' loop specifies a block of code to be executed when the loop is finished

Else

What is another name for error in Python?

Exception

Errors detected during execution are called

Exceptions

If we pass A zero value to bool ( ) construtor, it will treat it as ?. Any non zero value is?

False True

True or False You can delete items from tuples

False. Tuples are unchangeable

What block , if specified , will be executed regardless if the try block raises an error or not?

Finally block

What block is useful for closing objects and cleaning up resources?

Finally block

what keyword is used in try...except blocks. It defines a block of code to run when the try...except...else block is final.

Finally keyword

What creates more than one version of a function (results in different behavior)

Function overloading

The while loops require an _________ variable

Indexing variable (ex. i = 1)

What does self mean?

It is a reference to the current instance of the class

why is self used?

It is used to access variables that belong to the class

What is an object capable of returning its members one by one

Iterables

What is an object representing a stream of data

Iterator

What are examples of the iterables that are sequences?

List, tuples, strings

A loop inside of a loop is called a

Nested loop

What keyword is used to throw an exception if a condition occurs?

Raise exception

Missing colons, semi-colons, indentation, parathesis or closing tags are examples of what kind of errors?

Syntax errors

a = 33 b = 200 if b > a: print("b is greater than a") Is an example of what statement?

The 'if statement'

What statement can stop the while loop even if the condition is true

The break statement

With what statement can we stop the loop before it has looped through all the items

The break statement

What statement can we stop the current iteration of the loop, and continue with the next

The continue statement

what statement can run a block of code once when the condition no longer is true:

The else statement

If you are not sure of the contents in a 'for' loop but would like to make a placeholder 'for' loop. What statement would you use?

The pass statement (ex: for x in (1, 2, 3): pass)

How are exceptions handled?

Try statements

Print each adjective for every fruit

adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)

What is the syntax for initializing a function?

def __init__ (self):

Print each letter in banana:

for x in banana print (x)

Print each fruit from the following list: fruits = ["apple", "banana", "cherry"]

for x in fruits print (x) -> apple banana cherry

Exit a list when x = banana

for x in fruits print (x) if x == "banana": break

Skip "banana" in the list: fruits = ["apple", "banana", "cherry"]

for x in fruits: if x == "banana": continue print (x)

Print all numbers from 0 to 5 then print a message when the loop has ended

for x in range (0, 5): print (x) else: print ("Finally Finished")

Increment by 3, starting at 2 to 30 using the range function:

for x in range (2, 30, 3): print (x)

Print values from 2 to 6 but not including 6 using the range () function

for x in range (2, 6) print x

Use the range () function to print 6 numbers

for x in range(6): print x

Print i as long as i is less than 6:

i = 1 while i < 6 print (i) i += 1

Continue to the next iteration if i = 3

i = 1 while i < 6: i+=1 if i == 3: continue print (i)

Print a message once condition ( i < 6 ) is false

i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6")

What uses a parent class to create a child class with the same characteristics each instance is unique

inheritance

An iterator can be made by applying what function to an iterable?

iter () function

What function can you use to loop through a set of code a specified number of times

range () function

The range () function defaults to ? as a starting value

range () functions starts at 0

Close a file that is not writeable and continue with the program using a finally block

try: f = open("demofile.txt") f.write ("Lorum Ipsum") except: print("Something went wrong when writing to the file") finally: f.close()

Using the 'else' keyword show a try block that does not generate any error

try: print("Hello") except: print("Something went wrong") else: print("Nothing went wrong")

Print one message if the try block raises a NameError and another for other errors:

try: print(x) except NameError: print("Variable x is not defined") except: print("Something else went wrong")

Use the finally block

try: print(x) except: print("Something went wrong") finally: print("The 'try except' is finished")

How would one raise a TypeError if x is not an integer?

x = "hello" if not type (x) is int: raise TypeError ("Only integers are allowed")

Raise an Exception if x is lower than 0

x = -1 if x < 0: raise Exception ("Sorry no numbers lower than zero")


Related study sets

Ecological Econ Final Example Questions

View Set

VTA130 Restraint for Sample Collection

View Set

fluid and electrolyte homeostasis and imbalances

View Set

Vet Science: Injection Techniques (questions)

View Set

Exam 3 (ch. 9,10,11) quiz questions

View Set

Chapter 71: Care of Pt with Gynecological Problems EAQ

View Set