Midterm

Name: Student Name

What are the four roles of a computer?

Input, processing, storage, output

What is hardware?

The physical parts of a computer

What is software?

Programs that run on hardware

What number system does a computer use to store and process data?

Binary

What is a variable?

A named container for a value

What is a terminal?

A text-based interface for entering commands; a.k.a. console or CLI

What does CPU stand for?

Central Processing Unit

Name the five primitive data types in Python

Integer, floating point number, string, boolean, none type

What data type would you use for a true/false fact?

Boolean

What data type does input() return?

String

Which of these are an example of “concatenation?”

C 'foo' + 'bar'

What is the difference between = and ==?

= assigns a value while == compares a value

Finish the program:

user_prompt = "Enter a number between 1 and 10: "
user_input = input( user_prompt )
user_number = int( user_input )

# TODO

Write an if statement that prints “You picked the middle number!” if user_number equals 5.

if user_number == 5:
    print( "You picked the middle number!" )

Can an else statement exist without an if?

No

What is wrong with this code?

x = int( input() )
if x == 5
    print( "x is 5" )

Missing :

What is wrong with this code?

x = int( input() )
if x == 5:
print( "x is 5" )

Invalid indentation

What data type does / always return?

Divides, and always produces a floating-point number

What does the // operator do?

Floor division. It drops the remainder and produces an integer

What does the % operator do?

Modulus. It gives the remainder

What does the *= operator do?

Multiplies, then assigns the result

What type of value does a comparison such as age >= 18 produce?

A Boolean: True or False

What are the three logical operators in Python?

and, or, and not

What is the output?

print(not True)

False

What is the output?

rylan_is_old = False
rylan_is_teacher = True

if rylan_is_old and rylan_is_teacher:
    print( "Rylan is a wise old sage" )

elif rylan_is_teacher:
    print( "Rylan is worth listening to" )

else:
    print( "Rylan is just a guy" )

Rylan is worth listening to

What is the difference between semantics and syntax?

Semantics is the meaning. Syntax is the words, characters, and grammar

What is a function?

A reusable section of code

What is a parameter?

A name in a function definition for a value the caller passes in

Finish the program:

def compute_sum( x, y ):
    return x + y

print( "Enter two numbers to be summed:" )
x = int( input() )
y = int( input() )

# TODO

Compute the sum using the defined function and print the result.

print( compute_sum(x, y) )

Finish the program:

# TODO

x = int( input() )
x_squared = compute_square( x )
print( x, "squared is", x_squared )

Define the missing function.

def compute_square(x):
    return x * x

Finish the program:

def get_word( word_type ):
    prompt = f"Enter a word that is a { word_type }: "
    return input( prompt )

# TODO

print( f"Once upon a time there was a { noun }." )
print( f"The { noun } was very { adjective }." )
print( f"One day the { noun } decided to { verb } at the { location }!" )
print( "That was a weird day." )
print( "THE END" )

Add the required function calls and assignments.

noun = get_word( "noun" )
adjective = get_word( "adjective" )
verb = get_word( "verb" )
location = get_word( "location" )