Skip to content

Commit 62b7fe5

Browse files
authored
Merge branch 'main' into feat/add-numpy-byteswap-term
2 parents 7972a1f + 6c57034 commit 62b7fe5

File tree

10 files changed

+940
-0
lines changed

10 files changed

+940
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
Title: '.front()'
3+
Description: 'Returns a reference to the first element of the deque'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Classes'
9+
- 'Containers'
10+
- 'Deques'
11+
- 'OOP'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
In C++, the **`.front()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) returns a reference to the first element in the [deque](https://www.codecademy.com/resources/docs/python/deque).
18+
19+
## Syntax
20+
21+
```pseudo
22+
deque_name.front()
23+
```
24+
25+
**Parameters:**
26+
27+
`.front()` does not take any parameters
28+
29+
**Return value:**
30+
31+
- Returns a reference to the first element of the deque.
32+
- For a non-const deque, returns `T&` (modifiable).
33+
- For a const deque, returns const `T&` (read-only).
34+
35+
## Example
36+
37+
The example below demonstrates use of `.front()` to access the first element in a deque:
38+
39+
```cpp
40+
#include <iostream>
41+
#include <deque>
42+
43+
int main() {
44+
// Create a deque of integers
45+
std::deque<int> numbers;
46+
47+
// Add some elements to the deque
48+
numbers.push_back(100);
49+
numbers.push_back(200);
50+
numbers.push_back(300);
51+
52+
// Access the first element using .front()
53+
std::cout << "First element: " << numbers.front() << std::endl;
54+
55+
// Modify the first element
56+
numbers.front() = 50;
57+
58+
// Display updated deque contents
59+
std::cout << "Updated deque: ";
60+
for (int num : numbers) {
61+
std::cout << num << " ";
62+
}
63+
std::cout << std::endl;
64+
65+
return 0;
66+
}
67+
```
68+
69+
The output of this program will be:
70+
71+
```shell
72+
First element: 100
73+
Updated deque: 50 200 300
74+
```
75+
76+
This shows that `.front()` allows both access and modification of the deque’s first element.
77+
78+
## Codebyte Example
79+
80+
Run the following codebyte example to understand the use of `.front()` method:
81+
82+
```codebyte/cpp
83+
#include <iostream>
84+
#include <deque>
85+
#include <string>
86+
87+
int main() {
88+
// Create a deque of strings
89+
std::deque<std::string> myDeque;
90+
91+
// Add elements to the deque
92+
myDeque.push_back("Hello");
93+
myDeque.push_back("World");
94+
myDeque.push_back("!");
95+
96+
// Print the front element
97+
std::cout << "Front element before change: " << myDeque.front() << std::endl;
98+
99+
// Modify the front element
100+
myDeque.front() = "Hi";
101+
102+
// Print the modified front element
103+
std::cout << "Front element after change: " << myDeque.front() << std::endl;
104+
105+
// Print all elements of the deque
106+
std::cout << "Complete deque: ";
107+
for (const auto& str : myDeque) {
108+
std::cout << str << " ";
109+
}
110+
std::cout << std::endl;
111+
112+
return 0;
113+
}
114+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
Title: '.xlim()'
3+
Description: 'Gets or sets the x-axis limits of the current axes in a Matplotlib plot.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Functions'
9+
- 'Matplotlib'
10+
- 'Subplots'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.xlim()`** function in `matplotlib.pyplot` is used to get or set the x-axis limits of the current plot. It helps control the visible range of data on the x-axis, zoom in on specific sections, or maintain consistent axis ranges across multiple plots.
17+
18+
The primary use cases for `.xlim()` include:
19+
20+
- Focusing on a particular range of x-values.
21+
- Maintaining consistent axis scaling between multiple plots.
22+
- Preventing automatic resizing that hides important data.
23+
24+
## Syntax
25+
26+
```pseudo
27+
matplotlib.pyplot.xlim(*args, **kwargs)
28+
```
29+
30+
**Parameters:**
31+
32+
The function accepts the following keyword arguments:
33+
34+
- `left` or `xmin` (float, optional): Lower limit of the x-axis.
35+
- `right` or `xmax` (float, optional): Upper limit of the x-axis.
36+
- `emit` (bool, default: True): Whether observers are notified of the axis limit change (default: True).
37+
- `auto` (bool, default: True): If `True`, turns on automatic scaling.
38+
39+
**Return value:**
40+
41+
The `.xlim()` function returns a tuple (xmin, xmax) representing the current x-axis limits.
42+
43+
## Example 1: Setting Custom X-Axis Limits
44+
45+
This example shows how to set specific x-axis limits using `.xlim()`:
46+
47+
```py
48+
import matplotlib.pyplot as plt
49+
import numpy as np
50+
51+
x = np.linspace(0, 20, 100)
52+
y = np.sin(x)
53+
54+
plt.plot(x, y, color='purple', linewidth=2)
55+
plt.title('Sine Wave with Custom X Limits')
56+
plt.xlabel('X')
57+
plt.ylabel('sin(x)')
58+
59+
# Set x-axis limits
60+
plt.xlim(0, 10)
61+
62+
plt.grid(True)
63+
plt.show()
64+
```
65+
66+
The output generated by this code will be:
67+
68+
![Line plot showing sine wave with limited x-axis range](https://raw.githubusercontent.com/Codecademy/docs/main/media/xlim-Plot-1.png)
69+
70+
This code plots a sine wave from 0 to 20 on the x-axis, but the `plt.xlim(0, 10)` command restricts the visible range to data between x = 0 and x = 10. As a result, only the first half of the sine wave is displayed. The curve appears smooth and purple, with grid lines and labeled axes for clarity.
71+
72+
## Example 2: Getting Current X-Axis Limits
73+
74+
Calling `.xlim()` without arguments retrieves the current x-axis limits:
75+
76+
```py
77+
import matplotlib.pyplot as plt
78+
79+
plt.plot([0, 1, 2, 3], [10, 20, 25, 30])
80+
print(plt.xlim()) # Outputs current x-axis limits
81+
plt.show()
82+
```
83+
84+
The output generated by this code will be:
85+
86+
![Line plot of points (0,10), (1,20), (2,25), (3,30) with automatically determined x-axis limits](https://raw.githubusercontent.com/Codecademy/docs/main/media/xlim-Plot-2.png)
87+
88+
In this case, the output is:
89+
90+
```shell
91+
(np.float64(-0.15), np.float64(3.15))
92+
```
93+
94+
This indicates that Matplotlib automatically adds a small margin around the plotted points for better visualization. The `.xlim()` function can also be applied after plotting to dynamically adjust the x-axis limits, as demonstrated in Example 1.

0 commit comments

Comments
 (0)