Skip to content

Commit cbe8862

Browse files
feat: add files for day-9
1 parent 9a3ae07 commit cbe8862

File tree

5 files changed

+283
-0
lines changed

5 files changed

+283
-0
lines changed

Day-09/01-loops.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Loops in Python (for and while)
2+
3+
## Introduction
4+
5+
Loops are a fundamental concept in programming, and they allow you to perform repetitive tasks efficiently. In Python, there are two primary types of loops: "for" and "while."
6+
7+
## For Loop
8+
9+
The "for" loop is used to iterate over a sequence (such as a list, tuple, string, or range) and execute a set of statements for each item in the sequence. The loop continues until all items in the sequence have been processed.
10+
11+
**Syntax:**
12+
13+
```python
14+
for variable in sequence:
15+
# Code to be executed for each item in the sequence
16+
```
17+
18+
**Example:**
19+
20+
```python
21+
fruits = ["apple", "banana", "cherry"]
22+
for fruit in fruits:
23+
print(fruit)
24+
```
25+
26+
**Output:**
27+
28+
```
29+
apple
30+
banana
31+
cherry
32+
```
33+
34+
In this example, the loop iterates over the "fruits" list, and in each iteration, the "fruit" variable takes on the value of the current item in the list.
35+
36+
#### While Loop
37+
38+
The "while" loop continues to execute a block of code as long as a specified condition is true. It's often used when you don't know in advance how many times the loop should run.
39+
40+
**Syntax:**
41+
42+
```python
43+
while condition:
44+
# Code to be executed as long as the condition is true
45+
```
46+
47+
**Example:**
48+
49+
```python
50+
count = 0
51+
while count < 5:
52+
print(count)
53+
count += 1
54+
```
55+
56+
**Output:**
57+
58+
```
59+
0
60+
1
61+
2
62+
3
63+
4
64+
```
65+
66+
In this example, the "while" loop continues to execute as long as the "count" is less than 5. The "count" variable is incremented in each iteration.

Day-09/02-loop-controls.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Loop Control Statements (break and continue)
2+
3+
## Introduction
4+
5+
Loop control statements are used to modify the behavior of loops, providing greater control and flexibility during iteration. In Python, two primary loop control statements are "break" and "continue."
6+
7+
## `break` Statement
8+
9+
The "break" statement is used to exit the loop prematurely. It can be applied to both "for" and "while" loops, allowing you to terminate the loop when a particular condition is met.
10+
11+
**Example:**
12+
13+
```python
14+
numbers = [1, 2, 3, 4, 5]
15+
for number in numbers:
16+
if number == 3:
17+
break
18+
print(number)
19+
```
20+
21+
**Output:**
22+
23+
```
24+
1
25+
2
26+
```
27+
28+
In this example, the loop stops when it encounters the number 3.
29+
30+
## `continue` Statement
31+
32+
The "continue" statement is used to skip the current iteration of the loop and proceed to the next one. It can be used in both "for" and "while" loops, enabling you to bypass certain iterations based on a condition.
33+
34+
**Example:**
35+
36+
```python
37+
numbers = [1, 2, 3, 4, 5]
38+
for number in numbers:
39+
if number == 3:
40+
continue
41+
print(number)
42+
```
43+
44+
**Output:**
45+
46+
```
47+
1
48+
2
49+
4
50+
5
51+
```
52+
53+
In this example, the loop skips the iteration where the number is 3 and continues with the next iteration.
54+
55+
## Practice Exercise - Automating Log File Analysis
56+
57+
#### Introduction
58+
59+
In this practice exercise, we use a "for" loop to automate the analysis of a log file and identify lines containing the word "error." This demonstrates how loops can be used to process data and extract relevant information efficiently.
60+
61+
**Example:**
62+
63+
```python
64+
log_file = [
65+
"INFO: Operation successful",
66+
"ERROR: File not found",
67+
"DEBUG: Connection established",
68+
"ERROR: Database connection failed",
69+
]
70+
71+
for line in log_file:
72+
if "ERROR" in line:
73+
print(line)
74+
```
75+
76+
**Output:**
77+
78+
```
79+
ERROR: File not found
80+
ERROR: Database connection failed
81+
```
82+
83+
In this exercise, the loop iterates through the "log_file" list and prints lines containing the word "ERROR."

Day-09/03-for-loop-devops-usecases.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# For Loop DevOps use-cases
2+
3+
1. **Server Provisioning and Configuration:**
4+
5+
DevOps engineers use "for" loops when provisioning multiple servers or virtual machines with the same configuration. For example, when setting up monitoring agents on multiple servers:
6+
7+
```bash
8+
servers=("server1" "server2" "server3")
9+
for server in "${servers[@]}"; do
10+
configure_monitoring_agent "$server"
11+
done
12+
```
13+
14+
2. **Deploying Configurations to Multiple Environments:**
15+
16+
When deploying configurations to different environments (e.g., development, staging, production), DevOps engineers can use a "for" loop to apply the same configuration changes to each environment:
17+
18+
```bash
19+
environments=("dev" "staging" "prod")
20+
for env in "${environments[@]}"; do
21+
deploy_configuration "$env"
22+
done
23+
```
24+
25+
3. **Backup and Restore Operations:**
26+
27+
Automating backup and restore operations is a common use case. DevOps engineers can use "for" loops to create backups for multiple databases or services and later restore them as needed.
28+
29+
```bash
30+
databases=("db1" "db2" "db3")
31+
for db in "${databases[@]}"; do
32+
create_backup "$db"
33+
done
34+
```
35+
36+
4. **Log Rotation and Cleanup:**
37+
38+
DevOps engineers use "for" loops to manage log files, rotate logs, and clean up older log files to save disk space.
39+
40+
```bash
41+
log_files=("app.log" "access.log" "error.log")
42+
for log_file in "${log_files[@]}"; do
43+
rotate_and_cleanup_logs "$log_file"
44+
done
45+
```
46+
47+
5. **Monitoring and Reporting:**
48+
49+
In scenarios where you need to gather data or perform checks on multiple systems, a "for" loop is handy. For example, monitoring server resources across multiple machines:
50+
51+
```bash
52+
servers=("server1" "server2" "server3")
53+
for server in "${servers[@]}"; do
54+
check_resource_utilization "$server"
55+
done
56+
```
57+
58+
6. **Managing Cloud Resources:**
59+
60+
When working with cloud infrastructure, DevOps engineers can use "for" loops to manage resources like virtual machines, databases, and storage across different cloud providers.
61+
62+
```bash
63+
instances=("instance1" "instance2" "instance3")
64+
for instance in "${instances[@]}"; do
65+
resize_instance "$instance"
66+
done
67+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# While Loop DevOps Usecases
2+
3+
DevOps engineers often use "while" loops in various real-time use cases to automate, monitor, and manage infrastructure and deployments. Here are some practical use cases from a DevOps engineer's perspective:
4+
5+
1. **Continuous Integration/Continuous Deployment (CI/CD) Pipeline:**
6+
7+
DevOps engineers often use "while" loops in CI/CD pipelines to monitor the deployment status of applications. They can create a "while" loop that periodically checks the status of a deployment or a rolling update until it completes successfully or fails. For example, waiting for a certain number of pods to be ready in a Kubernetes deployment:
8+
9+
```bash
10+
while kubectl get deployment/myapp | grep -q 0/1; do
11+
echo "Waiting for myapp to be ready..."
12+
sleep 10
13+
done
14+
```
15+
16+
2. **Provisioning and Scaling Cloud Resources:**
17+
18+
When provisioning or scaling cloud resources, DevOps engineers may use "while" loops to wait for the resources to be fully provisioned and ready. For instance, waiting for an Amazon EC2 instance to become available:
19+
20+
```bash
21+
while ! aws ec2 describe-instance-status --instance-ids i-1234567890abcdef0 | grep -q "running"; do
22+
echo "Waiting for the EC2 instance to be running..."
23+
sleep 10
24+
done
25+
```
26+
27+
3. **Log Analysis and Alerting:**
28+
29+
DevOps engineers can use "while" loops to continuously monitor logs for specific events or errors and trigger alerts when a certain condition is met. For example, tailing a log file and alerting when an error is detected:
30+
31+
```bash
32+
while true; do
33+
if tail -n 1 /var/log/app.log | grep -q "ERROR"; then
34+
send_alert "Error detected in the log."
35+
fi
36+
sleep 5
37+
done
38+
```
39+
40+
4. **Database Replication and Data Synchronization:**
41+
42+
DevOps engineers use "while" loops to monitor database replication and ensure data consistency across multiple database instances. The loop can check for replication lag and trigger corrective actions when necessary.
43+
44+
```bash
45+
while true; do
46+
replication_lag=$(mysql -e "SHOW SLAVE STATUS\G" | grep "Seconds_Behind_Master" | awk '{print $2}')
47+
if [ "$replication_lag" -gt 60 ]; then
48+
trigger_data_sync
49+
fi
50+
sleep 60
51+
done
52+
```
53+
54+
5. **Service Health Monitoring and Auto-Recovery:**
55+
56+
DevOps engineers can use "while" loops to continuously check the health of services and automatically trigger recovery actions when services become unhealthy.
57+
58+
```bash
59+
while true; do
60+
if ! check_service_health; then
61+
restart_service
62+
fi
63+
sleep 30
64+
done
65+
```
66+

Day-09/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Loops

0 commit comments

Comments
 (0)