programming updates
This commit is contained in:
58
tech_docs/arrays.md
Normal file
58
tech_docs/arrays.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
Certainly! In Python, array sequences are data structures that store a collection of elements in a specific order. The three main built-in array sequence types in Python are lists, tuples, and strings. Let's explore each of them:
|
||||||
|
|
||||||
|
1. Lists (square brackets `[]`):
|
||||||
|
- Lists are mutable, meaning you can modify, add, or remove elements after creation.
|
||||||
|
- Elements in a list can be of different data types (e.g., integers, strings, objects).
|
||||||
|
- Lists are defined using square brackets `[]` and elements are separated by commas.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [1, 2, 3, "apple", True]
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Tuples (parentheses `()`):
|
||||||
|
- Tuples are immutable, meaning you cannot modify them once they are created.
|
||||||
|
- Elements in a tuple can be of different data types, similar to lists.
|
||||||
|
- Tuples are defined using parentheses `()` and elements are separated by commas.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_tuple = (1, 2, 3, "apple", True)
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Strings (double quotes `""`):
|
||||||
|
- Strings are immutable sequences of characters.
|
||||||
|
- They are defined using either single quotes `''` or double quotes `""`.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_string = "Hello, World!"
|
||||||
|
```
|
||||||
|
|
||||||
|
Indexing:
|
||||||
|
All three array sequence types support indexing, which allows you to access individual elements within the sequence using their position or index. In Python, indexing starts from 0.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
my_list = [1, 2, 3, 4, 5]
|
||||||
|
print(my_list[0]) # Output: 1
|
||||||
|
print(my_list[2]) # Output: 3
|
||||||
|
|
||||||
|
my_tuple = (1, 2, 3, 4, 5)
|
||||||
|
print(my_tuple[1]) # Output: 2
|
||||||
|
print(my_tuple[4]) # Output: 5
|
||||||
|
|
||||||
|
my_string = "Hello"
|
||||||
|
print(my_string[0]) # Output: 'H'
|
||||||
|
print(my_string[4]) # Output: 'o'
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also use negative indexing to access elements from the end of the sequence. For example, `-1` refers to the last element, `-2` refers to the second-to-last element, and so on.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```python
|
||||||
|
my_list = [1, 2, 3, 4, 5]
|
||||||
|
print(my_list[-1]) # Output: 5
|
||||||
|
print(my_list[-3]) # Output: 3
|
||||||
|
```
|
||||||
|
|
||||||
|
In addition to indexing, array sequences in Python support various operations like slicing (extracting a portion of the sequence), concatenation (joining sequences together), and more.
|
||||||
|
|
||||||
|
I hope this helps you understand array sequences, lists, tuples, strings, and indexing in Python better! Let me know if you have any further questions.
|
||||||
57
tech_docs/indexing.md
Normal file
57
tech_docs/indexing.md
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
Certainly! Indexing is a fundamental operation in Python that allows you to access individual elements within an array sequence (such as a list, tuple, or string) based on their position. It is useful in various scenarios:
|
||||||
|
|
||||||
|
1. Accessing specific elements:
|
||||||
|
- When you have an array sequence and need to retrieve a specific element, indexing allows you to directly access that element using its position.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [10, 20, 30, 40, 50]
|
||||||
|
first_element = my_list[0] # Accessing the first element
|
||||||
|
third_element = my_list[2] # Accessing the third element
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Modifying elements (for mutable sequences like lists):
|
||||||
|
- Indexing allows you to modify specific elements within a mutable sequence by assigning a new value to the desired position.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [10, 20, 30, 40, 50]
|
||||||
|
my_list[1] = 25 # Modifying the second element
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Iterating over elements:
|
||||||
|
- Indexing is often used in combination with loops to iterate over the elements of an array sequence.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [10, 20, 30, 40, 50]
|
||||||
|
for i in range(len(my_list)):
|
||||||
|
print(my_list[i])
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Slicing:
|
||||||
|
- Indexing is used to define slices, which allow you to extract a portion of an array sequence.
|
||||||
|
- Slicing is done using the syntax `sequence[start:end:step]`, where `start` is the starting index (inclusive), `end` is the ending index (exclusive), and `step` is the stride or step size.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [10, 20, 30, 40, 50]
|
||||||
|
subset = my_list[1:4] # Extracting elements from index 1 to 3
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Checking membership:
|
||||||
|
- Indexing can be used in combination with conditional statements to check if an element exists at a specific position within an array sequence.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_list = [10, 20, 30, 40, 50]
|
||||||
|
if my_list[2] == 30:
|
||||||
|
print("The third element is 30")
|
||||||
|
```
|
||||||
|
|
||||||
|
6. Accessing characters in strings:
|
||||||
|
- Since strings are also array sequences, indexing allows you to access individual characters within a string.
|
||||||
|
- Example:
|
||||||
|
```python
|
||||||
|
my_string = "Hello"
|
||||||
|
first_char = my_string[0] # Accessing the first character
|
||||||
|
```
|
||||||
|
|
||||||
|
These are just a few examples of how indexing is useful in Python. Indexing is a fundamental operation that is widely used in various scenarios when working with array sequences. It provides a way to directly access, modify, and manipulate elements based on their position within the sequence.
|
||||||
|
|
||||||
|
Indexing is efficient in Python because array sequences are implemented as contiguous blocks of memory, allowing for fast access to elements based on their index. This makes indexing a valuable tool for performing operations on array sequences efficiently.
|
||||||
Reference in New Issue
Block a user