Skip to content

Commit 09f5fb2

Browse files
committed
Update the project
1 parent c4bc1bb commit 09f5fb2

File tree

6 files changed

+62
-29
lines changed

6 files changed

+62
-29
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,35 @@ SOLID principles, design patterns, and best practices.
1111

1212
Ensure that the following tools are installed before setting up the project:
1313

14+
- **IDE of your choice** - PyCharm, Visual Studio Code, or any other Python IDE
1415
- **Python 3.10+** - the examples rely on modern Python features
1516
- **git** - to clone this repository
1617

1718
## Installation
1819

20+
Clone the repository to your local machine:
21+
22+
```bash
23+
git clone https://github.com/braboj/python-by-example.git
24+
```
25+
26+
Change into the project directory:
27+
28+
```bash
29+
cd python-by-example
30+
```
31+
1932
Create a virtual environment and install dependencies:
2033

21-
#### Linux/macOS
34+
- For Linx and macOS, use the following commands:
2235

2336
```bash
2437
python -m venv .venv
2538
source .venv/bin/activate
2639
pip install -r requirements.txt
2740
```
2841

29-
#### Windows
42+
- For Windows, use the following commands:
3043

3144
```cmd
3245
python -m venv .venv
@@ -36,13 +49,28 @@ pip install -r requirements.txt
3649

3750
## Usage
3851

39-
Run the examples using Python:
52+
The examples are intented to be used with an IDE of your choice so that you can
53+
run, debug, and modify them as needed. Just open the project in your IDE, and
54+
you can start exploring the examples.
55+
56+
```text
57+
.python-by-example
58+
├── .venv/ # Virtual environment directory
59+
├── examples/ # Directory containing all the examples
60+
│ ├── A01_introduction/ # Introduction examples
61+
│ │ ├── hello_world.py
62+
```
63+
64+
You can also run the examples from the command line. For example, to run the
65+
`hello_world.py` example, use the following command:
4066

4167
```bash
42-
python path/to/example.py
68+
python examples/A01_introduction/hello_world.py
4369
```
4470

4571

4672
## License
4773

48-
This project is proprietary software. Copying or distribution is prohibited without express permission from the author.
74+
This project is proprietary software. Copying or distribution is prohibited
75+
without express permission from the author. See the [LICENSE](LICENSE) file for
76+
more details.

examples/A02_variables/07_list_data.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,32 @@
1212
# - Mutable: Can be modified after creation
1313
# - Ordered: Order of elements is preserved
1414
# - Heterogeneous: Can contain elements of different types
15+
# - Passed by reference: Changes to one variable affect all references
1516

16-
# Indexed
17+
# Indexed (elements can be accessed by their index)
1718
a = [1, 2, 3]
1819
print(a[0])
1920
# Output: 1
2021

21-
# Mutable
22+
# Mutable (can be modified after creation)
2223
b = [1, 2, 3]
2324
b[0] = 4
2425
print(b)
2526
# Output: [4, 2, 3]
2627

27-
# Ordered
28+
# Ordered (order of elements is preserved)
2829
c = [2, 1, 3]
2930
print(c)
3031
# Output: [2, 1, 3]
3132

32-
# Heterogeneous
33+
# Heterogeneous (can contain elements of different types)
3334
d = [1, 'two', 3.0]
3435
print(d)
3536
# Output: [1, 'two', 3.0]
3637

37-
# (!) Passing by reference (!)
38+
# Passed by reference (changes to one variable affect all references)
3839
l1 = [1, 2, 3]
3940
l2 = l1
4041
l2[0] = 4
4142
print(l1, l2)
42-
# Output: [4, 2, 3]
43-
44-
# Copying a list
45-
l3 = l1.copy()
46-
l3[0] = 5
47-
print(l1, l3)
48-
# Output: [4, 2, 3] [5, 2, 3]
43+
# Output: [4, 2, 3] [4, 2, 3]

examples/A02_variables/08_tuple_data.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
# and performance benefits they provide over lists, especially when dealing
66
# with large datasets. They also can be used as keys in dictionaries.
77
#
8-
# - Immutable: Once created, the string cannot be changed.
9-
# - Ordered: The characters in the string maintain their order.
10-
# - Indexed: Each character in the string can be accessed by its index.
11-
# - Heterogeneous: Strings can contain characters of different types
8+
# - Immutable: Once created, the tuple cannot be changed.
9+
# - Ordered: The elements in the tuple maintain their order.
10+
# - Indexed: Each element in the tuple can be accessed by its index.
11+
# - Heterogeneous: Tuples can contain characters of different types
1212

1313
# Immutable (a change creates a new object)
1414
tuple1 = (1, 2, 3)
15-
tuple2 = tuple1 + (4, 5)
16-
print(id(tuple1), id(tuple2))
15+
tuple2 = tuple1
16+
print(tuple2 is tuple1) # Check if both variables point to the same object
17+
tuple2 += (4, 5)
18+
print(tuple2 is tuple1) # After modification, they are different objects
19+
# Output:
20+
# False
21+
# True
1722

1823
# Ordered (characters maintain their order)
1924
tuple3 = (1, 2, 3)

examples/A02_variables/09_set_data.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# union, intersection, and difference. The set type has the following
66
# characteristics:
77
#
8+
# - Passed by reference: Changes to one variable affect all references
89
# - Unique: A set cannot contain duplicate elements.
910
# - Mutable: You can add or remove elements from a set.
1011
# - Unordered: The elements in a set do not have a specific order.
@@ -18,11 +19,6 @@
1819
print(s1, s2)
1920
# Output: {1, 2, 3, 4} {1, 2, 3, 4}
2021

21-
# Copying a set
22-
s3 = s1.copy()
23-
s3.add(5)
24-
print(s1, s3)
25-
2622
# All elements are unique
2723
a = {1, 1, 2, 2, 3, 3}
2824
print(id(a), a, sep=": ")

examples/A02_variables/10_dictionary_data.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
# appears more than once, the last assigned value is retained. The
66
# dictionary variable has the following properties:
77
#
8+
# - Passed by reference: Changes to one variable affect all references
89
# - Key access: You can access values using their keys
910
# - Unique keys: Each key must be unique within the dictionary
1011
# - Mutable: Can be modified after creation
1112
# - Ordered: The order of inserted items is maintained (as of Python 3.7)
1213
# - Heterogeneous: Can contain keys and values of different types
1314

15+
# Passed by reference (changes to one variable affect all references)
16+
d1 = {'junior': 1, 'mid': 2, 'senior': 3}
17+
d2 = d1
18+
d2['junior'] = 4
19+
print(d1, d2)
20+
# Output: {'junior': 4, 'mid': 2, 'senior': 3} {'junior': 4, 'mid': 2, 'senior': 3}
21+
1422
# Key access
1523
a = {'junior': 1, 'mid': 2, 'senior': 3}
1624
print(a['mid'])

examples/A02_variables/11_string_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# Immutable (a change creates a new object)
1313
string1 = "Hello, world!"
1414
string2 = string1.replace("world", "Python")
15-
print(id(string1), id(string2))
15+
print(string2 is string1)
16+
# Output: False
1617

1718
# Ordered (characters maintain their order)
1819
string3 = "Python"

0 commit comments

Comments
 (0)