2. Elementary Programming
Lecture 2.
Elementary Programming
92 minutes video lecture, 20 minutes coding practice time.
Slides
Video Lectures
Review Questions
1: input() statement reads in a value from the user as numeric. (T/F)?
False
2: What does the following program print?
radius = 7.5
print ("radius")
Click here to see the answer.
radius
3: What does the following program print?
radius = 7.5
print (radius)
Click here to see the answer.
7.5
4: Which of the following are valid identifiers?
- a
- +app
- 3number
- radiusOfTheCircle
- $2
- d+7
- True
Click here to see the answer.
a, radiusOfTheCircle
5: What is the value stored in count?
count = 7 / 3
Click here to see the answer.
2.3333
6: What is the value stored in test?
test = 7 % 3
Click here to see the answer.
1
7: What is the value stored in count?
count = 7 // 3
Click here to see the answer.
2
8: What is the value stored in test?
test = 7.5 // 3
Click here to see the answer.
2.0
9: What is the value stored in x?
x = 5
x /= 2
Click here to see the answer.
2.5
10: What is the value stored in test?
test = 5
test += test + 1
Click here to see the answer.
11
11: What is the value stored in x?
x = 5
x //= 2
Click here to see the answer.
2
In-class Programming Exercises
1: Write a program that reads in an input as the radius of a circle from the user, and calculates and prints the area of a circle.
# In-class Exercise 1
# Step 1: Read in radius from the user
radius = input("Please input the radius of a circle and press Enter: ")
radius = float(radius)
# Step 2: Compute area
area = radius * radius * 3.14159
# Step 3: Display the area
print("The area of a circle with the radius", radius, "is", area)
2: Write a program that reads in a Celsius degree from the user, converts Celsius to Fahrenheit degree, and displays the result.
# In-class Exercise 2
# Step 1: Read in Celsius degree from the user
celsius = input("Please input the Celsius degree and press Enter: ")
celsius = float(celsius)
# Step 2: Convert Celsius to Fahrenheit degree
fahrenheit = (9/5) * celsius + 32
# Step 3: Display the result
print("Celsius degree of", celsius, "is equal to", fahrenheit, "Fahrenheit degree")
3: Write a program that reads three numbers from the user and displays their average.
# In-class Exercise 3
# Step 1: Read in the three numbers from the user
number1 = float(input("Please input the first number and press Enter:"))
number2 = float(input("Please input the second number and press Enter:"))
number3 = float(input("Please input the third number and press Enter:"))
# Step 2: Calculate their average
average = (number1 + number2 + number3) / 3
# Step 3: Display the result
print("The average of the three numbers is", average)
4: Write a program to obtain minutes and remaining seconds from an amount of time in seconds. Example : Convert 200 seconds to 3 minutes and 20 seconds.
# In-class exercise 4
# Step 1: Read in the time in seconds from the user
timeInSeconds = float(input ("Please input the time (in seconds) and press Enter:"))
# Step 2: Convert the time to minutes and seconds
minutes = int ( timeInSeconds // 60)
seconds = timeInSeconds % 60
# Step 3: Display the result
print(timeInSeconds , "seconds equals to", minutes, "minutes and", seconds, "seconds")
Practice-at-home Programming Exercises
1: Write a program that reads a two digit integer from the user and swap its digits to create a new integer. Example: if the integer is 93, after swapping it becomes 39.
# Practice exercise 1
# Step 1: Read in the two-digit number from the user
twoDigitNumber = int (input ("Please input a two-digit number and press Enter:"))
# Step 2: Swap its digits and create a new integer
firstNumberTemporary = twoDigitNumber // 10
secondNumberTemporary = twoDigitNumber % 10
numberAfterSwap = secondNumberTemporary * 10 + firstNumberTemporary
# Step 3: Display the result
print("After the swap, the new number is", numberAfterSwap)
2: Write a program that reads numbers for radius and length from the user, and displays the volume of a cylinder on console. The formulas for area and volume are as follows: area = radius * radius * pi; volume = area * length.
# Practice Exercise 2
# Step 1: Read in radius and length from the user
radius = float(input("Please input the radius of a cylinder and press Enter: "))
length = float(input("Please input the length of a cylinder and press Enter: "))
# Step 2: Compute volume
area = radius * radius * 3.14159
volume = area * length
# Step 3: Display the area
print("The volume of a cylinder with the radius", radius, ", and length", length, "is", volume)
3: Write a program that reads the values of x and y from the user, and display the result after the following calculation.
$$ y^{x-7} + \frac{x+y}{4} - \frac{2(x-y) + 3}{5} + \frac{y}{3x - 10} $$
Click to see the answer.
# Practice Exercise 3
# Step 1: Read in x and y
x = float(input("Please input x and press Enter: "))
y = float(input("Please input y and press Enter: "))
# Step 2: Compute the answer
result = pow(y, x-7) + (x+y)/4 - (2*(x-y)+3)/5 + y/(3*x-10)
# Step 3: Display the result
print("The result is", result)