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

Summary of Python Programming lesson

មូលសង្ខេប / Abstract

This article summarises the main lessons of Python Programming, including variables, basic operations, and string manipulation.

ពាក្យគន្លឹះ

Python Programming: 

Variables, Basic Operations and String manipulation

Variables

Variable in Programming is a named storage location that holds a value or data. For example, if we put x=2, so it means variable x stores the value of 2. Therefore, we do not it to write the value again; we can just use the variable instead. 

 

These variables can be categorized into two main group such as basic variable types and data structures. The differences between those two is that a basic variable holds a single piece of data, while a data structure is a specialized format for organizing and storing multiple values or objects together so they can be managed efficiently.

 

Basic variable types include Integers(int), Floats(float), Strings(str), Booleans(bool), and NoneType (None). Integers (int) are whole numbers like x=2 while floats are numbers with decimal points or in exponential form, such as x=2.2. Strings are characters or text that are enclosed in single or double quotes, such as name = "Sok" or name = 'Sok'. The Boolean (bool) is used in logical operations and conditional statements, and they represent either True or False. As for NoneType (None) is used when there is no value.

 

Data structures have Lists (list), Tuples (tuple), Dictionaries (dict), and Sets (set).

Lists (list) are used for to store an ordered, mutable (changeable) collection of items under a single variable name, such as numbers = [1, 2, 3]. Tuples (tuple) are similar to list, but they cannot be changed after the creation. Dictionaries (dict) are used to store data as labeled key-value pairs while Sets (set) to store an unordered collection of unique items. Unlike lists or tuples, sets do not allow duplicate values. Because they are powered by an underlying hash table, they are optimized for extremely fast data lookups.

Basic Operations

 

Basic Operations are fundamental actions performed on variables and values. There are five main categories used to manipulate data. 

 

Firstly, we have Arithmetic Operations. Arithmetic operations in Python are symbols used to perform standard mathematical calculations on numbers and variables like addition (+), subtraction (-), multiplication (*), and division (/). Normal division always returns a float, such as 10/2=2.0 or 10/4=2.5 while Floor Division (//) drop the decimal like 10/4=2. Modulus (%) to find the remainder of division like 10%4=2, and Exponentiation (**) to calculate powers, such as 2**3=8 (2 to the power of 3).

 

Secondly, we get Assignment Operation that is used to give a value to a variable. 

Assignment (=) is used to assign a value to a variable while In-Place Operations (+=, -=, *=, /=) used to perform an operation and update the variable in-place.

For example, y = 5, y += 3 equivalent to y = y + 3, so y is now 8.

 

Thirdly, there is Comparison Operations. They are used to compare two values and return a Boolean result (either true or false). Comparison operators include: Equality (==) and Inequality (!=) are used to check if values are equal or not. For example:if we write result = (3 == 3) so the result is True or result = (4 != 7) so the result is True. Greater Than (>), Less Than (<), Greater Than or Equal To (>=), Less Than or Equal To (<=): Compare numerical values like result = (10 > 5) so the result is True.

 

Moreover, they contain Logical Operations which are used to combine conditional statements: and, or, and not. These operators evaluate expressions and return a Boolean value (True or False) or the value of one of the operands. For example:

_result = (True and False) so the result is False

_result = (True or False) so the result is True

_result = not True so the result is False

 

Lastly, we have Membership Operations. It is utilizing in and not in to verify if a specific value exists within a sequence, such as a list or a string. Both operators always return a Boolean value: either True or False. 

String manipulation


In Python, a string is a sequence of characters used to store and manipulate text. It is represented by the built-in str class and can be written using single, double, or triple quotes. There are some string manipulation operations in Python. Concatenation is used to join strings such as joining "Hello" and "World" into "HelloWorld" using operators like +. 


String Formatting is using placeholders or f-strings which allow direct variable and expression interpolation inside curly braces. For example:

name = "Sopheap"

age = 30

result = f"Myname is {name} and I am {age} years old."

print(result) # My name is Sopheap and I am 30 years old.

 

Length of a String is used len() to determine the number of characters in a string.

Example:text = "Python"

length = len(text)

print(length) # length is 6

 

Accessing Individual Characters is retrieving specific characters by their index position. Using [-1] to extract the very last character is a highly efficient technique that avoids the need to calculate the total length. For instance:

text = "Python"

first_char= text [0]  

print(first_char) # first_char is 'P’

last_char= text [-1]  

print(last_char) # last_char is 'n’

 

Slicing is extracting specific parts of a string using the [start: stop: step] syntax. The step value defines the increment and can never be zero like:

text = "Python Programming" 

sliced_text= text [0:6] 

print(sliced_text) # Output: "Python"

 

Conversion is changing text casing using .upper() or .lower(), which simplifies checking and comparing user inputs. For example:

text = "Hello"

uppercase_text= text.upper()

print(uppercase_text) # uppercase_textis "HELLO"

lowercase_text= text.lower()

print(lowercase_text)  # lowercase_textis "hello"

 

Stripping Whitespace: Using .strip() to clean up invisible spaces at the beginning and end of a string, as those spaces typically hold no data value. Example: 

text = "   Python   "

stripped_text= text.strip()

print(stripped_text)  # stripped_textis "Python"

 

Replacing is using .replace() seamlessly swaps out an old substring for a new one within a text block. For instance:

text = "I like Java"

updated_text= text.replace("Java", "Python")

print(updated_text)  # updated_textis "I like Python"

 

Checking Substrings is verifying if a specific word or phrase exists in a text using the in operator like:

text = "Python is fun"

is_fun_present= "fun" in text

print(is_fun_present)  # is_fun_presentis True

 

Splitting is breaking a sentence into individual words based on spaces using .split(). This effectively converts a standard string into a list, split() turns a string into a list of words that can be easily modified. For example:

sentence = "This is a sentence."

words = sentence.split()

print(words)  # words is ['This', 'is', 'a', 'sentence.’]


In conclusion, this lesson introduces the basic concepts needed to work with Python. Variables and data types are used to store information, operations are used to process and compare values, and string manipulation provides different ways to work with text. These concepts are the foundation for writing and understanding Python programs.


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