basic doc : loops and operators
This commit is contained in:
122
Docs/Basic/03-loops.md
Normal file
122
Docs/Basic/03-loops.md
Normal file
@@ -0,0 +1,122 @@
|
||||
### **1. Looping Through a List**
|
||||
|
||||
```python
|
||||
list = [1, 2, 3, 5, 9, 'abbas']
|
||||
for item in list:
|
||||
print(f'Item In List: {item}')
|
||||
```
|
||||
|
||||
**Explanation:**
|
||||
- A `list` is a collection of items in Python, enclosed in square brackets `[]`.
|
||||
- The `for` loop iterates over each element in the list.
|
||||
- `item` is a variable that takes the value of each element in the list one by one.
|
||||
- `print(f'Item In List: {item}')` uses an f-string to display the current item.
|
||||
|
||||
**Output:**
|
||||
```
|
||||
Item In List: 1
|
||||
Item In List: 2
|
||||
Item In List: 3
|
||||
Item In List: 5
|
||||
Item In List: 9
|
||||
Item In List: abbas
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **2. Looping Through a String**
|
||||
|
||||
```python
|
||||
string = "abbas gholi"
|
||||
for char_string in string:
|
||||
print(char_string)
|
||||
```
|
||||
|
||||
**Explanation:**
|
||||
- A `string` is a sequence of characters.
|
||||
- The `for` loop iterates over each character in the string.
|
||||
- `char_string` is a variable that holds each character in turn.
|
||||
- Each character is printed on a new line.
|
||||
|
||||
**Output:**
|
||||
```
|
||||
a
|
||||
b
|
||||
b
|
||||
a
|
||||
s
|
||||
|
||||
g
|
||||
h
|
||||
o
|
||||
l
|
||||
i
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **3. Looping Through a Dictionary**
|
||||
|
||||
```python
|
||||
price = {
|
||||
"oil": 500000,
|
||||
"egg": 350000,
|
||||
"frute": {
|
||||
"apple": 100000,
|
||||
"orange": 120000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This is a dictionary with:
|
||||
- Keys: `"oil"`, `"egg"`, `"frute"`
|
||||
- Values: numbers and another dictionary
|
||||
|
||||
#### **Method 1: Looping Over Keys**
|
||||
|
||||
```python
|
||||
for name in price:
|
||||
print(name, price[name])
|
||||
```
|
||||
|
||||
**Explanation:**
|
||||
- `for name in price` iterates over the keys of the dictionary.
|
||||
- `price[name]` retrieves the value corresponding to the key.
|
||||
- Prints each key and its value.
|
||||
|
||||
**Output:**
|
||||
```
|
||||
oil 500000
|
||||
egg 350000
|
||||
frute {'apple': 100000, 'orange': 120000}
|
||||
```
|
||||
|
||||
#### **Method 2: Using `.items()`**
|
||||
|
||||
```python
|
||||
for name, pr in price.items():
|
||||
print(name, pr)
|
||||
```
|
||||
|
||||
**Explanation:**
|
||||
- `.items()` returns key-value pairs as tuples.
|
||||
- `name, pr` unpacks each tuple into two variables.
|
||||
- More efficient and readable than accessing `price[name]`.
|
||||
|
||||
**Output:**
|
||||
```
|
||||
oil 500000
|
||||
egg 350000
|
||||
frute {'apple': 100000, 'orange': 120000}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Summary**
|
||||
|
||||
| Concept | Description |
|
||||
|----------------|-------------|
|
||||
| **List** | Ordered collection of items. Use `for item in list` to iterate. |
|
||||
| **String** | Sequence of characters. Use `for char in string` to access each character. |
|
||||
| **Dictionary** | Key-value pairs. Use `for key in dict` or `for key, value in dict.items()` to loop. |
|
||||
|
||||
73
Docs/Basic/04-operators.md
Normal file
73
Docs/Basic/04-operators.md
Normal file
@@ -0,0 +1,73 @@
|
||||
### **1. `for` Loop with `range()`**
|
||||
|
||||
```python
|
||||
for i in range(2, 20, 1):
|
||||
print(f"i : {i}", end=" End \n")
|
||||
```
|
||||
|
||||
- **`range(start, stop, step)`**: Generates a sequence of numbers starting from `start` (inclusive), up to `stop` (exclusive), increasing by `step`.
|
||||
- `range(2, 20, 1)` → numbers from 2 to 19 (inclusive), step 1.
|
||||
- **`for` loop**: Iterates over each number in the range.
|
||||
- **`print(f"i : {i}", end=" End \n")`**:
|
||||
- `f-string`: Formats the output with the current value of `i`.
|
||||
- `end=" End \n"`: Replaces the default newline (`\n`) with `" End "` followed by a newline.
|
||||
- **Output**: Prints each number with `" End "` at the end of each line.
|
||||
|
||||
---
|
||||
|
||||
### **2. Looping Through a List Using Index and `enumerate()`**
|
||||
|
||||
```python
|
||||
list = ['abbas', 'mmd', 2006]
|
||||
for key in range(len(list)):
|
||||
value = list[key]
|
||||
print(key, value)
|
||||
```
|
||||
|
||||
- **`len(list)`**: Returns the number of elements in the list (3).
|
||||
- **`range(len(list))`**: Creates numbers `0, 1, 2` (indices of the list).
|
||||
- **`list[key]`**: Accesses the element at index `key`.
|
||||
- **Output**: Prints index and value pair for each element.
|
||||
|
||||
---
|
||||
|
||||
```python
|
||||
for key, value in enumerate(list):
|
||||
print(key, value)
|
||||
```
|
||||
|
||||
- **`enumerate(list)`**: Returns pairs of `(index, value)` for each element.
|
||||
- **`key, value`**: Unpacks each pair into two variables.
|
||||
- **Output**: Same as above, but more concise and Pythonic.
|
||||
|
||||
> ✅ **Best Practice**: Use `enumerate()` instead of `range(len())` for cleaner code.
|
||||
|
||||
---
|
||||
|
||||
### **3. `zip()` Function – Pairing Two Lists**
|
||||
|
||||
```python
|
||||
name = ['egg', 'oil']
|
||||
price = [370000, 500000]
|
||||
for final in zip(name, price):
|
||||
print(final)
|
||||
```
|
||||
|
||||
- **`zip(list1, list2)`**: Combines two lists element-wise into tuples.
|
||||
- `zip(['egg', 'oil'], [370000, 500000])` → `[('egg', 370000), ('oil', 500000)]`
|
||||
- **`for final in zip(...)`**: Iterates over each tuple.
|
||||
- **Output**: Prints each pair as a tuple.
|
||||
|
||||
> ✅ Use `zip()` when you need to process multiple lists in parallel.
|
||||
|
||||
---
|
||||
|
||||
### Summary of Key Concepts:
|
||||
|
||||
| Concept | Purpose |
|
||||
|----------------|--------|
|
||||
| `range(start, stop, step)` | Generate a sequence of numbers |
|
||||
| `for` loop | Iterate over a sequence |
|
||||
| `len(list)` | Get number of elements |
|
||||
| `enumerate()` | Get index and value in a loop |
|
||||
| `zip()` | Combine two or more lists element-wise |
|
||||
Reference in New Issue
Block a user