Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output of the following code? nums = set([1,1,2,3,3,3,4,4]) print(len(nums))

4

What does the readlines () method returns?

A list of lines.

argument

A value passed to function when function is called.

my_dict[key]=value:

Adds an entry if the entry does not exist, else modifies the existing entry.

Procedural

Programs are lists of instructions that tell the computer what to do with the program's input.

OOP ( Object Oriented Programming )

Programs manipulate collections of objects. Objects support methods that query or modify in members of the object.

Expression

Python code that produces a value.

my_dict.get(key, default) :

Reads the value of the key entry from the dictionary. If the key does not exist in the dictionary, then returns default.

my_dict.clear() :

Removes all items from the dictionary.

my_dict.pop(key, default) :

Removes and returns the key value from the dictionary. If key does not exist, then default is returned.

key in my_dict:

Tests for existence of key in my_dict

open()

The key function for working with files in Python.

A function is a block of code which only runs when it is called.

True

A set contains an unordered collection of unique and immutable objects.

True

Comprehensions can be used to make Lists, Dictionaries and Sets.

True

Dictionaries are mutable

True

Dictionary Keys must be immutable.

True

Everything that works with a list works with a tuple except methods that modify the tuple.

True

If Python zip function gets no iterable elements zip(), it returns an empty iterator.

True

If you write to a file, you can only write a string

True

Key must be immutable

True

Lambda creates an anonymous function that is not bound to a specific name.

True

Lists are mutable, their values can be changed while strings are immutable

True

Lists can contain a mixture of any python object, but strings can only hold characters

True

No two elements are identical. Only one copy may exist in a set.

True

set

Unordered collection of unique elements.

file

a collection of data that is stored on secondary storage like a disk or a thumb drive.

Set

a collection of objects, of many different types.

Which of the following is the use of function in python? a) Functions are reusable pieces of programs b) Functions don't provide better modularity for your application c) You can't also create your own functions d) All of the mentioned

a) Functions are reusable pieces of programs

my_set.add("x")

adds to the set, no effect if item is in set already

Lambda

an anonymous function in python

What are the two kinds of data structures?

built-in data structures user-defined data structures

Which of the following commands will create a list? a) list1 = list() b) list1 = [] c) list1 = list([1, 2, 3]) d) all of the mentioned

d) all of the mentioned

built-in data structures

data structures that are so common as to be provided by default

my_set.clear():

emptys the set

max(lst):

largest element, again all must be the same type.

my_dict.copy :

makes an independent copy of my_dict.

user-defined data structures

(classes in object oriented programming) that are designed for a particular task

The normal relational equality rules are ...

-Numbers compare their value. -Strings compare ASCII/Unicode encoded values. -Lists compare element-by-element

my_dict1.update(my_dict2) :

Merges dictionary my_dict1 with another dictionary my_dict2. Existing entries in my_dict1 are overwritten if the same keys exist in my_dict2.

Is the following line of code valid? >>>a,b=1,2,3

No, too many values to unpack

If a mutable object such as a list is passed to a function, and that function modifies the object in place (for example, by appending to the list):

Any changes to the object are reflected in the main program, since it was modified in place.

Functional

Decomposes a problem into a set of functions. Ideally, functions only take inputs and produce outputs.

del my_dict[key]:

Deletes the key entry from a dict.

Comma separated values (CSV)

Each line (even if blank) is a row. In each row, each value is separated from the others by a comma even if it is blank known as fields. Ex: Name,exam1,exam2 Sam,9,10 Mike,10,10

In Exception handling, finally clause will not be executed if break statement is executed in the try block.

False

Binary Data

Files that consist of data stored as a sequence of bytes. (Images, videos, and PDF files are examples of the types of files commonly stored as binary data).

filter()

Filters out items from a collection that are not wanted

map()

Function that takes two arguments (func,seq) and returns iterator.

reduce()

Function that takes two arguments (func,seq) and returns single value.

my_dict[key]:

Indexing operation (retrieves the value associated with key.

len(lst):

number of elements in list (top level).

Data Structures

particular ways of storing data to make some operation easier or more efficient. That is, they are tuned for certain tasks.

reduce()

reduces down the collection ( Like Sum )

my_set.discard("x"):

removes "x" from the set if exit, otherwise nothing will be removed.

my_set.remove("x"):

removes 'x' if exit, otherwise issue an error.

file.readlines() method:

returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on.

file.readline():

returns a single line at a time. It takes one optional parameter (size) which is the number of bytes from the line to return.

my_set.copy():

returns an independent copy of my_set

file.read() method:

returns the file contents as a string.

min(lst):

smallest element. Must all be the same type.

sum(lst):

sum of the elements, numeric only.

zip function

takes iterable elements as input, and returns iterator.

Computer systems with life threatening implications...

•Self-driving cars •Missiles •Nuclear reactors •Air traffic control •Medical equipment

Why have a code of ethics?

•Software engineers and Information Technology can •Do good or cause harm •Enable others to do good or cause harm •Influence others to do good or cause harm.


Conjuntos de estudio relacionados

Business Management - Brand Promise

View Set

Final Exam Chapter 13, 14, 15, and 16

View Set

Intro to Psych Unit 1 Test (1-5)

View Set

Quiz: Third-Party Policy Ownership

View Set

RENAL NCLEX, Saunders NCLEX Rena combined with other med surge books

View Set

Linux Ch. 22 Investigating User Issues

View Set