លំហសិក្សាកម្ពុជា លំហសិក្សាកម្ពុជា V1.0
ចូល ចុះឈ្មោះ

មេរៀនសង្ខេប : Variables, Basic Operations & String Manipulation

មូលសង្ខេប / Abstract

This lesson introduces the basic concepts of Python programming, including variables, data types, basic operations, and string manipulation. It explains how to store and work with different types of data and introduces common Python operators and string methods.

ពាក្យគន្លឹះ

1. Variables

A variable is a named storage location that holds data during program execution. A variable has a name, value, type, memory location, scope, and mutability. In Python, a variable is created by assigning a value with =. Example: x = 42. Python is dynamically typed, so its type is inferred from the assigned value.

Common types: int = whole numbers (x = 10); float = decimal numbers (y = 3.14); str = text in quotes (name = "Sok"); bool = True/False; list = ordered and mutable ([1, 2, 3]); tuple = ordered and immutable ((4, 5)); dict = key-value pairs ({'name':'Alice'}); set = unique unordered values ({1, 2, 3}); None = absence of a value.

Important: Python variable names are case-sensitive (myVar ≠ myvar), cannot start with a number, and should follow PEP 8 naming conventions.

 

2. Basic Operations

Arithmetic: + addition, − subtraction, * multiplication, / division, // floor division, % remainder (modulus), ** exponentiation. Examples: 10 + 5 = 15; 17 // 5 = 3; 17 % 5 = 2; 2 ** 3 = 8.

Assignment: = assigns a value. In-place operators update a variable: +=, -=, *=, /=. Example: y = 5; y += 3 → y = 8.

Comparison: == equal, != not equal, > greater than, < less than, >= greater/equal, <= less/equal. These comparisons return True or False.

Logical: and → True when both conditions are true; or → True when at least one is true; not → reverses the truth value.

Membership: in → checks whether a value exists in a sequence; not in → checks whether it does not exist. Example: 2 in [1,2,3] → True.

 

3. String Manipulation

Strings are sequences of characters. Common operations include:

• Concatenation: joins strings using +. Example: "Hello" + " " + "World" → "Hello World".

• Formatting: f-strings insert variables into text. Example: f"My name is {name} and I am {age} years old."

• Length: len(text) returns the number of characters.

• Indexing: text[0] gets the first character.

• Slicing: text[0:6] extracts characters from index 0 up to (not including) 6.

• Case conversion: text.upper() → uppercase; text.lower() → lowercase.

• Whitespace: text.strip() removes leading and trailing spaces.

• Replace: text.replace("Java", "Python") replaces a substring.

• Checking: "fun" in text checks whether a substring exists.

• Splitting: sentence.split() breaks a string into a list of words.

 

4. Key Exercise Skills

The exercise practices creating variables, using len(), converting text with upper(), replacing words with replace(), counting characters with count(), reversing a string with slicing [::-1], and printing results.

Block chain assingment1.pdf
PDF ឯកសារ
ទាញយក អាន
ចែករំលែក
រក្សាទុក
ការផ្សព្វផ្សាយ
ការផ្សព្វផ្សាយ
មតិយោបល់ 0
ចូលគណនី ដើម្បីបញ្ចេញមតិ