Python Exam 1

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

1/84

flashcard set

Earn XP

Description and Tags

week 1-5: operators, conditional logic, int, float, eval, lists, loops,

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

85 Terms

1
New cards

program

set of instructions written in a programming language that a computer can execute

2
New cards

programming

the process of creating programs

telling a computer how to solve a problem or

perform an action.

3
New cards

programming includes

  • Problem solving (breaking problem into steps)

  • Writing instructions in a programming language

  • Testing and debugging

4
New cards

assembly languages

First step beyond binary — uses symbols instead of just 0s and 1s

Still low-level: You write instructions that directly control the CPU and memory

5
New cards

High-Level Languages (like Python, Java,

C++)

Easier and more expressive for

humans. They are compiled or interpreted

into machine code

6
New cards

interactive mode

Run Python directly in the shell (>>>prompt)

Each line runs immediately and shows output

Good for quick testing

7
New cards

script mode

Write code in a file (.py) and run it

Runs the whole program at once

Use print() to see results

8
New cards

print()

Displays output to screen

the instruction that tells Python to show something on the screen

9
New cards

immutable

cannot be changed after creation

Any operation that alters the value will create a new object in memory

10
New cards

mutable

can be updated after creation, without creating a new object.

11
New cards

immutable data types

  • number

  • strings

  • tuples

12
New cards

mutable data types

  • list

  • dictionary

  • sets

13
New cards

string

a sequence of characters

Can include letters, digits, or symbols

Written inside 'single', "double", or '''triple''' quotes

14
New cards

numeric data

integers and floats

widely used in calculations

15
New cards

integers

whole numbers no decimals

16
New cards

floats

decimal numbers

17
New cards

Boolean

true or false

18
New cards

tuples

ordered, immutable collections

19
New cards

lists

ordered collection of objects

ordered, changeable collections

20
New cards

dictionaries

key-value pairs

21
New cards

sets

unique items, no order

22
New cards

variables

a name that stores data

Variables point to a value, but we often say the

variable is the value. They can later be changed

to point to something else

23
New cards

variable rules

Must begin with a letter or underscore

Can include letters, digits must begin with a letter or underscore, and underscores

built-in-function name cannot be a variable

24
New cards

assignment statement

gives a variable a value using the ‘=’ sign

25
New cards

input()

lets users type values while program runs

Program pauses until user presses Enter

always returns string

26
New cards

type conversion

int() → number

float() → decimal

str() → text

bool() → True/False

27
New cards

,

automatically spaces values

28
New cards

+

if both sides are numbers, + means add them.

If both sides are strings, + means join them together.

29
New cards

expression

An expression combines values and operators to

produce a result

30
New cards

eval()

expressions with operators/parentheses

takes a string and evaluates it as Python code.

If the string looks like math, it will compute the result.

If the string is a variable name or other valid Python code, it will also try to run it

31
New cards

abs()

absolute value

32
New cards

pow(a, b)

a raised to b

33
New cards

typing rules

Case sensitive

Spaces: indentation matters in Python

Errors appear if spaces are used incorrectly

34
New cards

sep=" "

ensures the words have spaces between them

35
New cards

if statement

used to make decisions in code

checks a Boolean condition (True/False).

If the condition is True, the indented code runs.

If the condition is False, that code is skipped

indentation shows which code belongs to the if

36
New cards

**

exponentiation

37
New cards

//

integer division

gives you the whole number result (rounded down) of a division

38
New cards

%

modulo remainder

Returns the remainder from a division

39
New cards

< >

comparison operator

40
New cards

==

equality operator

41
New cards

!=

not equal to operator

42
New cards

not operator

Reverses the Boolean value of the operand

If the operand is True, it returns False, and vice versa

43
New cards

and operator

Returns True if both operands are True

Otherwise, returns False

44
New cards

or operator

Returns True if at least one operand is True

Returns False only if both operands are False

45
New cards

else statement

executes when the condition is false

46
New cards

elif

When you want to test multiple conditions sequentially in a chained if-else block

47
New cards

type()

returns the type (or class) of the given object useful for inspecting the type of variables or objects at runtime

48
New cards

sys.exit()

ends a Python program immediately

sys.exit(0)

The number 0 means the program exited normally (no error)

Before using sys.exit(), you must import the sys module

49
New cards

random.randint(min, max)

random integer (inclusive range)

must first import random

50
New cards

time.asctime()

returns current time as a string

51
New cards

time.sleep(secs)

pauses the program for secs seconds

52
New cards

random.gauss(mu, sd) 

random float from Gaussian distribution

mu: The mean (average) of the distribution

sigma: The standard deviation (spread) of the distribution

53
New cards

while loop

repeat executing their [indented] code block until the condition becomes false

54
New cards

for loop

repeats something a specific number of times (for loop range (start,stop,step) or range(start,stop))

 A for loop in Python does not include the last stop value from the range

55
New cards

break

stops the loop immediately, even if the condition is still true

56
New cards

continue

skips the rest of the current iteration and continues with the next

57
New cards

list() or []

list function

Create a list with comma-separated values inside square brackets

58
New cards

list index

position number, in square brackets

access an individual element of a list

starts with 0 counting up from the beginning

Or -1 counting down from the end

59
New cards

in

in a condition checks membership (True/False).

in a for loop provides each value as the loop iterates

checks whether a value is in a list (True/False).

Use with if/while for validation.

Use 'not in' to check absence

60
New cards

len()

function to get length of list

61
New cards

list.append(value)

add an item to the end of a list

62
New cards

list.pop(position)

removes item at position

63
New cards

print(list.pop() )

Get and remove the last element

64
New cards

list.sort()

sort a list in place (ascending by default)

65
New cards

list[start:end]

list slicing

66
New cards

slicing

extracts or assigns a range of elements

start is inclusive

end is exclusive indexes separated by a colon

Slices can be open-ended

Slicing can also replace or remove parts, growing or shrinking the list

67
New cards

list[start : end : step]

list slicing using step

start: The index where the slice begins (inclusive). Defaults to 0 if omitted.

stop: The index where the slice ends (exclusive). Defaults to the end of the list if omitted.

step: The interval between elements. Defaults to 1 if omitted.

68
New cards

x.copy()

x[:]

[0:len(x)]

copy

69
New cards

x % 2 == 0

checks if divisible by 2 (even numbers)

70
New cards

range()

generates a sequence of numbers within range, which is commonly used with a for loop to iterate over a specific range of values

inclusive start

exclusive stop

range (start, stop, step)

71
New cards

sum()

adds values within parentheses

72
New cards

min(list)

returns smallest item in a list

73
New cards

max(list)

returns largest item in a list

74
New cards

list[::-1]
list.reverse()

reverse list

75
New cards

list[::2]

even items

76
New cards

+=

adds a value to a variable and then assigns the result back to that variable

77
New cards

[] + []

adds one list to the end of another

78
New cards

[]*value

repeats the list for value

79
New cards

L=[1,2,4,7]

for i in range(len(L)):

print(L[i])

prints out the items of a list, one-by-one, on separate lines

80
New cards

average = sum(list) / len(list)

computes the average of the values in a list

81
New cards

print(list.count(“x”))

returns the number of times x occurs in a list

82
New cards

print(name.index("x"))

returns the location of the first occurrence of x

83
New cards

list.remove(“x“)

removes the first occurrence of x from the list

84
New cards

list.insert(position,”x”)

inserts x at index position on the list

85
New cards

list = [1,2,3,4]

for i in range(len(list)):

list[i] = list[i]**

Replaces each element in a list with its square