Lesson 2.2: Operators, Expressions and String Handling
Introduction
Welcome to Lesson 2.2 of Foundation Computing! In this lesson, we're diving into Operators, Expressions, and String Handling in Python. By the end of this lesson, you'll have a solid understanding of the different types of operators, how to build and evaluate expressions, and how to handle strings effectively.
Learning Objectives
By the end of this lesson, you should be able to:
- Understand arithmetic operators, including integer division and modulo, and know the rule of operator precedence.
- Build and evaluate complex expressions while mixing different numeric types.
- Perform various string operations like concatenation, indexing, slicing, getting lengths, and using common string methods.
- Format output using f-strings and validate input with type conversion.
- Assess and evaluate both arithmetic and string expressions while applying correct operator precedence.
H2: Arithmetic Operators and Operator Precedence
In Python, arithmetic operators allow you to perform mathematical operations. Some common arithmetic operators include:
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Integer Division:
// - Modulo:
%
Let’s see these operators in action with some examples:
Example 1: Basic Arithmetic Operations
result_add = 7 + 3 # 10
result_sub = 7 - 3 # 4
result_mul = 7 * 3 # 21
result_div = 7 / 3 # 2.333...
result_int_div = 7 // 3 # 2
result_mod = 7 % 3 # 1
In Python, when you divide two integers, it typically returns a float. For example, $7 / 3$ results in approximately $2.33$. If you want only the integer part, you can use integer division //, which in this case gives you $2$.
The modulo operator % returns the remainder of the division. For instance, $7 \mod 3$ results in $1$ because $3$ fits into $7$ two times, which leaves a remainder of $1$.
Operator Precedence
Operators are evaluated in a specific order known as operator precedence. The order is:
- Parentheses
() - Exponents
** - Multiplication
*, Division/, Integer Division//, and Modulo% - Addition
+and Subtraction-
This means that operations in parentheses are performed first, followed by exponents, and so on. Here’s an example to illustrate:
Example 2: Evaluating Expressions
result = 10 + 6 * 2 # 22
result_with_parentheses = (10 + 6) * 2 # 32
In the first case, multiplication takes precedence over addition, resulting in $10 + (6 2) = 22$. In the second case, we’ve used parentheses to change the order, resulting in $(10 + 6) 2 = 32$.
H2: Building and Evaluating Expressions
You can create complex expressions by mixing operators and numeric types. Python does automatic type conversion, which can sometimes yield surprising results.
Example 3: Mixing Numeric Types
result = 5 + 7.0 # 12.0
result2 = 5 * 3 + 2 # 17
In the first example, notice how adding an integer ($5$) and a float ($7.0$) gives you a float result ($12.0$). This is due to automatic type conversion. In the second example, we first perform the multiplication, then add $2$, giving us $17$.
H2: String Operations
Strings are sequences of characters. Python provides several ways to manipulate strings:
Concatenation
You can concatenate strings using the + operator:
greeting = "Hello" + " " + "World!" # "Hello World!"
Indexing and Slicing
Strings are indexed. The first character is at index 0:
sample_text = "Python"
first_char = sample_text[0] # 'P'
sliced_text = sample_text[1:4] # 'yth'
Length and Common String Methods
You can find the length of a string using len():
length_of_text = len(sample_text) # 6
Strings also come with methods such as .upper(), .lower(), and .replace():
uppercase_text = sample_text.upper() # 'PYTHON'
H2: Formatting Output with f-Strings
Python 3.6 introduced f-strings, an easy way to format strings dynamically. Here’s how to use them:
name = "students"
formatted_string = f"Hello, {name}!" # 'Hello, students!'
This makes it easy to insert variables directly into your strings.
H2: Basic Input Validation
When you take input from users, it’s essential to validate it. One way to do this is by converting input types:
age_input = input("Enter your age: ")
age = int(age_input) # Converts the input string to an integer
This converts the string input into an integer, ensuring you can perform mathematical operations with it.
H2: Conclusion
In this lesson, you've learned how to use various operators and evaluate expressions in Python. You've also explored string handling, including operations like concatenation, indexing, slicing, and formatting output using f-strings.
Understanding these fundamental concepts is crucial as you progress in programming. Practice these techniques, and you'll become more comfortable with coding in Python!
Study Notes
- Arithmetic operators:
+,-,*,/,//,% - Operator precedence rules
- Building expressions with mixed numeric types
- String operations: concatenation, indexing, slicing, and string methods
- Formatting output with f-strings
- Validating input with type conversion
