Porównaj commity

...

4 Commity

Autor SHA1 Wiadomość Data
Ankit Mahato d3dc992ccc Add copy module 2023-03-29 07:07:00 +05:30
Ankit Mahato 2752111ddf Builtin functions list & sorted on dict 2023-03-29 06:52:13 +05:30
Ankit Mahato 52048d3d5a Update dictionary |= operator 2023-03-29 06:46:18 +05:30
Ankit Mahato f3392f3ef3 Minor fixes 2023-03-29 06:24:22 +05:30
1 zmienionych plików z 119 dodań i 5 usunięć

124
README.md
Wyświetl plik

@ -658,7 +658,7 @@ The backslash (`\`) character can be used in a string literal to escape characte
| `\"` | Double quote (`"`) |
| `\a` | ASCII Bell (BEL) |
| `\b` | ASCII Backspace (BS) |
| `\f` | ASCII Formfeed (FF) |
| `\f` | ASCII Form-feed (FF) |
| `\n` | ASCII Linefeed (LF) |
| `\r` | ASCII Carriage Return (CR) |
| `\t` | ASCII Horizontal Tab (TAB) |
@ -3539,7 +3539,7 @@ The backslash (`\`) character can be used in a string to escape characters that
| `\"` | Double quote (`"`) |
| `\a` | ASCII Bell (BEL) |
| `\b` | ASCII Backspace (BS) |
| `\f` | ASCII Formfeed (FF) |
| `\f` | ASCII Form-feed (FF) |
| `\n` | ASCII Linefeed (LF) |
| `\r` | ASCII Carriage Return (CR) |
| `\t` | ASCII Horizontal Tab (TAB) |
@ -5811,6 +5811,38 @@ In case of common keys between the two operands (dictionaries), the values of th
In the above example, both `d` and `n` shared a common key `yr`. The value corresponding to `yr` in `n` gets priority.
The `|=` (union) augmented assignment operator can be used to update the dictionary with keys and values from another dictionary or an iterable of (key, value) pairs.
``` python
>>> d = {"book": "Python", "year": 1990}
>>> dnew = {"author": "Guido"}
>>> d |= dnew
>>> d
{'book': 'Python', 'year': 1990, 'author': 'Guido'}
```
If both the dictionaries share a common key, then the value corresponding to the key in the right operand is the updated value.
``` python
>>> d = {"book": "Python", "year": 1990}
>>> dnew = {"author": "Guido", "year": 2000}
>>> d |= dnew
>>> d
{'book': 'Python', 'year': 2000, 'author': 'Guido'}
```
In the above example, both `d` and `dnew` shared a common key `year`. The value (`1990`) corresponding to `year` in `d` is updated with the new value.
The `|=` operator also works in case the right operand is an iterable instead of a dictionary as shown in the example below.
``` python
>>> d = {"book": "Python", "year": 1990}
>>> inew = [("author", "Guido"), ("year", 2000)]
>>> d |= inew
>>> d
{'book': 'Python', 'year': 2000, 'author': 'Guido'}
```
## Traversing a Dictionary
Compared to position based indexing of `list` or `tuple`, dictionaries are indexed based on the key.
@ -5936,6 +5968,26 @@ In case of string keys, they return the first and last occurring strings alphabe
3
```
### list()
To get the list of all keys in a dictionary, use the `list()` built-in function.
``` python
>>> d = {"book": "Python", "year": 1990, "author": "Guido"}
>>> list(d)
['book', 'year', 'author']
```
### sorted()
To get a sorted list of keys, you can use the `sorted()` built-in function.
``` python
>>> d = {"book": "Python", "year": 1990, "author": "Guido"}
>>> sorted(d)
['author', 'book', 'year']
```
## Creating a Copy of a Dictionary
A new copy of a dictionary can be made using `copy()` method:
@ -6181,7 +6233,7 @@ Python has built-in functions to handle various aspects of datatypes such as che
`type()` and `isinstance()` builtin functions are used for checking the data type of objects.
Check out the **Type Checking** section in the chapter **Variable, Objects & Data Types** to learn more about it in detail.
Check out [Type Checking](#type-checking) section in the chapter [Variable, Objects & Data Types](#variables-objects--data-types) to learn more about it in detail.
### Built-in Type Functions
@ -6202,7 +6254,7 @@ The following functions are often used to assign a default value or create an em
| `frozenset()` | `frozenset()` |
| `dict()` | `{}` |
The **Type Casting** section of the chapter **Variable, Objects & Data Types** covers these functions in detail in case any argument is passed.
The [Type Casting](#type-casting) section of the chapter [Variable, Objects & Data Types](#variables-objects--data-types) covers these functions in detail in case any argument is passed.
## I/O Functions
@ -6323,11 +6375,12 @@ Converts the integer Unicode code point into the corresponding Unicode string.
Apart from built-in functions, the **Python Standard Library** also contains a wide range of built-in modules which are a group of functions organized based on functionality.
Some of the commonly used modules are mentioned below:
Some commonly used modules are:
- `math` - Mathematical functions
- `random` - Generate pseudo-random numbers
- `statistics` - Statistical functions
- `copy` - Create shallow and deep copy of objects
**Accessing Modules**
@ -6559,6 +6612,67 @@ If there are multiple modes with the same count, the first occurrence in the seq
'a'
```
## copy Module
### Limitation of Shallow Copy
`copy()` method does not recurse to create copies of the child objects, so if the child objects are mutable (example nested list) any modification in the child object will get reflected in the both the parent objects.
``` python
>>> old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Copying old list into a new list
>>> new_list = old_list.copy()
# Checking if both lists are pointing to the same object
>>> id(new_list)==id(old_list)
False
# Checking if items of both lists are pointing to the same objects
>>> [id(new_list[idx])==id(old_list[idx]) for idx in range(len(old_list))]
[True, True, True]
# Modify new list
>>> new_list[1][1] = 0
>>> new_list
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
>>> old_list
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
```
As we can see in the output, `new_list[1][1]` was modified which is reflected in both `new_list` and `old_list`.
The `copy` module provides the `deepcopy()` function which is helpful in mitigating this issue.
### Deep Copy - deepcopy(x[, memo])
Deep copy overcomes the shortcomings of `copy()` and recursively creates copies of the child objects found in the original list. This leads to the creation of an independent copy of the original.
``` python
>>> import copy
>>> old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Copying old list into a new list
>>> new_list = copy.deepcopy(old_list)
# Checking if both lists are pointing to the same object
>>> id(new_list)==id(old_list)
False
# Checking if items of both lists are pointing to the same objects
>>> [id(new_list[idx])==id(old_list[idx]) for idx in range(len(old_list))]
[False, False, False]
# Modify new list
>>> new_list[1][1] = 0
>>> new_list
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
>>> old_list
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```
Note the change in the `id()` equality of the children for `[True, True, True]` to `[False, False, False]`. As we can see in the output, `new_list[1][1]` was modified which gets reflected only in the `new_list`.
# File Handling
## File Handling in Python - Introduction & Overview