In daily programming, escape characters and operators are the fundamental symbols we use to communicate with computers. They are like the syntax rules of the programming world; only by mastering these basics can we write correct and efficient code. Today, we will delve into the usage techniques of escape characters and operators in Python!
⚡ Escape Characters: Making Special Characters “Obey”
In Python, escape characters are special characters that usually have a backslash `` in front of them to indicate their special function..
Common Escape Characters and Their Functions
|
Escape Character |
Description |
Example |
Output Result |
|---|---|---|---|
|
|
Newline |
|
HelloWorld |
|
|
Tab |
|
Hello World |
|
|
Carriage Return |
|
World |
|
|
Backspace |
|
HellWorld |
|
|
Backslash itself |
|
C:\Users |
|
|
Single Quote |
|
It’s |
|
|
Double Quote |
|
He said:”Hello” |
# Example of Escape Charactersprint(‘Hello\nWorld’) # Newlineprint(‘Hello\tWorld’) # Tabprint(‘Hello\bWorld’) # Backspace (deletes the previous character)print(‘It\’s a nice day’) # Single Quote
Unescaping: Making Escape Characters “Ineffective”
Sometimes we need to make escape characters lose their special meaning and display as ordinary characters. Python provides two methods:
-
1. Add another backslash before the escape character
-
2. Add an
<span>r</span>prefix to the beginning of the string (raw string)
# Desired output: In Python, \n is a newline characterMethod 1: Using double backslashesprint(‘In Python, \\n is a newline character’)# Method 2: Using raw string (recommended)print(r’In Python,
is a newline character’)
File Path Handling Tips
Whenever dealing with file paths, remember to unescape! This is a common mistake for many beginners.# File Path Handling Example# Incorrect way (escape characters will take effect)# file_path = ‘E:\HK_lesson\数分22班Python阶段\03_输入输出与格式化\03_格式化.py’# Correct way 1: Using raw stringfile_path = r’E:\HK_lesson\数分22班Python阶段\03_输入输出与格式化\03_格式化.py’# Correct way 2: Using double backslashesfile_path = ‘E:\HK_lesson\数分22班Python阶段\03_输入输出与格式化\03_格式化.py’# Correct way 3: Using forward slashes (Python supports this)file_path = ‘E:/HK_lesson/数分22班Python阶段/03_输入输出与格式化/03_格式化.py’print(file_path)💡 Tip: A raw string cannot end with an odd number of backslashes, as the last backslash will still escape the following quote.
➕ Operators: The “Mathematical Symbols” of the Programming World
Operators are fundamental components of programming, used to perform various calculations and logical operations.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
|
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
|
Addition |
|
|
|
|
Subtraction |
|
|
|
|
Multiplication |
|
|
|
|
Division |
|
|
|
|
Floor Division (returns the quotient) |
|
|
|
|
Modulus (returns the remainder) |
|
|
|
|
Exponentiation |
|
|
# Example of Arithmetic Operatorsa = 37b = 7print(a // b) # Output: 5 (quotient)print(a % b) # Output: 2 (remainder)print(2**3) # Output: 8 (2 to the power of 3)
Assignment Operators
Assignment operators are used to assign values to variables or perform compound operations (calculate first, then assign, cannot print directly).
|
Operator |
Example |
Equivalent To |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Example of Assignment Operatorsa = 21a += 10 # Equivalent to a = a + 10print(a) # Output: 31
Comparison Operators
Comparison operators are used to compare the relationship between two values, returning a boolean value (<span>True</span> or <span>False</span>).
|
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
|
Equal |
|
|
|
|
Not Equal |
|
|
|
|
Greater Than |
|
|
|
|
Greater Than or Equal |
|
|
|
|
Less Than |
|
|
|
|
Less Than or Equal |
|
|
# Example of Comparison Operatorsa = 12print(a == 12) # Output: Trueprint(a != 12) # Output: False
Logical Operators
Logical operators are used to combine conditional statements.
|
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
|
And (returns true if all conditions are true) |
|
|
|
|
Or (returns true if any condition is true) |
|
|
|
|
Not (inverts the boolean value) |
|
|
# Example of Logical Operatorsa = 13b = 21print(a > 10and b < 50) # Output: True (both conditions are satisfied)print(a > 10or b < 20) # Output: True (at least one condition is satisfied)print(not (a > 10or b < 20)) # Output: False (inversion)
🧠 Comprehensive Exercise: Digit Extraction
Let’s apply what we’ve learned through a practical example:Extracting the digits of a number.# Extracting the units, tens, hundreds, and thousands of 1024num = 1024# Thousands: 1024 // 1000 = 1 (floor division by 1000)thousands = num // 1000# Hundreds: (1024 % 1000) // 100 = 24 // 100 = 0# Or: 1024 // 100 % 10 = 10 % 10 = 0hundreds = (num // 100) % 10# Tens: (1024 % 100) // 10 = 24 // 10 = 2# Or: 1024 // 10 % 10 = 102 % 10 = 2tens = (num // 10) % 10# Units: 1024 % 10 = 4units = num % 10print(f”The thousands place of the number{num} is:{thousands}, hundreds place:{hundreds}, tens place:{tens}, units place:{units}“) This example comprehensively utilizes arithmetic operators (floor division<span>//</span> and modulus<span>%</span>), demonstrating how to extract the digits of a number through mathematical operations.
💡 Summary and Recommendations
-
Escape characters are important tools for handling special characters; remember to use raw strings (
<span>r"..."</span>) to avoid issues in file paths. -
Arithmetic operators are the foundation of mathematical calculations, with floor division
<span>//</span>and modulus<span>%</span>being particularly useful for numerical problems. -
Assignment operators can make code more concise, especially compound assignment operators (like
<span>+=</span>). -
Comparison operators and logical operators are fundamental for conditional statements; mastering them is crucial for writing logically complex programs.
Remember, programming is like learning a new language; these basic symbols and rules are the foundation of syntax. Practice more, and you will become increasingly proficient!