filmov
tv
module_2_lab_4 python 4th lab

Показать описание
LAB
Estimated time
10-15 minutes
Level of difficulty
Easy
Objectives
• becoming familiar with the concept of numbers, operators, and arithmetic operations in Python;
• performing basic calculations.
Scenario
Take a look at the code in the editor:
x = # hardcode your test data here
x = float(x) ######### type casting
# write your code here y= 3x3 - 2x2 + 3x - 1
print("y =", y)
it reads a float value, puts it into a variable named x, and prints the value of a variable named y. Your task is to complete the code in order to evaluate the following expression:
3x3 - 2x2 + 3x - 1
The result should be assigned to y.
Remember that classical algebraic notation likes to omit the multiplication operator - you need to use it explicitly. Note how we change data type to make sure that x is of type float.
Keep your code clean and readable, and test it using the data we've provided, each time assigning it to the x variable (by hardcoding it). Don't be discouraged by any initial failures. Be persistent and inquisitive.
Test Data
Sample input
x = 0
x = 1
x = -1
Expected Output
y = -1.0
y = 3.0
y = -9.0
output
x = -1
x = float(x)
## y= 3x3 - 2x2 + 3x - 1
y=3*(x ** 3) - 2*(x ** 2) + 3*x -1
print("y =", y)
LAB
Estimated time
5 minutes
Level of difficulty
Very Easy
Objectives
• becoming familiar with the concept of comments in Python;
• using and not using comments;
• replacing comments with code;
• experimenting with Python code.
Scenario
The code in the editor contains comments:
#this program computes the number of seconds in a given number of hours
# this program has been written two days ago
a = 2 # number of hours
seconds = 3600 # number of seconds in 1 hour
print("Hours: ", a) #printing the number of hours
# print("Seconds in Hours: ", a * seconds) # printing the number of seconds in a given number of hours
#here we should also print "Goodbye", but a programmer didn't have time to write any code
#this is the end of the program that computes the number of seconds in 3 hour
Try to improve it: add or remove comments where you find it appropriate (yes, sometimes removing a comment can make the code more readable), and change variable names where you think this will improve code comprehension.
#this program computes the number of seconds in a given number of hours
# this program has been written two days ago
hours = 2 # number of hours
seconds = 3600 # number of seconds in 1 hour
print("Hours: ", hours) #printing the number of hours
print("Seconds in Hours: ", hours * seconds) # printing the number of seconds in a given number of hours
#here we should also print "Goodbye", but a programmer didn't have time to write any code
#this is the end of the program that computes the number of seconds in 3 hour
LAB
Estimated time
5-10 minutes
Level of difficulty
Easy
Objectives
• becoming familiar with the inputting and outputting of data in Python;
• evaluating simple expressions.
Scenario
Your task is to complete the code in order to evaluate the results of four basic arithmetic operations:
# input a float value for variable a here
# input a float value for variable b here
# output the result of addition here
# output the result of subtraction here
# output the result of multiplication here
# output the result of division here
print("\nThat's all, folks!")
The results have to be printed to the console.
# input a float value for variable a here
a=float(input("value of a:"))
# input a float value for variable b here
b=float(input("value of b:"))
print("add: ",a+b)
print("sub: ",a-b)
print("multiply: ",a*b)
print("divide: ",a/b)
print("\nThat's all, folks!")
Estimated time
10-15 minutes
Level of difficulty
Easy
Objectives
• becoming familiar with the concept of numbers, operators, and arithmetic operations in Python;
• performing basic calculations.
Scenario
Take a look at the code in the editor:
x = # hardcode your test data here
x = float(x) ######### type casting
# write your code here y= 3x3 - 2x2 + 3x - 1
print("y =", y)
it reads a float value, puts it into a variable named x, and prints the value of a variable named y. Your task is to complete the code in order to evaluate the following expression:
3x3 - 2x2 + 3x - 1
The result should be assigned to y.
Remember that classical algebraic notation likes to omit the multiplication operator - you need to use it explicitly. Note how we change data type to make sure that x is of type float.
Keep your code clean and readable, and test it using the data we've provided, each time assigning it to the x variable (by hardcoding it). Don't be discouraged by any initial failures. Be persistent and inquisitive.
Test Data
Sample input
x = 0
x = 1
x = -1
Expected Output
y = -1.0
y = 3.0
y = -9.0
output
x = -1
x = float(x)
## y= 3x3 - 2x2 + 3x - 1
y=3*(x ** 3) - 2*(x ** 2) + 3*x -1
print("y =", y)
LAB
Estimated time
5 minutes
Level of difficulty
Very Easy
Objectives
• becoming familiar with the concept of comments in Python;
• using and not using comments;
• replacing comments with code;
• experimenting with Python code.
Scenario
The code in the editor contains comments:
#this program computes the number of seconds in a given number of hours
# this program has been written two days ago
a = 2 # number of hours
seconds = 3600 # number of seconds in 1 hour
print("Hours: ", a) #printing the number of hours
# print("Seconds in Hours: ", a * seconds) # printing the number of seconds in a given number of hours
#here we should also print "Goodbye", but a programmer didn't have time to write any code
#this is the end of the program that computes the number of seconds in 3 hour
Try to improve it: add or remove comments where you find it appropriate (yes, sometimes removing a comment can make the code more readable), and change variable names where you think this will improve code comprehension.
#this program computes the number of seconds in a given number of hours
# this program has been written two days ago
hours = 2 # number of hours
seconds = 3600 # number of seconds in 1 hour
print("Hours: ", hours) #printing the number of hours
print("Seconds in Hours: ", hours * seconds) # printing the number of seconds in a given number of hours
#here we should also print "Goodbye", but a programmer didn't have time to write any code
#this is the end of the program that computes the number of seconds in 3 hour
LAB
Estimated time
5-10 minutes
Level of difficulty
Easy
Objectives
• becoming familiar with the inputting and outputting of data in Python;
• evaluating simple expressions.
Scenario
Your task is to complete the code in order to evaluate the results of four basic arithmetic operations:
# input a float value for variable a here
# input a float value for variable b here
# output the result of addition here
# output the result of subtraction here
# output the result of multiplication here
# output the result of division here
print("\nThat's all, folks!")
The results have to be printed to the console.
# input a float value for variable a here
a=float(input("value of a:"))
# input a float value for variable b here
b=float(input("value of b:"))
print("add: ",a+b)
print("sub: ",a-b)
print("multiply: ",a*b)
print("divide: ",a/b)
print("\nThat's all, folks!")
Комментарии