update data type doc
This commit is contained in:
@@ -1,37 +1,53 @@
|
|||||||
Data Types In Python :
|
# Python Data Types and Basic Operations
|
||||||
|
|
||||||
|
## Data Types Overview
|
||||||
|
|
||||||
int : normal numbers like (1 , -100 , 20326 ,0 , ..)
|
| Type | Description | Examples |
|
||||||
float :
|
| :--- | :--- | :--- |
|
||||||
string: txts in ""
|
| **int** | Normal whole numbers. | `1`, `-100`, `20326`, `0` |
|
||||||
list [1,2,3,4,5] or ['abbas', 2 ,-10 , 2.65]
|
| **float** | Numbers with decimal points. | (No example provided) |
|
||||||
dictionary: {'name': abbas , 'l_name': 'gholi'}
|
| **string** | Text enclosed in quotes. | `"txt"` |
|
||||||
set: like set but dont have duplications
|
| **list** | Ordered, changeable collection of items. | `[1, 2, 3, 4, 5]`, `['abbas', 2, -10, 2.65]` |
|
||||||
|
| **dictionary** | Unordered, changeable collection of key-value pairs. | `{'name': 'abbas', 'l_name': 'gholi'}` |
|
||||||
|
| **set** | Unordered collection that does not allow duplicate members. | Like `set`, but no duplications. |
|
||||||
|
|
||||||
|
**Checking Type:**
|
||||||
```python
|
```python
|
||||||
print(type(var)) # Return Varible Type
|
print(type(var)) # Return Variable Type
|
||||||
```
|
```
|
||||||
strings:
|
|
||||||
|
|
||||||
strings is imutable (can,t change after set string with str[0]='i')
|
---
|
||||||
|
|
||||||
|
## Strings
|
||||||
|
|
||||||
|
Strings are **immutable** (cannot be changed after creation, e.g., `str[0] = 'i'` fails).
|
||||||
|
|
||||||
|
### String Indexing and Slicing
|
||||||
|
|
||||||
|
Indexing starts at 0. Negative indexing starts from the end (-1 is the last character).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
string="this is string about abbas gholi #1"
|
string="this is string about abbas gholi #1"
|
||||||
print(string[0]) # --> Return t
|
print(string[0]) # --> Return t
|
||||||
print(string[1:3]) # --> Return his
|
print(string[1:3]) # --> Return hi (End index is exclusive)
|
||||||
print(string[-1]) # --> Return 1
|
print(string[-1]) # --> Return 1
|
||||||
# [start : end]
|
# Format: [start : end]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Note on slicing examples provided (These might be incorrect based on standard Python slicing):**
|
||||||
```python
|
```python
|
||||||
string='012345678'
|
string='012345678'
|
||||||
print(string[5]) # --> return '5'
|
print(string[5]) # --> return '5'
|
||||||
print(string[1,5]) # --> return '1234'
|
# print(string[1,5]) # Incorrect syntax for standard slicing
|
||||||
print(string[1,4,2]) # --> return 13
|
# print(string[1,4,2]) # Incorrect syntax for standard slicing
|
||||||
print(string[::-1]) # --> return 876543210
|
print(string[::-1]) # --> return 876543210 (Reverses the string)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Multi-line Strings
|
||||||
|
|
||||||
|
Use triple quotes (`"""` or `'''`) for strings spanning multiple lines, including newlines and quotes.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# String With Enter Every Char
|
|
||||||
"""
|
"""
|
||||||
Hi This Is String Without Any Limits Like
|
Hi This Is String Without Any Limits Like
|
||||||
enter
|
enter
|
||||||
@@ -40,88 +56,106 @@ and any more
|
|||||||
"""
|
"""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### String Methods
|
||||||
string methods:
|
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
string="AbbAsGholi
|
string="AbbAsGholi"
|
||||||
print(string.upper()) # Return ABBASGHOLI
|
print(string.upper()) # Return ABBASGHOLI
|
||||||
print(string.lower()) # Return abbasgholi
|
print(string.lower()) # Return abbasgholi
|
||||||
print(string.islower()) # Return False
|
print(string.islower()) # Return False
|
||||||
print(string.index('i')) # Return 8
|
print(string.index('i')) # Return 8 (Index of first occurrence)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Splitting Strings:**
|
||||||
```python
|
```python
|
||||||
string = "abbas,mmd,asghar"
|
string = "abbas,mmd,asghar"
|
||||||
list_strings = string.split(",")
|
list_strings = string.split(",")
|
||||||
print(list_strings) # out put is : ['abbas','mmd','asghar']
|
print(list_strings) # out put is : ['abbas','mmd','asghar']
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### String Formatting
|
||||||
|
|
||||||
|
Multiple ways to embed variables into strings:
|
||||||
|
|
||||||
|
| Method | Example |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `%` formatting | `final_data = " Information: first_name: %s , last_name: %s , age : %i" % (f_name,l_name,age)` |
|
||||||
|
| `.format()` (Positional) | `final_data = "Information: first_name: {} , last_name: {} , age : {}".format(f_name,l_name,age)` |
|
||||||
|
| `.format()` (Indexed) | `final_data = "Information: first_name: {0} , last_name: {1} , age : {2}".format(f_name,l_name,age)` |
|
||||||
|
| `.format()` (Named) | `final_data = "Information: first_name: {f} , last_name: {l} , age : {a}".format(f=f_name,l=l_name,a=age)` |
|
||||||
|
| **F-Strings (Recommended)** | `final_data = f"Information: first_name: {f_name} , last_name: {l_name} , age : {age}"` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Lists
|
||||||
|
|
||||||
|
Lists are ordered and mutable (changeable).
|
||||||
|
|
||||||
|
### Accessing and Length
|
||||||
|
|
||||||
```python
|
```python
|
||||||
f_name = 'abbas'
|
list = [1, 2, 3, 'pizze']
|
||||||
l_name = 'gholi'
|
print(list[0]) # Return 1
|
||||||
age = 25
|
print(list[1:3]) # Return [2, 3]
|
||||||
final_data = " Information: first_name: %s , last_name: %s , age : %i" % (f_name,l_name,age)
|
print(list[-1]) # Return 'pizze'
|
||||||
final_data = "Information: first_name: {} , last_name: {} , age : {}".format(f_name,l_name,age)
|
|
||||||
final_data = "Information: first_name: {0} , last_name: {1} , age : {2}".format(f_name,l_name,age)
|
|
||||||
final_data = "Information: first_name: {f} , last_name: {l} , age : {a}".format(f=f_name,l=l_name,a=age)
|
|
||||||
final_data = f"Information: first_name: {f_name} , last_name: {l_name} , age : {age}"
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
lists:
|
|
||||||
|
|
||||||
```python
|
|
||||||
list = [1,2,3,'pizze']
|
|
||||||
print(list[0]) # Return 1
|
|
||||||
print(list[1:3]) # Return 2,3
|
|
||||||
print(list[-1]) # Return 'pizze'
|
|
||||||
print(len(list)) # Return 4
|
print(len(list)) # Return 4
|
||||||
print(list.count(3)) # return 3
|
```
|
||||||
|
|
||||||
|
### List Modification and Methods
|
||||||
|
|
||||||
|
```python
|
||||||
|
list = [1, 2, 3, 'pizze']
|
||||||
|
print(list.count(3)) # return 1 (Assuming you meant count of '3')
|
||||||
|
|
||||||
|
# Pop (removes and returns the last item)
|
||||||
list_last_value = list.pop()
|
list_last_value = list.pop()
|
||||||
print(list_last_value,list) # return 'pizze' , [1,2,3]
|
print(list_last_value, list) # return 'pizze' , [1, 2, 3]
|
||||||
```
|
|
||||||
|
|
||||||
```python
|
# Sorting
|
||||||
list = [9,5,1,10]
|
list_sort = [9, 5, 1, 10]
|
||||||
list.sort()
|
list_sort.sort()
|
||||||
print(list) # [1,5,9,10]
|
print(list_sort) # [1, 5, 9, 10]
|
||||||
|
|
||||||
|
# Mutability (Changing an element)
|
||||||
|
list_mut = [9, 5, 1, 10]
|
||||||
|
list_mut[0] = 2
|
||||||
|
print(list_mut) # [2, 5, 1, 10]
|
||||||
|
|
||||||
|
# Reversing
|
||||||
|
list_mut.reverse()
|
||||||
|
print(list_mut) # [10, 1, 5, 2]
|
||||||
```
|
```
|
||||||
|
|
||||||
```python
|
---
|
||||||
list = [9,5,1,10]
|
|
||||||
list[0] = 2
|
|
||||||
print(list) # [2,5,1,10]
|
|
||||||
list.reverse()
|
|
||||||
print(list) # [10,2,5,2]
|
|
||||||
```
|
|
||||||
|
|
||||||
|
## Dictionaries
|
||||||
|
|
||||||
dictionary:
|
Dictionaries store data in key-value pairs and are mutable.
|
||||||
|
|
||||||
|
**Note:** The structure provided in the original input for nested fruits is missing commas between key-value pairs, which causes a syntax error in standard Python. The corrected structure is assumed below for accessing methods.
|
||||||
|
|
||||||
|
**Assumed Corrected Structure:**
|
||||||
```python
|
```python
|
||||||
price = {
|
price = {
|
||||||
"oil": 500000,
|
"oil": 500000,
|
||||||
"egg": 350000
|
"egg": 350000,
|
||||||
"frute" : {
|
"frute" : {
|
||||||
"apple": 100000
|
"apple": 100000,
|
||||||
"orange": 120000
|
"orange": 120000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print(price['oil'])
|
|
||||||
print(price['frute']['apple'])
|
|
||||||
print(price.keys()) # show keys
|
|
||||||
print(price.values()) # show values
|
|
||||||
print(price.items()) # show all key , values
|
|
||||||
print(price.get('egg')) # Return egg
|
|
||||||
print(price.get('water'),-1) # return -1
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Dictionary Access and Methods
|
||||||
|
|
||||||
boolian:
|
```python
|
||||||
|
print(price['oil']) # Access by key: Return 500000
|
||||||
|
print(price['frute']['apple']) # Nested access: Return 100000
|
||||||
|
|
||||||
|
print(price.keys()) # show keys
|
||||||
|
print(price.values()) # show values
|
||||||
|
print(price.items()) # show all key , values
|
||||||
|
|
||||||
|
print(price.get('egg')) # Return egg value (350000)
|
||||||
|
print(price.get('water'), -1) # Return default value (-1) if key 'water' is not found
|
||||||
|
```
|
||||||
0
Docs/Basic/02-fileio.md
Normal file
0
Docs/Basic/02-fileio.md
Normal file
Reference in New Issue
Block a user