Questions from text Chapter 2

Ace your homework & exams now with Quizwiz!

What is the value of the following expression: 16 - 2 * 5 // 3 + 1 (A) 14 (B) 24 (C) 3 (D) 13.667

(A) 14

What value is printed when the following statement executes? print(18 / 4) (A) 4.5 (B) 5 (C) 4 (D) 2

(A) 4.5

What is the value of the following expression: 2 ** 2 ** 3 * 3 (A) 768 (B) 128 (C) 12 (D) 256

(A) 768

What is printed when the following statements execute? n = input("Please enter your age: ") # user types in 18 print ( type(n) ) (A) <class 'str'> (B) <class 'int'> (C) <class 18> (D) 18

(A) <class 'str'>

What value is printed when the following statement executes? print( int(53.785) ) (A) Nothing is printed. It generates a runtime error. (B) 53 (C) 54 (D) 53.785

(B) 53

True or False: the following is a legal variable name in Python: A_good_grade_is_A+ (A) True (B) False

(B) False (The + character is not allowed)

How can you determine the type of a variable? (A) Print out the value and determine the data type based on the value printed. (B) Use the type function. (C) Use it in a known equation and print the result. (D) Look at the declaration of the variable.

(B) Use the type function. Ex. print(type("what is this")) <class "str">

What is printed when the following statements execute? x = 12 x = x - 1 print(x) (A) 12 (B) -1 (C) 11 (D) Nothing. An error occurs because x can never be equal to x - 1.

(C) 11

What is printed when the following statements execute? x = 12 x = x - 3 x = x + 5 x = x + 1 print(x) (A) 12 (B) 9 (C) 15 (D) Nothing. An error occurs because x cannot be used that many times in assignment statements.

(C) 15

What value is printed when the following statement executes? print(18 // 4) (A) 4.25 (B) 5 (C) 4 (D) 2

(C) 4

What is printed when the following statements execute? day = "Thursday" day = 32.5 day = 19 print(day) (A) Nothing is printed. A runtime error occurs. (B) Thursday (C) 32.5 (D) 19

(D) 19

What value is printed when the following statement executes? print(18 % 4) (A) 4.25 (B) 5 (C) 4 (D) 2

(D) 2

What is the data type of 'this is what kind of data'? (A) Character (B) Integer (C) Float (D) String

(D) String

After the following statements, what are the values of x and y? x = 15 y = x x = 22 (A) x is 15 and y is 15 (B) x is 22 and y is 22 (C) x is 15 and y is 22 (D) x is 22 and y is 15

(D) x is 22 and y is 15

Module (%) function

- % means divide - then prints remainder Ex. remainder+ 100 %3 print(remainder) 1 (33+ 33 + 33 + 1) - When first number is smaller than second, it can not be divided at all Ex. remainder= 3%4 print(remainder) 3

Data types

- Integer (int) -String (str)

input function

- always returns string value

Strings

- enclosed in quotation marks - an int can be a string if it is enclosed in quotation marks

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer.

- num_miles= int(input("Enter the number of miles driven: ")) - num_gallons= int(input("Enter the number of gallons used: ")) - print("There is: ", num_gallons, "gallons used per: ", num_miles, "miles driven")

Integers

- numbers - float: a number with a decimal * floating functions will always return a float

The int function can take a floating point number or a string, and turn it into an int

- print(3.14, int(3.14)) - 3.14 3

The type converter float can turn an integer, a float, or a syntactically legal string into a float.

- print(float("123.45")) - 123.45

The type converter str turns its argument into a string

- print(str(17)) - 17

len function

- returns the number of characters in a string Ex. x = len("hello") print(x) 5

number= 2 + (3 - 1) * 10 / 5 * (2 + 3) print(number)

1. (3-1) & (2 +3) --> 2+(2) * 10/5 * (5) 2. 10/5 --> 2 +(2) * 2 * (5) 3. (2) *2 & 2*5 --> 2 +20 4. 2 + 20 --> 22

PEMDAS: order of precedence

1. Parenthesis () 2. Exponents (**) 3. Multiplication & division (*, /, //, %) * RIGHT to left 4. Add and subtract (+, -) * normal left to right

Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years. (A= P(1+r/n)^(nt))

P = 10000 n = 12 r = 0.08 t = int(input("Compound for how many years? ")) final = P * ( ((1 + (r/n)) ** (n * t)) ) print ("The final amount after", t, "years is", final)

Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

a= "All" b= "work" c= "and" d="no" e= "play" f= "makes" g= "Jack" h= "a" i= "dull" j= "boy" print(a , b , c , d , e , f , g , h , i , j)

Write a program that will convert degrees celsius to degrees fahrenheit.

c= int(input("Enter number of degrees Celsius: ")) f= c * 1.8 + 32 print(c , "degrees celsius equals: ", f , "degrees farenheit")

Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off. * Need to convert input function to int in order to retrieve an int/number input

current_time= int(input("What is the current time?") wait_time= int(input("What is the wait time?")) hours= current_time + wait_time alarm_time= hours % 24(bc in hours) print(alarm_time)

str_seconds = input("Please enter the number of seconds you wish to convert") total_secs = int(str_seconds) hours = ? secs_still_remaining = ? minutes = ? secs_finally_remaining = ? print("Hrs=", hours, "mins=", minutes, "secs=", secs_finally_remaining)

hours = total_secs // 3600 secs_still_remaining = total_secs % 3600 minutes = secs_still_remaining // 60 secs_finally_remaining = secs_still_remaining % 60

Write a program that will compute the area of a circle. Prompt the user to enter the radius and print a nice message back to the user with the answer.

pie= 3.14 r= int(input("What is the radius?")) A= pie * r **2 print("The area of the circle is: ", A)

Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.

start_day= int(input("What day did you start?")) length_stay= int(input("How long is your stay?")) end_date= start_day + length_stay end_date= end_date % 7 *(7 because 0 is 1 of 7 values)

Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.

w= int(input("Enter an amount for width:")) h= int(input("Enter an amount for height:")) a= w * h print("The area of the rectangle is: ", a)

Initializing and updating a variable:

x = 6 print(x) x= x + 1 print(x) 6, 7


Related study sets

Ch 15 Endometriosis and Adenomyosis

View Set

Chapter 6 Meiosis is the basis of sexual reproduction

View Set

Solving Systems: Introduction to Linear Combinations: Quiz

View Set

AP Biology Unit 7 Introduction to Natural Selection

View Set

Social Psychology - Compulsory reading

View Set