CPSC 150 final

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/71

flashcard set

Earn XP

Description and Tags

i am so cooked

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

72 Terms

1
New cards

What is a comment in a computer program?

Text intended only for the human reader that is completely ignored by the interpreter.

2
New cards

How do you start a comment in Python?

Using the "#" token.

3
New cards

Value

One of the fundamental things that a program manipulates.

4
New cards

Variable

A name that refers to a value.

5
New cards

Integer

Positive or negative whole numbers

6
New cards

float

decimal numbers

7
New cards

bool

(boolean: True, False)

8
New cards

NoneType

(default data type returned when a function stores nothing)

9
New cards

String

Anything surrounded by single or double quotes

10
New cards

What is the purpose of the type() function?

To find out what "class" a value falls into.

11
New cards

Variable Naming Rules (Python)

Names can be arbitrarily long , contain letters, underscores, and digits, but must begin with a letter or underscore. Case matters.

12
New cards

Assignment Operation

The assignment statement gives a value to a variable.

13
New cards

int() function

Takes a floating point number or a string and turns it into an int. For floats, it discards the decimal portion (truncation towards zero).

14
New cards

float() converter

Can turn an integer, a float, or a syntactically legal string into a float.

15
New cards

str() converter

Turns its argument into a string.

16
New cards

What does the + operator do with strings?

It causes concatenation, which means joining the two operands end to end.

17
New cards

What does the * operator do with strings?

It performs repetition.

18
New cards

len() function

Returns the number of characters in a string.

19
New cards

Are strings mutable?

No, strings are immutable, meaning you cannot change an existing string.

20
New cards

Function (Python)

A named sequence of statements that belong together, primarily to organize programs into logical chunks.

21
New cards

Syntax for defining a function

def NAME(Parameter): followed by indented STATEMENTS.

22
New cards

Parameters

Specifies what information, if any, is needed to use the new function.

23
New cards

Arguments

Values provided in a function call that are assigned to the parameters in the function definition.

24
New cards

Scope of Variables and Parameters

They are local, meaning a variable created inside a function only exists inside the function definition and cannot be used outside.

25
New cards

print vs. return (Key Difference)

print displays stuff to the screen and the function continues execution; return stops the execution of further statements and returns/stores a value to the function. A function can only return one thing.

26
New cards

Boolean Expression

True or False

27
New cards

Conditional Statement

A statement that controls the flow of execution based on some condition.

28
New cards

if Statement Execution Rule

At most one block of statements will be executed; the first block that evaluates to True gets executed.

29
New cards

Iteration

Repeated execution of a set of programming statements.

30
New cards

for loop

Used for iterating over a sequence(string, list, tuple, range)

31
New cards

while loop

Executes a set of statements as long as a condition is true

32
New cards

break statement

stops the loop entirely, “breaks” out of it

33
New cards

List

Collection of data stored in between brackets

34
New cards

Are lists mutable?

Yes, lists are mutable; you can change their elements by a simple assignment.

35
New cards

Tuple

Collection of data stored in between parenthesis

36
New cards

Are tuples mutable?

No, tuples are immutable, but if there is a list inside a tuple, that list can be modified.

37
New cards

Dictionary

Python's built-in mapping type that maps keys to values.

38
New cards

Dictionary Keys

Must be of an immutable type.

39
New cards

Dictionary Values

Can be of any type.

40
New cards

Are dictionaries mutable?

Yes, dictionaries are mutable.

41
New cards

myDict.keys()

Returns a list of all the keys in the dictionary.

42
New cards

myDict.items()

Returns a list of all the key-value pairs as tuples in the format (key, value).

43
New cards

What are the four basic data types listed, and what is a common non-basic type?

Basic: int, float, bool, NoneType. Common Non-Basic: str (String).

44
New cards

What is the rule for the very first character in a Python variable name?

It has to begin with a letter or underscore.

45
New cards

What is the name of the type conversion process that the int() function performs on a floating point number?

Truncation towards zero on the number line (discarding the decimal portion).

46
New cards

If you enclose the number 123 in quotes, what data type is it classified as?

String (str).

47
New cards

What is the priority order for mathematical operations in Python, according to PEMDAS?

Parentheses $>$ Exponentiation $>$ Multiplication = Division = Modulo $>$ Addition = Subtraction.

48
New cards

What is the term for what the + operator does when used with two strings?

Concatenation, which means joining the two operands end to end.

49
New cards

What value does the expression 2 == 2 evaluate to?

A Boolean value: True.

50
New cards

What is a characteristic of strings that means you cannot change an existing string?

Strings are immutable.

51
New cards

What is the primary purpose of functions in programming?

To help organize programs into chunks that match how we think about the problem.

52
New cards

In a function definition, variables and parameters are considered to be what?

Local.

53
New cards

What is the main effect of a function hitting a return statement?

It stores the value returned and stops executing any further statements in the function definition.

54
New cards

What is the default value returned by the print statement?

None.

55
New cards

In an if Statement group, when does the first block that evaluates to True get executed?

It gets executed irrespective of whether the next blocks in the same family evaluate to True or not.

56
New cards

When should you choose a for loop over a while loop?

Use a for loop if you know, before you start looping, the maximum number of times you'll need to execute the body.

57
New cards

What is an infinite loop?

A loop that repeats forever because the body of the loop does not change the value of variables necessary to make the condition eventually become False.

58
New cards

What does the range(start, stop) function return?

A list of numbers from start up to but not including stop.

59
New cards

What is the key difference between lists and strings in terms of mutability?

Lists are mutable (you can change elements) , while strings are immutable (you cannot change them).

60
New cards

What are the two types of components in a Python dictionary?

They map keys (must be immutable type) to values (can be of any type).

61
New cards

What dictionary method returns a list of all the key-value pairs as tuples?

myDict.items()

62
New cards

What are the three access modes for opening a file?

"r" (read), "w" (write), "a" (append).

63
New cards

What happens if you open a file in "w" (write) mode when the file already exists?

Writing to the file will overwrite the file.

64
New cards

Mutable types

Lists and dictionaries

65
New cards

Immutable types

Int, Float, String, Tuple, Boolean

66
New cards

Dictionary

Collection of keys and values store in between curly braces

67
New cards

x = ‘Hello World’

What index would we use to get “ello”?

x[1:5]

68
New cards

fruits = [“Apple”, “Pear”, “Berry”, “Banana”]

What index is Berry at?

fruits[2]

69
New cards

list = [3, 6, 8, 1, 2, 5, 0, 7, 2, 9]

i = 0

j = 0

while i < len(list) and list[i+1] != 0

if list[i] % 2 == 1:

j = j + 1

i = i + 1

What are the values of i and j after this loop is executed?

i = 5

j = 2

70
New cards

What range function generates the numbers 1, 5, 9, 13?

range(1, 14, 4)

71
New cards

What numbers ger generated by range( 6, 2, -1)

6, 5, 4, 3

72
New cards

n = 4

for i in range (1, n + 1):

for j in range ( 1, n)

if j < i:

print(i * j)

What does this loop print out?

2

3

6

4

8

12