Python Study guide week 4

Ace your homework & exams now with Quizwiz!

What is a 'try block'?

A block of code in which an exception is expected to be thrown.

What is an 'except block'?

A block of code to handle an exception that has been caught

What happens if you try to access a value in the dictionary using a key that does not exist?

An exception is thrown

How are values in a dictionary accessed?

By putting the key inside square brackets, like a list.

How are values in a dictionary accessed?What happens if a key already exists in the dictionary and an attempt is made to store a new value using the same key?

By putting the key inside square brackets, like a list.

How are values added to a dictionary?

By using square brackets, a key and the assignment operator.

How can an item be deleted from a dictionary?

By using the del statement.

How can you determine if a key exists in the dictionary?

By using the in operator to test for existence of the key.

What can you do to prevent run-time exceptions from crashing your program?

Implement try/except blocks anywhere a runtime exception might be possible.

What is the purpose of the 'else' clause in a try/except construct?

It holds code that should only execute when an exception has not been thrown

What does the term, "throw an exception" refer to?

It means that the normal execution of code has been disrupted by an error condition.

What does the keys() dictionary method do?

It produces an object with a list of keys in the dictionary.

What code should be placed inside a try block?

Just the code that could be expected to throw an exception.

What happens if a key already exists in the dictionary and an attempt is made to store a new value using the same key?

The original value is replaced by the new value.

How can an except block be defined to capture specific exceptions?

The particular kind of runtime exception, such as ValueError, can be specified in the except statement.

What happens if your program does not catch an exception?

The system catches the exception and the program terminates immediately

How do you access a value within a dictionary?

Using square brackets and a key.

What is an important requirement of Secure Programming?

Writing programs that perform input validation and that catch and handle runtime exceptions.

Does all the code in a try block execute if no exception is thrown?

Yes

Which of the following data types may be values in a dictionary?

an integer a floating point number a string (any of these)

How many times can the same value appear in a dictionary?

any number of times

In other programming languages, the dictionary data structure is referred to as a(an)

associative array hash hash table all of the above!!!!!

Dictionaries are created using

curly braces.

If you want to have access to all of the functions within the random module, what command would you use?

import random

Keys in a dictionary

must be unique

The randint function is part of the random module. Given the following import statement, how would you call randint? from random import randint

value = randint(1,10)

The randint function is part of the random module. Given the following import statement, how would the randint function be called: import random

value = random.randint(1,10)

What is the syntax for items within a dictionary?

A collection of key/value pairs inside curly braces, each separated by a comma. With each key/value pair, the key comes first, then a colon, then a value.

What happens when an event disrupts the normal flow of a program?

An exception is thrown

What is one way in which a hacker can compromise a system via a programming application?

Cause the appication to crash by entering invalid input.


Related study sets

Lab 21- Muscles of the Chest, Shoulder, and Upper Limb

View Set

Heteronormativity & Queer Engagements

View Set

Marketing Test Ch 20, 13, 9, & 16

View Set

A & P Lecture Exam #4 (Questions)

View Set