LAB
Objectives
Scenario
Take a look at the code in the editor: it reads a float value, puts it into a variable named x, and prints the value of the 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 tit explicitly. Note how we cahnge data type to make sure that x is of type float.
Keep your code clean and readable, and text is 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
Key Takeaways
var = 2
print(var)
var = 3
print(var)
var += 1
print(var)
You can combine text and variables using the + operator, and use the print() function to output strings and variables, e.g.: (2.1.4.4)
var = "007"
print("Agent " + var)
Exercise 1
What is the output of the following snippet?
var = 2
var = 3
print(var)
3
Exercise 2
Which of the following variable names are illegal in Python?
my_var
m
101 - incorrect (starts with a digit)
averylongvariablename
m101
m 101 - incorrect (contains a space)
Del
del - Incorrect (is a keyword)
Exercise 3
What is the output of the following snippet?
a = '1'
b = "1"
print(a + b)
11 it prints them next to each other does not add them.
Exercise 4
What is the output of the following snippet?
a = 6
b = 3
a /= 2 * b
print(a)
1.0
2 * b = 6
a = 6 → 6 / 6 = 1.0