Module 3 - Operators and Expressions Header

Module 3 - Operators and Expressions

Data Type Conversion Functions

In Python, data type conversion functionsEssential tools in Python for transforming variables from one type to another. are essential tools for transforming variables from one type to another. The following are commonly used conversion functions along with examples and use cases for each:

int ( )

This function is used to convert a value to an integer data type. It's particularly useful when dealing with user input or when converting floating-point numbers to integers.

Example:

float_num = 3.14
int_num = int(float_num)
print(int_num) # Output: 3

float ( )

This function converts a value to a floating-point number. It's handy when you need to ensure decimal precision or when converting integers to floats.

Example:

int_value = 5
float_value = float(int_value)
print(float_value) # Output: 5.0

str ( )

The str() function is used to convert values to string data types. This is often necessary when combining non-string data with strings in output or when writing data to a file.

Example:

number = 42
str_number = str(number)
print("The answer is: " + str_number) # Output: The answer is: 42


 



Introducing Operators

 In Python, operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to perform operations on variables and values. There are many different types of operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership., but they can be grouped into the following categories:

  • Arithmetic operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to perform mathematical operations on numbers. For example, the additionArithmetic operator that adds two numbers together. operator (+) adds two numbers together, while the subtractionArithmetic operator that subtracts one number from another. operator (-) subtracts one number from another.
  • Assignment operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to assign values to variables. For example, the assignment operator (=) assigns the value 10 to the variable x.
  • Comparison operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to compare two values and return a Boolean value (True or False). For example, the comparison operator == checks if two values are equal.
  • Logical operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to combine Boolean values and return a single Boolean value. For example, the logical operator and (&&) returns True if both Boolean values are True.
  • Identity operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to compare the identity of two objects. For example, the identity operator is (is) returns True if two objects are the same object.
  • Membership operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.: These operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to check if a value is a member of a collection. For example, the membership operator in (in) returns True if the value is a member of the collection.


Arithmetic Operators

In Python, arithmetic operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. are used to perform mathematical operations on numbers. There are six arithmetic operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. in Python:

  • AdditionArithmetic operator that adds two numbers together. (+): Adds two numbers together.
# AdditionArithmetic operator that adds two numbers together.
x = 1 + 2
print(x)
# Output: 3
  • SubtractionArithmetic operator that subtracts one number from another. (-): Subtracts one number from another.
# SubtractionArithmetic operator that subtracts one number from another.
y = 5 - 3
print(y)
# Output: 2
  • MultiplicationArithmetic operator that multiplies two numbers together. (*): Multiplies two numbers together.
# MultiplicationArithmetic operator that multiplies two numbers together.
z = 2 * 3
print(z)
# Output: 6
  • DivisionArithmetic operator that divides one number by another. (/): Divides one number by another.
# DivisionArithmetic operator that divides one number by another.
a = 10 / 2
print(a)
# Output: 5.0
  • ModulusArithmetic operator that gives the remainder of dividing one number by another. (%): Gives the remainder of dividing one number by another.
# ModulusArithmetic operator that gives the remainder of dividing one number by another.
b = 10 % 3
print(b)
# Output: 1
  • Floor DivisionArithmetic operator that divides one number by another. (//): Divides one number by another, dropping the remainder.
# Floor DivisionArithmetic operator that divides one number by another.
a = 11 // 2
print(a)
# Output: 5.0
  • ExponentiationArithmetic operator that performs exponential calculation, raising one number to the power of another. (**): Performs exponential calculation, i.e. raising one number to the power of another.
# ExponentiationArithmetic operator that performs exponential calculation, raising one number to the power of another.
c = 2 ** 3
print(c)
# Output: 8

Arithmetic operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. follow a specific order of precedenceThe specific order in which arithmetic operators are evaluated in an expression., which determines which operations are performed first. The order of precedenceThe specific order in which arithmetic operators are evaluated in an expression. is as follows:

  1. ExponentiationArithmetic operator that performs exponential calculation, raising one number to the power of another.
  2. MultiplicationArithmetic operator that multiplies two numbers together. and divisionArithmetic operator that divides one number by another.
  3. AdditionArithmetic operator that adds two numbers together. and subtractionArithmetic operator that subtracts one number from another.

If there are multiple operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. of the same precedence in an expression, they are evaluated from left to right.

Here is an example of how the order of precedenceThe specific order in which arithmetic operators are evaluated in an expression. affects the evaluation of an expression:

x = 2 * 3 + 4

In this expression, the multiplicationArithmetic operator that multiplies two numbers together. operator has a higher precedence than the additionArithmetic operator that adds two numbers together. operator. Therefore, the multiplicationArithmetic operator that multiplies two numbers together. is performed first, followed by the additionArithmetic operator that adds two numbers together.. The expression is evaluated as follows:

(2 * 3) + 4  
= 6 + 4  
= 10  
 



Python Expressions

In Python, an expression is a combination of values, variables, operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership., and functions that evaluates to a single value. Expressions are used to perform calculations, manipulate data, and control the flow of execution.

There are many different types of expressions in Python, including:

  • Arithmetic expressions: These expressions perform mathematical operations on numbers. For example, the expression 2 + 2 evaluates to the value 4.
  • Boolean expressionsExpressions that evaluate to either True or False.: These expressions evaluate to either True or False. For example, the expression 1 < 2 evaluates to True, and the expression 1 > 2 evaluates to False.
  • Comparison expressionsExpressions that compare two values and evaluate to either True or False.: These expressions compare two values and evaluate to either True or False. For example, the expression 1 == 2 evaluates to False, and the expression 1 != 2 evaluates to True.
  • Logical expressionsExpressions that combine Boolean expressions using logical operators such as AND, OR, and NOT.: These expressions combine Boolean expressionsExpressions that evaluate to either True or False. using logical operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. such as AND, OR, and NOT. For example, the expression (1 < 2) and (2 < 3) evaluates to True.
  • String expressionsExpressions that combine strings using string operators such as concatenation and repetition.: These expressions combine strings using string operatorsUsed to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership. such as concatenation and repetition. For example, the expression "Hello " + "World" evaluates to the string "Hello World".

Expressions can be used in a variety of ways in Python. For example, they can be used to:

  1. Calculate the value of a variable.
  2. Compare two values.
  3. Control the flow of execution.
  4. Create strings.
  5. Call functions.


Try it: Expressions

Take some time in the window below to use print() and different expressions.  

Videos for Module 3 - Operators and Expressions

Intro to Type Conversion ()

Type Conversion Demo ()

Basic Python Math Operators ()

More Python Operators ()

Order of Operations ()

A3 Explanation ()

A3 Code Demonstration ()

Key Terms for Module 3 - Operators and Expressions


Addition Arithmetic operator that adds two numbers together.
Arithmetic Operators Perform mathematical operations on numbers. Include Addition, Subtraction, Multiplication, Division, Modulus, Floor Division, Exponentiation.
Boolean Expressions Expressions that evaluate to either True or False.
Comparison Expressions Expressions that compare two values and evaluate to either True or False.
Data Type Conversion Functions Essential tools in Python for transforming variables from one type to another.
Division Arithmetic operator that divides one number by another.
Exponentiation Arithmetic operator that performs exponential calculation, raising one number to the power of another.
float() Converts a value to a floating-point number. Useful for decimal precision or converting integers to floats.
Floor Division Arithmetic operator that divides one number by another, dropping the remainder.
int() Converts a value to an integer data type. Useful for user input or converting floating-point numbers to integers.
Logical Expressions Expressions that combine Boolean expressions using logical operators such as AND, OR, and NOT.
Modulus Arithmetic operator that gives the remainder of dividing one number by another.
Multiplication Arithmetic operator that multiplies two numbers together.
Operators Used to perform operations on variables and values in Python. Categories: Arithmetic, Assignment, Comparison, Logical, Identity, Membership.
Order of Precedence The specific order in which arithmetic operators are evaluated in an expression.
Python Expressions Combinations of values, variables, operators, and functions that evaluate to a single value. Types include Arithmetic, Boolean, Comparison, Logical, String.
str() Converts values to string data types. Necessary for combining non-string data with strings in output or writing to a file.
String Expressions Expressions that combine strings using string operators such as concatenation and repetition.
Subtraction Arithmetic operator that subtracts one number from another.

Quiz Yourself - Module 3 - Operators and Expressions

Test your knowledge of this module by choosing options below. You can keep trying until you get the right answer.

Skip to the Next Question