លំហសិក្សាកម្ពុជា
V1.0
This lesson introduces the fundamental concepts of Python programming, including arithmetic, comparison, logical, assignment, and membership operators. It also explains common string manipulation techniques, such as finding string length, accessing characters, slicing, changing letter cases, removing whitespace, replacing substrings, checking for specific text, and splitting strings. Understanding these concepts helps learners perform calculations, compare values, make logical decisions, and process text efficiently in Python.
| Operation | Symbol | Example |
|---|---|---|
| Add | + |
2 + 3 = 5 |
| Subtract | - |
5 - 2 = 3 |
| Multiply | * |
3 * 4 = 12 |
| Divide | / |
10 / 3 = 3.33 |
| Floor Div | // |
10 // 3 = 3 |
| Modulus | % |
10 % 3 = 1 |
| Power | ** |
2 ** 3 = 8 |
1. Arithmetic Operators
a = 10
b = 3
print(a + b) # 13 (Addition)
print(a - b) # 7 (Subtraction)
print(a * b) # 30 (Multiplication)
print(a / b) # 3.333... (Division - always returns float)
print(a // b) # 3 (Floor Division)
print(a % b) # 1 (Modulus - remainder)
print(a ** b) # 1000 (Exponentiation)
2. Comparison Operators
x = 5
y = 10
print(x == y) # False (Equal)
print(x != y) # True (Not equal)
print(x > y) # False (Greater than)
print(x < y) # True (Less than)
print(x >= y) # False (Greater or equal)
print(x <= y) # True (Less or equal)
3. Logical Operators
p = True
q = False
print(p and q) # False
print(p or q) # True
print(not p) # False
4. Assignment Operators
n = 10
n += 5 # n = n + 5 → 15
n -= 3 # n = n - 3 → 12
n *= 2 # n = n * 2 → 24
n /= 4 # n = n / 4 → 6.0
n //= 2 # n = n // 2 → 3.0
n %= 2 # n = n % 2 → 1.0
n **= 3 # n = n ** 3 → 1.0
Operators that check whether a value exists inside a sequence (string, list, tuple, set, dict, range).
"py" in "python" # True
"Py" in "python" # False
5 in [1, 2, 3, 4, 5] # True
"x" not in ["a", "b"] # True
"age" in {"name":"Al", "age":25} # True (key)
25 in {"name":"Al", "age":25} # False
25 in {"name":"Al", "age":25}.values() # True
999_999 in set(range(1_000_000)) # True (instant)
| Data Type | What in Checks |
Example | Result |
|---|---|---|---|
| String | Substring | "ell" in "hello" |
True |
| List | Element | 3 in [1, 2, 3] |
True |
| Tuple | Element | 2 in (1, 2, 3) |
True |
| Set | Element | "a" in {"a","b"} |
True |
| Dictionary | keys only | "name" in {"name":"Al"} |
True |
| Range | Number in range | 5 in range(10) |
True |
String manipulation
Here are some common string manipulation operations in Python:
3. Length of a String:
Example: text = "Python"
length = len(text)
print(length) # length is 6
4. Accessing Individual Characters:
Example: text = "Python"
first_char = text[0]
print(first_char) # first_char is 'P’
5. Slicing:
Example: text = "Python Programming"
sliced_text = text[0:6]
print(sliced_text) # Output: "Python"
16String manipulation
Here are some common string manipulation operations in Python:
6. Conversion:
Example: text = "Hello"
uppercase_text = text.upper()
print(uppercase_text) # uppercase_text is "HELLO"
lowercase_text = text.lower()
print(lowercase_text) # lowercase_text is "hello"
7. Stripping Whitespace:
Example: text = " Python "
stripped_text = text.strip()
print(stripped_text) # stripped_text is "Python"
8. Replacing Substrings:
Example: • Replacing occurrences of a substring with another.
text = "I like Java"
updated_text = text.replace("Java", "Python")
print(updated_text) # updated_text is "I like Python"
17String manipulation
Here are some common string manipulation operations in Python:
9. Checking for Substrings:
Example: text = "Python is fun"
is_fun_present = "fun" in text
print(is_fun_present) # is_fun_present is True
10. Splitting:
Example: • Breaking a string into a list of substrings based on a delimiter.
sentence = "This is a sentence."
words = sentence.split()
print(words) # words is ['This', 'is', 'a', 'sentence.’]
Python offers powerful string manipulation methods, enabling versatile text handling for
various applications. Understanding these methods is key to achieving desired results
efficiently.
មតិយោបល់ 0