COP3502C - M4, M5, M6 (up to slide 11)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

output and function of operation s="hello", s[1]=>

"e" [] used for indexing and slicing

what's the output a = 12, b = (12,) c = (12, 1, 6, 9) d = a + b print(d) print(c[1:3]) e = list(d) e[0] = 3 print(e) print(tuple(e))

(12, 12) (1, 6) [3, 12] (3, 12)

what does * do when unpacking lists/tuples? What's the output? x = (1, 2, 3, 4) (works the same if it were list too) a, b, *c = x print(a, b, c) a, *b, c = x print(a, b, c) *a, b, c = x print(a, b, c)

* absorbs the rest of elements not matched with the other elements 1 2 [3, 4] 1 [2, 3] 4 [1, 2] 3 4

how to secure personal info

- Lock it up (encryption) - Hide it (deniability) - Audit systems (testing)

if you collect data, you are responsible for it and should...

- give terms of use (What will we do with it?) - Guard user privacy (Keep it from others) - Keep it secure (Store it safely)

output and function of operation min([9,1,5]) =>

1 min(seq) or max(seq) Returns the min and max values in seq

output and function of operation (1,2,2,3,2).index(2) =>

1 seq.index(x) returns the index of the 1st occurrence of x

my_list = [[1,2,3], [4,5,6], [7,8,9]] for sublist in my_list: >>> for element in sublist: >>>>>> print(item, end=' ') >>> print() what's the output?

1 2 3 4 5 6 7 8 9

what's the output? my_list = [1,2,3,4,5] for item in my_list: >>>print(item, end=' ') print() for index, item in enumerate(my_list): >>>print(f'Index {index}: {item}')

1 2 3 4 5 Index 0: 1 Index 1: 2 Index 2: 3 Index 3: 4 Index 4: 5

Sometimes law enforcement and privacy are on opposite sides. when reporting, We should ask if disclosure is (4)

1) legal? 2) ethical? 3) helpful? 4) harm-causing?

how to write 12 as a tuple

12, or (12, ) just need the comma, it will print w/parentheses regardless of whether you put it

hex_digit = 'D' print(ord('A')) print(ord('B')) print(ord('C')) print(ord(hex_digit) - ord('A') + 10) output: 65 66 67 _?_

13

output and function of operation sum([9,1,5]) =>

15 sum(seq) returns the sum of all elem in a sequence

d = (True) * 2 print(d) what's the output? explain

2 because True is evaluated as 1 while False is evaluated as 0

course_num = 3502 welcome = 'Welcome to COP' + str(course_num) + 'C. ' messages = 'Hope you will have fun ' >>> \ 'learning python.' print(len(welcome)) print(welcome + messages) print(welcome[3:12]) welcome[1] = "h" what's the output?

21 # finds string length Welcome to COP3502C. Hope you will have fun learning python. # concatenates two strings come to C come to C (uses indexing, prints from index 3 to 11) TypeError: 'str' object does not support item assignment (u cant change c to h because strings are immutable)

output and function of operation len((2,4,9)) =>

3 len(seq) returns the num of elem in a sequence

output and function of operation (1,2,2,3,2).count(2) =>

3 seq.count(x) returns the num of occurrences of x

my_list = [[1,2,3], [4,5,6], [7,8,9]] print(my_list[2][0], my_list[2][1], my_list[2][2]) what's the output?

7 8 9

== and != vs is and is not

==, != --> used to compare whether integers, strings, & values in the containers (e.g., list, tuple, range) are the same is and is not --> used to compare whether the variables reference the same object

what's the output a = [1, 2, 4] b = [1, 2, 4] print(a == b) print(a is b)

True False

what's the output a, b = 11, 11 print(a == b) c, d = "hello", "hello" print(c == d)

True True

output and function of operation 4 in [1,2,4] =>

True in checks the membership

if "anything" vs if " " vs if 0 vs if (any number): >>> print(True) else: >>> print(False) what's the output?

True, False, False, True

course_num = 3502 welcome = 'Welcome to COP' + str(course_num) + 'C. ' messages = 'Hope you will have fun ' >>> \ 'learning python.' print(welcome) print(messages) what's the output? what does the backslash do?

Welcome to COP3502C. Hope you will have fun learning python. \ splits the code to see full line without needing to scroll if it's too long

rows, cols = (3, 4) board = [] for i in range(rows): >>> row = [] >>> for j in range(cols): >>>>>> row.append("-") >>> board.append(row) board[0][1] = 'x' for row in board: >>> print(row)

['-', 'x', '-', '-'] ['-', '-', '-', '-'] ['-', '-', '-', '-']

rows, cols = (3, 4) board = [["-" for i in range(cols)] >>>>>>>>>>>>>for j in range(rows)] board[0][1] = 'x' for row in board: >>> print(row) what's the output?

['-', 'x', '-', '-'] ['-', '-', '-', '-'] ['-', '-', '-', '-']

QC: What is the output of the following program? b = [8, 2, 7, 1, 6, 12] c = [item // 2 for item in b if item % 2 == 0] c[0:-1] = '12' print(c)

['1', '2', 6]

even_numbers = [i for i in range(0, 10) if i % 2 == 0] print(even_numbers) same as even_numbers = [] for i in range(0,10): >>> if i % 2 == 0: >>>>>> even_numbers.append(i) print(even_numbers) what's the output?

[0, 2, 4, 6, 8]

my_list = [5,2,1,4,3,3,9] my_list.sort() print(my_list) my_list.reverse() print(my_list) my_list.clear() print(my_list) what's the output?

[1, 2, 3, 3, 4, 5, 9] [9, 5, 4, 3, 3, 2, 1] []

a = [1, 2, 3, 4] a.append(100) print(a) a.extend([7, 8, 9, 10]) print(a) a.append([8, 9, 10]) print(a) what's the output?

[1, 2, 3, 4, 100] # append only adds one element [1, 2, 3, 4, 100, 7, 8, 9, 10] # extend will add multiple values [1, 2, 3, 4, 100, 7, 8, 9, 10, [8, 9, 10]]

my_list = [1,2,3,4,5] my_list.insert(3, 99) print(my_list) my_list.remove(99) print(my_list) my_list.pop(0) print(my_list) what's the output?

[1, 2, 3, 99, 4, 5] # insert 99 at index 3 [1, 2, 3, 4, 5] # remove first occurence of 99 from list [2, 3, 4, 5] # remove element at index 0

QC: What is the output of the following program? a = [1, 5] + [3] a.append(1) a.extend([1, 3]) print(a)

[1, 5, 3, 1, 1, 3]

output and function of operation [1,2] * 3 =>

[1,2,1,2,1,2] * repeats a sequence multiple times

output and function of operation [1,2] + [4,5] =>

[1,2,4,5] + combines two sequences

first_list = [ >>> [1, 2, 3], >>> [4, 5, 6], >>> [7, 8, 9] ] res = [] for sublist in first_list: >>> res.append(sum(sublist)) print(res) What's the output?

[6, 15, 24]

first_list = [ >>> [1, 2, 3], >>> [4, 5, 6], >>> [7, 8, 9] ] res2 = [sum(sublist) for sublist in first_list] print(res2)

[6, 15, 24]

def square(x): >>> return x*x second_list = [3,5,8,2] square_elements = [square(item) for item in second_list] print(square_elements) what's the output?

[9, 25, 64, 4]

QC: What is the output of the following program? a = [8] * 2 b = [5, 9, 1] c = [a, b] d = c a[:] = [6] b[2] = 4 print(d)

[[6], [5, 9, 4]]

2D list

a list within a list representing a table with rows and columns

tuple is

a sequence of comma-separated values; typically surrounded with parentheses

append vs extend

adds one value vs multiple values

general ACM Code of Ethics are mainly about

being righteous, fair, and honest

Elements of a string are accessed using

brackets [ ]

whenever there's a colon (slicing), the output will come back in

brackets [] as a list

We could convert between a text character and encoded value using ___ to get text and ___ to get encoded value

chr(), ord()

an unordered collection of objects is a ___ two examples are ___ and ___

container sets and dictionaries

deep copy

copies the object as a new collection that is not affected by any changes in the original object so if deep is changed, original is NOT changed and vice versa

shallow copy

copies the reference to an object (basically just contains nesting) if nested is changed, original is changed and vice versa (NOT just one way around)

what dict method Merges two dictionaries and overrides existing entries in dict1 if the same key exists in dict2

dict1.update(dict2)

how to type an empty dict vs empty set

dict: a = {} set: a = set() (but once there's elements in the {}, type becomes set

what dict method is used to read the given key value and if it doesn't exist, it returns given default

dict_name.get(key, default)

what dict method removes the given key value or default if that key doesn't exist

dict_name.pop(key, default)

a = "Welcome to COP3502C" print(a[1]) print(a[-1]) what's the output?

e c (you can index from the end too, but it starts at 1 instead of 0 since you can't have -0)

QC: What is the output of the following program? a = [2, (7, 'Joe'), 'Welcome', 6] print(a[1][-1][-1], a[-2][-3], a[0:-3], a[3:1:-2])

e o [2] [6]

list nesting

embedding of a list inside another list

built-in function that iterates over a sequence and provides an iteration counter

enumerate()

what is penetration testing (white hat hacking)

ethical hacking it's how We find vulnerabilities in systems

- Moral principles that govern behavior - Often recognized and can be codified - Tend to be shared (rather than personal) - A branch of philosophy these are...

ethics

String is mutable/immutable?

immutable

tuple is immutable or mutable

immutable

membership operators are

in and not in operators (used to determine if a specific value can be found within a container like a list, a string, or a range)

refers to creations of the intellect: inventions, literary and artistic works, symbols, names images, and designs used in commerce are a part of it

intellectual property

identity operators are

is and is not operators (used to determine whether two variables are bound to the same object)

what happens when you try to create a new entry with a key that already exists in the dictionary

it REPLACES the existing entry because dict keys are unique

Dictionary associates ___ with ___. Keys must be of an ___ type

keys values immutable (like strings)

ethics is different from

law

what can you use to find string length?

len print(len(example))

____ are mutable and can be indexed from its front or back. They are usually are surrounded with brackets[]. A ___ can be obtained using [m:n], where m is inclusive starting point and n is exclusive ending point.

lists, slice

Are sets mutable or immutable?

mutable

how to add 'address': 'Downtown' as an item in my_dict = {'name': 'Jack', 'age': 26}

my_dict['address'] = 'Downtown' print(my_dict)

my_dict = {'name': 'Jack', 'age': 26} how to update 26 to 27

my_dict['age'] = 27 # update value print(my_dict)

two ways to clear my_list

my_list.clear() or my_list[:] = []

two ways to reverse my_list

my_list.reverse() or my_list[::-1]

two ways to get last element of my_list

my_list[-1] or my_list(len(my_list)-1)

professional ACM Code of Ethics are mainly about

professionalism

what does name_of_dict.items() do

returns (key, value) tuples in a list separated by commas

what does name_of_dict.keys() do

returns dictionary keys (before colon) in a list separated by commas

what does name_of_dict.values() do

returns dictionary values (after colon) in a list separated by commas

an ordered collection of objects is a ___ three examples are ___, ___, and ___ because you can index them, which means

sequence lists, strings, tuples, count their places in order starting from 0

a sequence of characters

string

when replacing slices with a ____, you replace it with each character in the string separated individually like replacing index[2:4] = "hi33" will be ["h", "i", "3", "3"]

string

Command-line arguments

values entered by a user when running a program from a command line. The contents are automatically stored in the list sys.argv

can you have mixed data types in a tuple? (strings, integers, floating point numbers, etc)

yes

how to sort, reverse, and clear functions operating on tuples and strings

you can't because they're immutable, unlike lists

what's the output? d = set([1, 1, 1, 2, 3, 9, 1]) print(d)

{1, 2, 3, 9} cause a set is an unordered collection of UNIQUE elements so duplicates taken out

what's the output a = {1, 2, 3} b = set([4,5,6]) c = a.union(b) print(a, b, c) a = {2, 3, 4} b = {3, 4, 5} e = a.intersection(b) print(a, b, e) h = a.difference(b) print(a, b, h)

{1, 2, 3} {4, 5, 6} {1, 2, 3, 4, 5, 6} {2, 3, 4} {3, 4, 5} {3, 4} {2, 3, 4} {3, 4, 5} {2}

what's the output a = {1, 2, 3} my_set = set() my_set.add(3) my_set.update({4, 5, 3, 8}) print(my_set, len(my_set)) my_set.remove(4) print(my_set) print(4 in my_set) my_set.pop() print(my_set)

{3, 4, 5, 8} 4 {3, 5, 8} False {5, 8} # random value is removed because sets are UNORDERED so no indexing

- is the numerical representation of a character such as 'a' or '@' or an action of some sort. - is used to represent every possible character as a unique number, known as code point

ASCII Code (American Standard Code for Information Interchange)

Responsibly Disclosure is when you... while Full Disclosure is when you...

Alert vendor confidentially, then public later Immediately publicize all details

statements that is used to set sanity (checks that enviro is running right) during the development process

Assertions

my_list = ["first", "second", "third", "fourth"] print(my_list[0], my_list[2]) print(my_list[-1], my_list[-3]) print(my_list[0:3]) print(my_list[:-1]) print(my_list[-3:]) print(my_list[:]) print(my_list[0:4:2]) print(my_list[::-1]) what's the output?

SLICING LISTS first third fourth second ['first', 'second', 'third'] ['first', 'second', 'third'] # no starting point ['second', 'third', 'fourth'] # no end point ['first', 'second', 'third', 'fourth'] # copy a list ['first', 'third'] # move 2 elements forward each time ['fourth', 'third', 'second', 'first'] # reverse the list (no start/end point, but moves backward 1 each time)

built-in functions that operate on sequences are called ___ (such as lists, tuples and strings)

Sequence-type functions

what's the output a = 12 b = 0 assert b != 0 print("The result of division: ", a / b)

Traceback (most recent call last): >>>File "main.py", line 4, in <module> >>>>>>assert b != 0 AssertionError (error is shown when the assert statement is not true)

set of moral principles that regulate the use of computers

Computer ethics

two branches of Intellectual Property

Industrial Property (which protects patents, trademarks, industrial designs, commercial names, geographic indications etc.) and Copyright (Which protects original authorship by preventing the work from being used without prior authorization)

what's the output my_dict = {'name': 'Jack', 'age': 26} print(my_dict.get('name', 'Sally')) print(my_dict.get('address', 'Gainesville')) my_dict.update({'address':'Orlando'}) my_dict.update({'name': 'Sally'}) print(my_dict) my_dict.pop('address') print(my_dict)

Jack Gainesville {'name': 'Sally', 'age': 26, 'address': 'Orlando'} {'name': 'Sally', 'age': 26}

Which of these values are mutable (changeable) and immutable (fixed): string, list, tuple, integer, float

Mutable: list rest is immutable

my_list = [1,2,3,4,5] my_list[1:3] = [100] print(my_list) my_list[1:3] = 'hello' print(my_list) my_list[1:6] = (2,3,4) print(my_list) my_list[1:3] = [] print(my_list) what's the output?

SLICING ASSIGNMENT [1, 100, 4, 5] # replaces that range [1, 'h', 'e', 'l', 'l', 'o', 5] [1, 2, 3, 4, 5] [1, 4, 5] # empty brackets remove elements

what's the output a = 12 b = 0 assert b != 0, "Zero Division Error" print("The result of division: ", a / b)

Traceback (most recent call last): File "main.py", line 4, in <module> >>>assert b != 0, "Zero Division Error" >>>>>>AssertionError: Zero Division Error (error is shown when the assert statement is not true... if something typed after assert statement, it'll show after the error)


संबंधित स्टडी सेट्स

Chapter 31: Drug Therapy for Nasal Congestion and Cough

View Set

Cultural Anthropology exam questions

View Set

Women and Children chapter 41 prep u

View Set

Chapter 1: Windows Server 2012 Roles and Features

View Set