PYTHON PROGRAMMING 10

 

separator-blank.png

 

PART 1 – INTRO TO PYTHON PROGRAMMING

 

PROGRAMS

 

PROGRAM 1 - DISPLAY ON SCREEN (print)

 

print("hello world")
 
print("hello", "world")
 
print("hello")
print("world")

 

PROGRAM 2 - STRINGS (string variable, +, string output)

 

msg1 = "Hi there!  "

msg2 = "I hope you are well!  "

msg3 = "Take care!  "

fullmsg = msg1 + msg2 + msg3

 

print(fullmsg)

 

PROGRAM 3 - USER INPUT (input)

 

name = input("What's your name?")
print("Hello", name)

 

 

TASK 1

Write the program that asks the user for their first name, then their middle name and then their last name and greet the person. Can you figure out how to add an exclamation mark at the end of the greeting without having a space between the last name and it?

 

SAMPLE IO

What is your first name? Patrick
What is your middle name? Omer
What is your last name? Campeau
Well hello there Patrick Omer Campeau! 

 

 

PROGRAM 4 - NUMERIC VARIABLES (float, converting from string, input, simple arithmetic)

 

Area of rectangle

 

length = 3
width = 5
area = length * width
print("The area is", area)

 

Area of rectangle with user input

 

length = float(input("What is the length?"))
width = float(input("What is the width?"))
area = length * width
print("The area is", area)

 

Combining string and numeric variable with str(…)

 

#Ask the user for two numbers and add them up.
num1 = input("Enter a number")
num2 =
input("Enter another number")
total =
float(num1) + float(num2)

msg =
"The total is " + str(total) + "."
print(msg)

 

 

 

 

TASK 2

 

Consider the program that calculates the area of a rectangle.  Add the statements required to make it also calculate the perimeter of the rectangle.

 

SAMPLE IO

 

What is the length? 6

What is the width? 5

The area is 30.

The perimeter is 22.

 

 

PROGRAM 5 – MORE ARITHMETIC

 

Apple purchase program

 

appleCount = 9
applePrice = 1.25      #per apple
total = appleCount * applePrice
print("The total cost is ", total)

 


TASK 3

Consider the apple purchase program and add the statements required to calculate the total cost after a 6% tax is added to the cost.  Remember that 6% is simply 0.06.

 

 

 

TASK 4

 

Can you change the above program so that the appleCount and applePrice variables get values that are inputted by the user?

Remember that you need to convert the input to a number before using it.  The float function does this for us.

 

 

separator-blank.png