Intro to Computer Science: Python latest update with complete verified solutions 2025

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

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.

154 Terms

1
New cards

Computer Science

The study of computation and computational devices

2
New cards

What are the two major components of computation

algorithms and abstraction

3
New cards

algorithm

A set of instructions demonstrating how to explicitly perform a task

4
New cards

Are algorithms always the same?

Most of the steps are standardized, and are thus the same for solving any one of a given class of problems

Certain details may be changed to provide problem-specific solutions

5
New cards

What are the four components of any algorithm?

Finite number of steps

Eventually ends

well-defined, explicit instructions

solves a general class of problems

6
New cards

What are the four ways in which we measure algorithms?

Correctness

Robustness

Efficiency

Clarity

7
New cards

Correctness

The ability of an algorithm to produce the right output for all inputs

8
New cards

Robustness

The ability of an algorithm to handle unexpected inputs

9
New cards

Efficiency

The ability of an algorithm to not take too long when performing a task

10
New cards

Clarity

The ability of an algorithm to be easily understandable and alterable

11
New cards

Abstraction

a simplified representation of something more complex. Abstractions allow you to hide details to help you manage complexity, focus on relevant concepts, and reason about problems at a higher level.

12
New cards

heavy abstraction

removing all fine details, leaving only major components intact.

13
New cards

What does a programming language do?

converts instructions that are provided by the user into machine code output (implements an algorithm)

14
New cards

Python

a high level, very readable, high abstraction coding language

15
New cards

why is Python considered to be high-abstraction

Because it hides all the underlying details behind turning user input into machine code output, focusing only on the major details of what's occurring in a way that's comprehendible.

Hides all the irrelevant data.

16
New cards

Variable

A name that refers to a value

17
New cards

How are variables in math similar to variables in computer science? How are they different?

In both, variables are names that represent values. Additionally, in both, they are often stored for later usage.

In computer science, variables can be redefined repeatedly, unlike in math.

18
New cards

syntax

The form of a statement that we must adhere to whenever inputting statements in Python.

19
New cards

Assignment statement (and syntax)

Uses the equal sign to give the object property on the left of the equal sign the value on the right of the equal sign.

=

20
New cards

Naming rules for python variables (3)

1. Must start with a letter or a _

2. Can only contain numbers, letters, and _

3. Case sensitive

21
New cards

Give the proper terminology for the following:

x = 7

x = 4 + 4

print ( )

x

4

+ , =

Assignment statement

Assignment with expression

Print statement

Variable

Constant

Operator

22
New cards

How does assignment work in Python (three step process)

1. Interprets the variable on the left side of the =

2. Evaluates the statement on the right side of the =

3. Assigns the value to the stored variable in memory

23
New cards

Order of operations (Python)

( ) parentheses.

** exponentiation

* multiplication, / division, % remainder

+ addition, - subtraction

24
New cards

How can we swap variable values in Python (two methods)

1.

temp = x

x = y

y = x

2. Simultaneous assignment

x , y = y , x

25
New cards

Relational Operators in Python

Compare two values and return a Boolean

==, !=, >=, <=, >, <

26
New cards

Logical Operators in Python

Check for a condition and return a Boolean

And, Not, Or

27
New cards

Explain how and, not, and or work

and - if all conditions are true, output is true

or - if at least one condition is true, output is true

not - if condition is false, output is false

28
New cards

Describe the precedence for the logical operators in Python

Not first, then and, then or

29
New cards

Data Type (Python)

An implicitly understood class of data that is given to all variables, literals, and constants in Python

30
New cards

What are the five main data types

List, string, Boolean, character and integer

31
New cards

How can we check the data type in Python?

type ( )

32
New cards

How does "+" work differently for numbers and strings in Python

With strings, it will concatenate whatever is within the ""

With numbers, it will output a sum

33
New cards

float vs. integer. Which one has precedence (i.e., if you entered 7 + 7.0, would the response be a float or an integer)?

floats are decimal values, integers are integers

floats take precedence (14.0)

34
New cards

How do we convert values into different types

float ( integer ) = integer.0

int (float ) = float rounded down

str ( ) = " "

35
New cards

What are the other two numeric data types in Python (not float or integer)

Complex numbers (x+yi) and long integers.

36
New cards

What do we call a string with a single character?

Character

37
New cards

Lists vs. Tuples

Both can contain multiple items of varying data types

You can change the items in a List, while a Tuple is locked

Lists are represented with [ , , , ], while Tuples are represented with ( , , , )

38
New cards

how can we prompt a user for input

input ( " " )

will display whatever is in the "" to the user

39
New cards

what data type do input statements return by default?

string

40
New cards

how can we request numerical input?

float (input ( ) )

int (input ( ) )

41
New cards

how do we implement comments in Python

#, anything after this will be ignored

42
New cards

Why is # so important in Python?

Because it allows for comments, which can improve the clarity and readability of your code

also because you can use it to "turn off" certain lines of code while debugging

43
New cards

Booleans

a data type that contains the values True and False

44
New cards

what are the two values in the Boolean data class?

True, False (capitalized)

45
New cards

how do relational operators work with individual letters?

letters are compared to one another based on their order, from a-z-A-Z

no real values assigned to each letter, just ambiguous assignment that compares their ordered position

46
New cards

how do relational operators work with strings?

The first letters are compared. If they match, the second letters are compared, and so on.

47
New cards

what is False in Python

any empty data structure (0, 0.0, empty string, None objects (data type), etc.

everything else is True

48
New cards

When does Python stop evaluating? (give two examples for and and or statements)

Python stops evaluating when it knows the value of a Boolean. For example, if we have:

1 > 2 and 4>2, Python will stop evaluating after the first statement, because it already knows that the entire statement is False

alternatively, if we have 2 > 1 or 4 > 2, Python will stop evaluating after the first statement, because it already knows that the entire statement is True

49
New cards

What statements should we avoid when attempting to keep our Booleans simple?

== True

== False

double negatives

50
New cards

if statement

A line that determines whether or not you run a certain chunk of code

51
New cards

if-else statement

allows your program to perform one action if the Boolean expression evaluates to true and a different action if the Boolean expression evaluates to false.

identical to if statements, but adds an alternative set of instructions for False Boolean instances, whereas if statements just stop/skip in this instance

52
New cards

if-elif-else statement

similar to if-else, but allows for different conditions

can end in else, but doesn't need to

53
New cards

if-elif-else syntax

if

elif

else

can have as many elifs as you want

54
New cards

for loop

Loops that have a predetermined beginning, end, and increment (step interval).

55
New cards

parameter specifications for for loops

1 parameter = end (start set to 0, increment to 1)

2 parameters = start and end (increment set to 1)

3 parameters = start, end, and parameter

56
New cards

when will a for loop stop executing

when the value equals or exceeds the end parameter (will not run the final value when this occurs)

57
New cards

does a for loop have automatic incrementation? what about a while loop?

for loop - yes

while loop - no (you have to implement it or the loop will never terminate)

58
New cards

does a for loop require outside initialization? what about a while loop?

for loop - no, it will initialize at the start parameter

while loop - yes, you need to predefine the variable

59
New cards

when do we use for loops?

when we're applying computation to every item in a collection

60
New cards

when do we use while loops?

when we're attempting to get to a certain condition

61
New cards

can we have float increments?

no

62
New cards

what happens in a nested for loop?

for every incrementation of the outside loop, the entirety of the inside loop is run

63
New cards

break

command used to terminate the current loop immediately

use with caution, because it can make your code hard to read

64
New cards

Try-Except Statement

Python executes all of the statements underneath try.

If there is no error, then Python does nothing and skips over all the statements underneath except.

However, if Python crashes while inside the try portion, it recovers and jumps over to except, where it executes all the statements underneath there.

65
New cards

what can you do to an except statement to make it more versatile

you can type except to prompt specific sets of commands for specific error types

66
New cards

when do we use functions in python?

when we want to perform the same operation repeatedly throughout a program.

67
New cards

how can functions make debugging easier?

you only have to fix one instance of the buggy code, rather than every individual instance of the body within the program

68
New cards

how do we define a function? how do we call a function? (syntax)

def ():

()

69
New cards

function

reusable code that takes arguments as input, performs a computation, and returns a result

70
New cards

global keyword

allows you to use a non-parameter function within a body of statements

71
New cards

is variable definition/reassignments within functions local or global (i.e., if you define or reassign a variable value within a function, will the changes/definition be present outside of the function).

local, any defined variables and any variable value changes will not be present outside of the function

72
New cards

parameter

a piece of information that a function needs to work properly (one that is not the same in every use of the function)

73
New cards

how many parameters can a function have?

can have none, can have (theoretically) an infinite amount

74
New cards

what does a return statement do?

the value after the return command will be evaluated and associated with the specific function call, which can then be used outside of the function. After this assignment, the function ends.

e.g.

funcname (x):

return 2x

print(funcname(7))

-> 14

75
New cards

print vs. return

return will store the returned value in the specific function call in question, whereas print will simply display any values on the screen (following the function, these values are destroyed and cannot be referenced again).

76
New cards

what is the associated value of any function call that does not have a return statement?

None

77
New cards

how do we write a function (5 steps)?

1. figure out the intended behavior of the function

2. define what types of data we want to put in and get out

3. write the header

4. add a description of what it will do in a comment

5. fill in the body

78
New cards

what are the two types of functions in python?

defined functions and built-in functions

79
New cards

where did the term "bug" come from?

in the 1940s, Navy Admiral Grace Hopper found a moth in a computer that was being worked on. From this point on, technical errors became known as bugs.

80
New cards

what are some of the major causes of bugs?

syntax (brackets, colons, indentation, etc.), variable naming (case-sensitivity, naming rules, etc.), and function formatting (what orders are the parameters being used it, is the output what we expect it to be, etc.), among others.

81
New cards

module

a python file containing a collection of functions

82
New cards

how do we import modules

import

83
New cards

what must be true if we are to import a self-made module?

the python file being imported must be on the same device as the interpreter

84
New cards

how do we call a function within a module

.

85
New cards

how do we import a specific function from a module

does this make a difference?

from import

when a function is imported in this way, we don't have to use dot notation when we call the function

86
New cards

overloaded operator

An operator that can operate on two or more types of operands (+ and * serve different functions when working with strings, integers, floats, etc.).

87
New cards

what are the two ways we can iterate through strings? How are these two ways different from one another?

for in :

for in range (len()):

in the first case, the iterating variable is a character in the string; in the second case, the iterating variable is a number corresponding to an index position.

88
New cards

slicing a string

the process of finding a subset of characters from a string.

[x:y] (range of char including x index, but not y)

[x:] (range from x index to end of string)

[:y] (range from beginning of string to 1 before y)

[x] (x index of string)

[:] (whole string)

89
New cards

can we use negative index positions when slicing a string?

yes, it just counts down from the last character to the first

the last character has an index of -1

90
New cards

how can we check if a string is contained within another string?

in

91
New cards

what is the order of characters that python uses for relational operators

capitals have lower value than lower case levels

lower alphabet letters have lower value than higher alphabet letter

z has higher value than any other letter, A has the lowest

92
New cards

method

a function that is specific to one type of object

93
New cards

how do we call functions?

.( )

94
New cards

strings are immutable. How can we alter a string given this reality?

none of its methods can change the content of a String object

we can only alter strings through reassignment

95
New cards

what function can we use to find out all of the methods associated with a data type?

dir(datatype)

96
New cards

what does .find() do?

returns the beginning index of the first occurrence of the argument within a given string

97
New cards

what does .replace(x,y) do?

replaces all x with y in a string

98
New cards

what does .count() do?

returns the number of times a substring occurs within a given string

99
New cards

what do .startwith() and .endwith() do?

return True when the start/end value matches that in the parameter, False otherwise

100
New cards

lists

data structures that let us store collections of data with associated indices