When building functions, debugging scripts, or writing code in general, you'll need to add come commentary to make sure evrything is working as it should. This is where the print() function comes in. This tutorial will cover the following learning objectives:
How and When to Use Arithmetic Operators
How and When to Use Relational Operators
How and When to Use Arithmetic Operators
Summary
If you want to add two values, use the following syntax: [number] + [number] OR [variable1] + [variable2]
If you want to subtract two values, use the following syntax: [number] - [number] OR [variable1] - [variable2]
If you want to multiply two values, use the following syntax: [number] * [number] OR [variable1] * [variable2]
If you want to divide two values, use the following syntax: [number] / [number] OR [variable1] / [variable2]
If you want to divide two numbers and only get the whole number that goes into the numerator, use the following syntax: [number] // [number] OR [variable1] // [variable2]
If you want to divide two numbers and get the remainder, use the following syntax: [number] % [number] OR [variable1] % [variable2]
If you want to exponentiatea number by a certain value, use the following syntax: [number] ** [exponent_value] OR [variable1] ** [exponent_value]
NOTE: You can override a variable's value by re-declaring it. Example: quantity = 5 quantity = quantity * 2
How and When to Use Relational Operators
Summary:
Relational Operators are use to create an expression that is used to test a relation between two operands (typically variables or conditions) to evaluate whether the test is True or False.
If you want to see if a variable is equal to another variable or a static value, use the following syntax: [variable1] == [value] OR [variable1] == [variable2]
If you want to see if a variable is not equal to another variable or a static value, use the following syntax: [variable1] != [value] OR [variable1] != [variable2]
If you want to see if a variable is greater than another variable or a static value, use the following syntax: [variable1] > [value] OR [variable1] > [variable2]
If you want to see if a variable is less than another variable or a static value, use the following syntax: [variable1] < [value] OR [variable1] < [variable2]
If you want to see if a variable is greater than OR equal to another variable or a static value, use the following syntax: [variable1] >= [value] OR [variable1] >= [variable2]
If you want to see if a variable is less than OR equal to another variable or a static value, use the following syntax: [variable1] <= [value] OR [variable1] <= [variable2]
Exercise
Congratulations! You just completed the Operators Tutorial! To help test your knowledge, let's
practice creating some Arithmetic and Relational Expressions.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
Instructions:
Open your IDE (e.g., VS Code, PyCharm, Spyder).
Create a New Python File. Name it "operators.py"
In the Newly Created Python File, complete the following tasks:
Declare a variable named "retail_price" and give it a value of 19.99.
Declare a variabled named "discounted_price" and give it a value of 14.99.
Declare a variable named "discount_rate" and give it a value of 0.25.
Create a Relational Expression that Shows Whether the "retail_price" variable is less than or equal to the "discounted_price" variable. Output the result.
Create a Relational Expression that Shows Whether the "discount_rate" variable is greater than 1. Output the result.
Give the "discounted_price" a new value by multiplying the "retail_price" variable by the "discount_rate" variable. Output the result.
Exercise Completed! Click here to view the answers.
Have any issues with the above exercise? Post your question on Discord!