Python Class - Midterm

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output of these statements: a = 4 b = "5" print( a * b ) 9 20 "45" "5555"

"5555"

Which of the following returns a string 'ALL'? 'all'.capitalize() 'all'.upper() 'ALL'.capitalize() 'ALL'.lower()

'all'.upper()

In Python, how does / differ from // For example, how would the output of these two statements differ? You don't need to give the actual result of the print statement. Just describe how they would differ. print( 10/3 ) print( 10//3 )

/ : result of division in decimal places. 3.333333 // : integer division. Fractional part is discarded. 3

Write out the sequence of numbers that would be generated from these Python statements: for x in range(0, 10, 2): print( x, end=" ")

0 2 4 6 8

What gets printed when these statements execute? x = 10 if ( x > 5 ): x = x + 2 if ( x < 11 ): x = 100 print( x )

12

How many times will this loop execute (what is the value of x after execution)? x = 0 for i in range(6): for j in range(4, 1, -1): x = x + 1

18

What will be the output of this program? def cube(x): answer = x * x return answer answer = 2 result = cube(2) print( answer, result )

2 4

A pixel uses three bytes to represent a color. How many different colors can be specified using three bytes? 2^3 = 8 colors 2^8 = 256 colors 2^10 = 1024 colors 2^24 = 16777216 colors

2^24 = 16777216 colors

Which of the following are not valid variable names? Circle all that are not valid variable names. current_speed Celsius 2timesHarder else

2timesHarder else

Given the list variable x = [1, 3, 5, 7, 9, 11]. What is the output of print( x[x[1]] )?

7

Python uses which symbol to make the following line a part of the current statement? # ! // :

:

What is the difference between = and == in Python? When might you use each one?

= is an assignment operator == is "equal to". Comparison operator

In your own words, define what a variable is and how they are used in Python.

A variable is a place where we can store a value. In Python using an assignment statement we can create a variable with a value. To retrieve a value from a variable we can simply use it in an expression.

True of False. This is not a valid set of statements in Python. We cannot set something equal to itself plus 1. sum = 10 sum = sum + 1

False

True or False. It is not possible to convert between data types in Python. For example, if I have a variable x that contains the value 1.5. There exists no way to convert this to an int variable.

False

True or False. Lists can only contain the same type of variables. For example, we can create a list of all integers, a list of all strings, or a list of all floats, but we cannot mix data types within the same list.

False

True or False. Python functions do not accept input from a file. In other words, there exists no way in which to define a Python function that takes inputs from a separate file.

False

True or False. The following Python code will execute without error. myVariable = "Welcome to Python" print( MyVariable )

False

Provide an example of when you might use an integer variable and an example of when you might use a floating point variable.

Integer variable: age, counter, number of days before graduation, ... Floating point variable: GPA, probability, speed, distance, ...

You want to create a program that calculates area of a triangle when you are given the length of three sides using Heron's Formula. When a, b, and c are the lengths of three sides, the area is where Follow steps to create the program. Make a comment: your name, date, and the title of the program (2 point) Import the math library (2 point) Define a main() function (2 points) Ask the user to input the three lengths of a triangle. (a, b, and c) (3 points) If the user enters a value of zero or negative number for any length, print The side length of a triangle should be greater than zeroand quit the program (use return statement). (2 points) If sum of lengths of any two sides is less than the third one, print The three lengths cannot make a triangleand quit the program. (3 points) Create a variable, s with the value of .(2 points) Create a variable, area with the value of (3 points) Print The area of a triangle with side length __, __, and __ is __. (3 points) Call main() function. (2 points) Sample: >>> main() Type three sides of a triangle: 3,4,5 The area of a triangle with 3, 4, and 5 is 6.0 >>> main() Type three sides of a triangle: 5,0,6 The side lengthof a triangle should be greater than zero >>> main() Type three sides of a triangle: 3,4,8 The three lengths cannot make a triangle >>>

Possible answer: # YOUR NAME # DATE # Area of a triangle import math def main(): a, b, c = eval(input("Type three sides of a triangle: ")) if (a <=0 or b <= 0 or c <=0): print("The side lengthof a triangle should be greater than zero") return if (a+b<c or b+c<a or c+a<b): print("The three lengths cannot make a triangle") return s = (a + b + c) / 2 area = math.sqrt(s*(s-a)*(s-b)*(s-c)) print("The area of a triangle with ", a, ", ", b, ", and ", c, " is ", area, sep="") main()

Which is not true of a comment in Python? They make a program more efficient by making it execute faster They are intended for human readers They are ignored by Python when executing your program

They make a program more efficient by making it execute faster

True or False. The Python int data type is identical to the mathematical concept of an integer.

True

What gets printed when these statements execute? x = "world health organization" for w in x.split(): print(w[0].upper(), end="")

WHO

Create two additional variables first and last which keeps the first name and the last name respectively. (6 points)

first = name[:space] last = name[space+1:]

In Python, getting user input is done with which built-in function? print read input get

input

Write a statement to read a full name from the user and store it into a variable name. The statement shows a prompt, "What is your name?" (4 points)

name = input("What is your name?")

Which of the following is the same as p[0:-1]? p[-1] p[:] p[:len(p)-1] p[0:len(p)]

p[:len(p)-1]

Print the name in the form of "Last_name, First_Initial." For example, if the input is "Kyongil Yoon", the output is "Yoon, K." (4 points)

print(last + ", " + first[0].upper())

Which of the following is not a Python data type? bool float rational str

rational

A function can send output back to the main program using print assign return sendBack

return

Define a variable, space, which keeps the position of space in the full name. Assume that the full name has only one space in it. In other words, it is "FirstName LastName". If the input is "Kyongil Yoon", space will be 7. (4 points)

space = name.find(" ")

Assume that the variable x contains an integer. Which of the following would convert x to a string? int(x) str(x) float(x) makeString(x)

str(x)

Identify, and correct, the error in the following set of Python statements. (It calculates the twice of the second digit of the input. For instance, when x is 46, z should be 12 which is 6*2.) x = input("Enter a two digit number ") y = x[1] # use indexing to get the right-most digit z = y * 2 # multiply the right-most digit by 2

x and y are string variables. So y * 2 is not multiplication but repetition. z = y * 2 should be corrected to z = int(y) * 2


Conjuntos de estudio relacionados

Electrical Systems Vocabulary Practice

View Set

The Origins of Democracy Questions

View Set

Parts of Speech (DGP Monday Notes)

View Set

Med Surg II - Chapter 67 - Care of Patients with Kidney Disorders

View Set

Dominio 3 Risk Identification, Monitoring

View Set

Chapter 14 The Behavior of Gases Test Review

View Set

CLS201 - Intro to Medical Emergencies

View Set