Computational Thinking Midterm

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/113

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

114 Terms

1
New cards

What data type is always returned by input()?

string

2
New cards

What will be the output of print(5)?

5

3
New cards

Which of the following best describes comments in python?

They are ignored by Python and written for humans.

4
New cards

When do you use elif

When you have a case with multiple conditions, use elif to denote additional outcome. 

5
New cards

if, elif conditional formatting

if

elif

elif

else

6
New cards

What will 12 % 5 evaluate to?

2

7
New cards

What is the result of round (15.6)?

16

8
New cards

Which operator returns the remainder after division?

%

9
New cards

After importing date time as dt, which line correctly retrieves the current date and time?

dt.datetime.now()

10
New cards

which of the following correctly imports the date time module with an alias?

Import date time as dt

11
New cards

Which function from the math module always rounds a number up?

ciel()

12
New cards

Which of the following is the correct way to import the sqrt function from the math module?

from math import sqrt

13
New cards

What is the result of the expression 7//2?

3

14
New cards

Literal

Fixed value that you write directly in your code - they represent themselves exactly. A number in an equation.

15
New cards

Expressions

Combinations of literals, variable, and operators that python can evaluate to produce a value. What comes after the equal sign in an equation.

16
New cards

Statement

Complete instructions that Python can execute. The entire equation. 

17
New cards

Data objects

Objects have a type that defines the kinds of things programs can do to them. They are scalar, they cannot be subdivided or they can be non-scalar, they have an internal structure that can be accessed. 

18
New cards

Scalar Objects Example

Look at the image.

<p>Look at the image. </p>
19
New cards

Non-scalar objects example

Look at the image.

<p>Look at the image. </p>
20
New cards

type casting hierarchy

int>float>str

21
New cards

What does // produce

how many times a number can go in another one, 5//3 = 1

22
New cards

What does % produce

the remainder after division, 5%3 =2

23
New cards

What does ** produce1

Exponents / to the power of

24
New cards

How do you import as an alias?

import matplotlib as plt which would be used as plt.plot([1,2,3,4])

25
New cards

Math module

ciel = round up, floor = round down, round = normal rounding. 

26
New cards

What does the not operator do?

Negates a boolean value

27
New cards

What will be the result of the expression 10 != 5

True

28
New cards

Which operator is used to require at least one condition to be True?

or

29
New cards

Which scenario best demonstrates the use of the compound logical expressions?

Determining if a student passed based on grade and attendence

30
New cards

What keyword is used in a multi-way decision

elif

31
New cards

Which of these is an example of boundary testing in an age classifier

Age = 12, 13, 17, 18

32
New cards

Which keyword begins a one-way decision in python?

if

33
New cards

Why does the operator != do

Not Equa;

34
New cards

What does the operator == mean

Equal to

35
New cards

What does a not operator do

used to negate a boolean expression

36
New cards

Which type of loop runs when you don’t know in advance how many times it will execute?

While loop

37
New cards

What does the iteration variable do in a for loop?

Holds each item in the sequence being processed.

38
New cards

What keyword ends a loop immediately?

break

39
New cards

What will this code print?

for i in range(1, 6):
    if i % 2 == 0:
        continue
    print(i)

1 3 5

40
New cards

Which python statements correctly increment the counter event_count?

even_count += 1

 

even_count = even_count + 1

41
New cards

What is the output of

for i in range(2, 6):
     print(i)

2 3 4 5

42
New cards

What does the operator += mean

Add and reassign

43
New cards

Which of the following loops is definite?

for i in range(5):

44
New cards

What keyword skips the rest of the current iteration but continues the loop?

continue

45
New cards

What is an indefinite loop?

a loop that keeps running until a specific condition is met. Will execute forever because the condition never becomes false.

46
New cards

What is a while loop

It evaluates the condition, and proceeds to test the conditions until it is false.

47
New cards

More assignment operators

Look at the image

<p>Look at the image</p>
48
New cards

What is a definite loop

A loop that runs a known number of times, you do know how many times it will repeat running it. 

49
New cards

Iteration variables

For loops that explicit iteration variables that change each time through a loop. These iteration variables move through a sequence or set.

50
New cards

Range arguments

range(start, stop, step)

start - first number in the sequence

stop - upper limit

step - by how much it counts up or down

51
New cards

Lists

fruits = [“apple”, “banana”, “cherry”] , lists are mutable, they can be changed. duplicates are allowed, and allows mixed data types.

52
New cards

String formatting with modulo %

placeholders

<p>placeholders</p>
53
New cards

Format specifiers for modulo

Look at image

<p>Look at image</p>
54
New cards

F string formatting

f"Hello, {name}! You’re {age} years old.”

You put the variables inside of the curly brackets

55
New cards

Important formatting instructions 

Look at the image

<p>Look at the image</p>
56
New cards

Formatting tables using f strings

print(f"{'Month':>5} {'Payment':>10} {'Principal':>12} {'Interest':>10} {'Balance':>12}")

<p><span>print(f"{'Month':&gt;5} {'Payment':&gt;10} {'Principal':&gt;12} {'Interest':&gt;10} {'Balance':&gt;12}")</span></p>
57
New cards

What does print(fruit[1]) output when the word banana is there

a

58
New cards

Using open()

fh = open(“mbox.txt”, “r”)

59
New cards

Closing open files using close()

fh.close()

60
New cards

read the lines into a list

lines = fh.readlines()

61
New cards

Read the whole file into one string

content = fh.read()

62
New cards

Using the with statement

with open(“box.txt”, “r”) as fh:

content = fh.read(),

the with command automatically closes the file after its done reading

63
New cards

repr()

A build in function that return the representation of an object

<p>A build in function that return the representation of an object </p>
64
New cards

len()

gives us the length of a string

65
New cards

.lower()

converts to lowercase

66
New cards

.upper()

converts to uppercase

67
New cards

.casefold()

“Aggressive” lowercase, works with unicode, converts it all to one readable text.

68
New cards

.strip()

removes leading and trailing whitespace, can insert a specific character into the parenthesis to specify what to strip.

69
New cards

More search and count functions

Look at the image

<p>Look at the image</p>
70
New cards

.split()

splits on whitespace by default, can insert characters to split by

71
New cards

open, “a”

append, will append to the end of the file

72
New cards

open “w”

write, if a file doesn’t exist, python will create a new file otherwise it will overwrite any existing content

73
New cards

open “x”

write, if a file exists, python gives error, otherwise this will create a new file to which python can write content. 

74
New cards

What is a list?

A list is a kind of collection, allows us to put many values in a single “variable”.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

always in closed square brackets. mutable and each part can be induced to each part.

75
New cards

Negative Indices

Look at this image

<p>Look at this image</p>
76
New cards

.append()

we can add elements using the append method, the list stays in order and new elements are added at the end of the list.

77
New cards

deleting a list

del(fruit)

print(fruit), will be empty

78
New cards

.index()

returns the position of the first occurrence of an element in the list 

79
New cards

.pop()

remove and return the last element in the list

80
New cards

.sort()

sorts elements in the list in ascending order, .sort(reverse=True) sorts in descending order

81
New cards

.deepcopy()

executes a deep copy, must import the copy module first. creates a second copy of the list. must attach to a new list.

82
New cards

Dictionaries

A mutable data structure that stores data as key-value pairs. ]

scores = { 'Chuck' : 61, 'Fred' : 83, 'Jan': 99 }

83
New cards

Tuple

A read only list, immutable, cannot be changed after it's created. In parenthesis.

<p>A read only list, immutable, cannot be changed after it's created. In parenthesis. </p>
84
New cards

What is a set?

A set is a collection of unique items that are unordered and unindexed. You cannot access elements using an index number. Inside curly brackets. 

<p>A set is a collection of unique items that are unordered and unindexed. You cannot access elements using an index number. Inside curly brackets.&nbsp;</p>
85
New cards

What does the difference method do for sets?

Returns items in one set but not the other

86
New cards

Which of the following creates a set correctly?

team = {“Alice”, “Bob”}

87
New cards

Which operations gives all elements that are in a or b (or both)?

a | b

88
New cards

What happens if you try to modify a tuple element?

Raises a TypeError

89
New cards

Which method removes a specific key from a dictionary and returns its value?

pop()

90
New cards

Why are tuples used instead of lists in some cases?

Tuples are faster and immutable

91
New cards

What does this code print?

a = {1, 2, 3} b = {3, 4, 5} print(a & b)

{3}

92
New cards

Given info = {"name": "Alice", "age": 30}, what does info.update({"age": 31}) do?

Updates “age” to 31

93
New cards

What will this code print?

prices = {"apple": 1.5, "banana": 0.9}
print(prices["banana"])

0.9

94
New cards

What happens if you add a duplicate element to a set?

The duplicate is ignored.

95
New cards

Which module is needed to perform a deep copy of a list in python

import copy

96
New cards

If sales = [100, 200, 300], what is the output of sales * 2?

[100, 200, 300, 100, 200, 300]

97
New cards

If inventory = [50, 20, 70, 30], what does inventory.sort() produce?


[20,30,50,70]

98
New cards

Which of the following correctly creates a list in Python?

list = [1,2,3]

99
New cards

What does negative indexing allow you to do in a list?

Access items from the end of a list

100
New cards

Which of the following correctly copies a list without linking it to the original?

b =a.copy()