លំហសិក្សាកម្ពុជា
V1.0
A variable is a name used to store data in a program. In Python, we create a variable by using =
Example: x = 42
Python is dynamically typed, so Python automatically knows the type of data stored in a variable.
Common variable types :
Int : whole numbers such as 10
Float : decimal numbers such as 3.14
Str : text such as "Hello"
Bool : True or False
List : an ordered and changeable collection such as [1, 2, 3]
Tuple : an ordered collection that can’t be changed such as (4, 5)
Dict : stores data as key value pairs such as {‘name': 'Alice', 'age': 25}
Set : a collection of unique values such as {1, 2, 3}
None : represents no value
Python is case sensitive, so myVar and myvar are different. A variable name can’t start with a number.
Python has several types of basic operations.
Arithmetic operations are used for calculations :
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
//: Floor division
% : Modulus or remainder
**: Exponentiation or power
Example: 17 // 5 gives 3, while 17 % 5 gives 2.
Assignment operations are used to give or update values :
= : Assigns a value
+= : Adds and updates
-= : Subtracts and updates
*= : Multiplies and updates
/= : Divides and updates
Example: x = 5 and x += 3 makes x equal to 8.
Comparison operations compare two values :
== : Equal
!= : Not equal
> : Greater than
< : Less than
>= : Greater than or equal
<= : Less than or equal
The result is usually True or False.
Logical operations are used with conditions :
And : both conditions must be true
Or : at least one condition must be true
Not : changes True to False or False to True
Membership operations check whether something exists in a collection :
In : checks if a value exists
Not in : checks if a value does not exist
A string is text made up of characters. String manipulation means working with and changing text.
Important string operations include:
Concatenation : joining strings together using +
String formatting : creating text using variables, often with an f-strings
len() : finds the number of characters
Indexing : gets a character from a specific position
Slicing : gets part of a string
.upper() : changes text to uppercase
.lower() : changes text to lowercase
.strip() : removes spaces at the beginning and end
.replace() : replaces one piece of text with another
In : checks whether a word or character exists in a string
.split() : separates a string into smaller parts or words
Variables store data.
Data types describe what kind of data is stored.
Operators perform calculations, comparisons and logical operations.
Strings are used for text and can be changed or analyzed using different methods.
មតិយោបល់ 0