CSE 20 midterm

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/46

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.

47 Terms

1
New cards

variable

is a named item, such as x or num_people, that holds a value.

2
New cards

assignment statement

assigns a variable with a value, such as x = 5. That statement means x is assigned with 5, and x keeps that value during subsequent statements, until x is assigned again.

3
New cards

Processor

The processor runs the computer's programs, reading and executing instructions from memory, performing operations, and reading and writing data from and to memory. When powered on, the processor starts executing the program. The first instruction is typically at memory location 0. That program is commonly called the BIOS (basic input/output system), which sets up the computer's basic peripherals

4
New cards

Memory: RAM

  • (random-access memory) temporarily holds data read from storage and is designed so any address can be accessed much faster than from a disk. The "random access" term comes from accessing any memory location quickly and in arbitrary order, without spinning a disk to get a proper location under a head. RAM is more costly per bit than disk due to RAM's higher speed. RAM chips typically appear on a printed circuit board along with a processor chip. RAM is volatile, losing its contents when powered off. Memory size is typically listed in bits, or in bytes where a byte is 8 bits. Common sizes involve megabytes (million bytes), gigabytes (billion bytes), or terabytes (trillion bytes). 

5
New cards

incrementing

Increasing a variable's value by 1, as in x = x + 1, is common and known as incrementing the variable.

6
New cards

Python is ____ meaning uppercase and lowercase letters differ. Ex: "Cat" and "cat" are different. The following are valid names: c, cat, Cat, n1m1, short1, and _hello. The following are invalid names: 42c (doesn't start with a letter or underscore), hi there (has a space), and cat$ (has a symbol other than a letter, digit, or underscore).


case sensitive

7
New cards

An ____ represents a value and is automatically created by the interpreter when executing a line of code. For example, executing x = 4 creates a new object to represent the value 4. A programmer does not explicitly create objects; instead, the interpreter creates and manipulates objects as needed to run the Python code. Objects are used to represent everything in a Python program, including integers, strings, functions, lists, etc.


object

8
New cards

garbage collection

Deleting unused objects is an automatic process called garbage collection that frees memory space

9
New cards

Name binding

is the process of associating names with interpreter objects. An object can have more than one name bound to it, and every name is always bound to exactly one object. Name binding occurs whenever an assignment statement is executed, as demonstrated below.

10
New cards

Mutability

indicates whether the object's value is allowed to be changed.

11
New cards

Integers and strings are ___; meaning integer and string values cannot be changed.

immutable

12
New cards

Float-type objects have a limited range of values that can be represented. For a standard 32-bit installation of Python, the maximum floating-point value is approximately 1.8x10308, and the minimum floating-point value is 2.3x10-308. Assigning a floating-point value outside of this range generates an _____ Overflow occurs when a value is too large to be stored in the memory allocated by the interpreter.

Overflow Error

13
New cards

In general, floating-point types should be used to represent quantities that are measured, such as distances, temperatures, volumes, and weights. Integer types should be used to represent quantities that are counted, such as numbers of cars, students, cities, hours, and minutes

true

14
New cards

What is output by print(f'{9.1357:.3f}')?


9.136

print(f'{9.1357:.3f}') outputs 9.136 because the third digit after the decimal point is rounded.

15
New cards

print(f'{my_float:.5f}')

{} → means insert a value here.

my_float → the variable you want to print.

: → this tells Python “I’m going to specify a format now.”

.5f →

  • . → start formatting the decimal part.

  • 5 → 5 digits after the decimal point.

f → format it as a floating-point number (decimal number).

16
New cards

| Format | Meaning | Example output |

| ------ | ---------------- | -------------- |

| .2f | 2 decimal places | 3.14 |

| .3f | 3 decimal places | 3.142 |

| .5f | 5 decimal places | 0.71429 |

Specifies the number of digits after the decimal point in floating-point formatting.

17
New cards

When should you use float(input()) instead of int(input())?

Use float(input()) if you need to print or calculate with decimals (e.g., using .2f, .3f formatting).

Use int(input()) only for whole numbers without decimals.

18
New cards

If you see .2f, .3f, .5f → use

float()

19
New cards

If you're counting things (like apples, students) use

int()

20
New cards

expression

is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 (x + 1). A common place where expressions are used is on the right side of an assignment statement, as in y = 2 (x + 1).

21
New cards

literal

specific code value like 2

22
New cards

operator

is a symbol that performs a built-in calculation, like +, which performs addition.

23
New cards

In programming, multiplication typically must be indicated explicitly using which operator?

*

24
New cards

Commas are not allowed in an integer literal. So 1,333,555 is written as 1333555.

true

25
New cards

Which expression gets the tens digit of x? Ex: If x = 693, which expression yields 9?

x // 10 removes the rightmost digit, putting the tens digit in the ones place. Then % 10 gets the (new) ones digit. Ex: 693 / / 10 is 69, then 69 % 10 is 9.

26
New cards

% (Modulo)

  • Gives the remainder after division.

  • "What's left over?"

Example:
7 % 3 1 (because 7 - 3×2 = 1 left over)

27
New cards

// (Floor Division)

  • Divides and gives the whole number (integer) part only.

  • "How many times does it fit?"

Example:
7 // 3 2 (because 3 fits into 7 two times)

28
New cards
  • Use // when you care about how many full items fit.

  • Use % when you care about what's left over.

Floor division gives the whole number result of division, while modulo gives the remainder. They are both used in calculations to determine how many complete units fit or what remains.

29
New cards

module

is Python code located in another file

30
New cards

is a list of statements that can be executed simply by referring to the function's name. The statements for sqrt() are within the math module itself and are not relevant to the programmer.

Function

31
New cards

ceil(x)

round up value

32
New cards

fabs(x)

absolute value

33
New cards

floor(x)

round down value

34
New cards

randrange()

method generates random integers within a specified range. A single positive integer argument can be passed to the randrange() method to return an integer between 0 (inclusive) and the specified value (exclusive). Ex: random.randrange(10) returns an integer with 10 possible values: 0, 1, 2, ..., 8, 9.

35
New cards

How many possible values can be produced by random.randint(-5, 5)?

-5 to 5 has 11 possible values: -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.
Ranges involving negatives are treated the same as ranges with only positives

36
New cards

Unicode

Python uses Unicode to represent every possible character as a unique number, known as a code point. For example, the character "G" has the code point decimal value of 71.

37
New cards

What is the output of
print('10...\n9...')

10...
9...

38
New cards

ord()

returns an encoded integer value for a string of length one.

39
New cards

chr()

returns a string of one character for an encoded integer.

40
New cards

Adding r before the string literal causes all escape sequences in a string to be ignored.

ex: print(r'The escape sequence for new line is \n'

answer: The escape sequence for new line is \n

41
New cards

One approach to output "\\" in a string literal is to add an "r" before the string literal. Alternatively, an additional "\" can be inserted before each existing "\" to escape the backslashes.

42
New cards

What is the formula for the perimeter of a square?

Perimeter = 4 × edge length
(In Python: square_perim = 4.0 * square_edge)

43
New cards

How do you compute and print the square perimeter if edge length is read from input?

44
New cards

How do you fix a NameError?

A: Make sure you spelled the variable name exactly the same everywhere and defined it before using it.
Example:

x = 5 print(x) #

45
New cards

Complete the program as follows:

  1. Read val1 and val2 from input as floats, respectively.

  2. Compute mean_of_two using the formula .

  3. Output 'Mean is ' followed by the value of mean_of_two to three digits after the decimal point.

Ex: If the input is:

10.0 24.0

then the output is:

Mean is 17.000

val1 = float(input())

val2 = float(input())

mean_of_two = (val1 + val2) / 2.0

print(f'Mean is {mean_of_two:.3f}')

46
New cards

How do you compute the average of two numbers in Python?

avg = (num1 + num2) / 2.0

47
New cards
OSZAR »