This document is a brief summary of a Python lesson on variables, basic operations, and string manipulation. It covers how variables store data and their types (int, float, str, bool, list, tuple, dict, set, None), the five basic operation categories (arithmetic, assignment, comparison, logical, membership), and common string methods like concatenation, formatting, slicing, and splitting.
What is a Variable?
A variable is a container that stores a piece of information so it can be used later in a program. Every variable has a name, a value, and a type. In Python you create one by writing a name, an equals sign, and a value, like x = 42. Python is case-sensitive, so myVar and myvar are different variables, and names cannot start with a number.
Data Types
Python works out the type of a variable automatically from its value. The common types are:
- int: whole numbers (x = 10)
- float: decimal numbers (y = 3.14)
- str: text (name = "Sok")
- bool: True or False values
- list: an ordered group of items that can be changed
- tuple: an ordered group of items that cannot be changed
- dict: key-value pairs
- set: a group of unique values only
- None: an empty or missing value
Basic Operations
Basic operations are the actions you can perform with variables. There are five types:
- Arithmetic: math operations (+, -, *, /, //, %, **)
- Assignment: storing or updating values (=, +=, -=, *=, /=)
- Comparison: checking equality or size (==, !=, >, <, >=, <=)
- Logical: combining conditions (and, or, not)
- Membership: checking if a value exists (in, not in)
Floor division (//) rounds down, and modulus (%) gives the remainder after division.
String Manipulation
String manipulation is about working with text, one of the most common data types. The key techniques are:
- Concatenation: joins strings together with the + sign
- f-strings: insert variables directly into text, for example f"My name is {name}"
- len(): returns the number of characters in a string
- Indexing: accesses one character by its position, starting at 0
- Slicing: takes a portion of a string, for example text[0:6]
- .upper() and .lower(): convert between uppercase and lowercase
- .strip(): removes extra spaces from the start and end
- .replace(): swaps one piece of text for another
- in: checks whether a word exists inside a string
- .split(): breaks a sentence into a list of words
Conclusion
Overall, this lesson builds a solid foundation for working with data in Python. Understanding how variables store information and how these operations manipulate it is essential before moving on to loops, functions, and conditional statements.
មតិយោបល់ 0