forked from iam-veeramalla/python-for-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from iam-veeramalla/main
Pulling upto Day-5
- Loading branch information
Showing
26 changed files
with
471 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Certainly! The choice between using shell scripting and Python in DevOps depends on the specific task or problem you're trying to solve. Both have their strengths and are suitable for different scenarios. Here are some guidelines to help you decide when to use each: | ||
|
||
**Use Shell Scripting When:** | ||
|
||
1. **System Administration Tasks:** Shell scripting is excellent for automating routine system administration tasks like managing files, directories, and processes. You can use shell scripts for tasks like starting/stopping services, managing users, and basic file manipulation. | ||
|
||
2. **Command Line Interactions:** If your task primarily involves running command line tools and utilities, shell scripting can be more efficient. It's easy to call and control these utilities from a shell script. | ||
|
||
3. **Rapid Prototyping:** If you need to quickly prototype a solution or perform one-off tasks, shell scripting is usually faster to write and execute. It's great for ad-hoc tasks. | ||
|
||
4. **Text Processing:** Shell scripting is well-suited for tasks that involve text manipulation, such as parsing log files, searching and replacing text, or extracting data from text-based sources. | ||
|
||
5. **Environment Variables and Configuration:** Shell scripts are useful for managing environment variables and configuring your system. | ||
|
||
**Use Python When:** | ||
|
||
1. **Complex Logic:** Python is a full-fledged programming language and is well-suited for tasks that involve complex logic, data structures, and algorithms. If your task requires extensive data manipulation, Python can be a more powerful choice. | ||
|
||
2. **Cross-Platform Compatibility:** Python is more platform-independent than shell scripting, making it a better choice for tasks that need to run on different operating systems. | ||
|
||
3. **API Integration:** Python has extensive libraries and modules for interacting with APIs, databases, and web services. If your task involves working with APIs, Python may be a better choice. | ||
|
||
4. **Reusable Code:** If you plan to reuse your code or build larger applications, Python's structure and modularity make it easier to manage and maintain. | ||
|
||
5. **Error Handling:** Python provides better error handling and debugging capabilities, which can be valuable in DevOps where reliability is crucial. | ||
|
||
6. **Advanced Data Processing:** If your task involves advanced data processing, data analysis, or machine learning, Python's rich ecosystem of libraries (e.g., Pandas, NumPy, SciPy) makes it a more suitable choice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("Hello, World!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Data Types | ||
|
||
In programming, a data type is a classification or categorization that specifies which type of value a variable can hold. Data types are essential because they determine how data is stored in memory and what operations can be performed on that data. Python, like many programming languages, supports several built-in data types. Here are some of the common data types in Python: | ||
|
||
1. **Numeric Data Types:** | ||
- **int**: Represents integers (whole numbers). Example: `x = 5` | ||
- **float**: Represents floating-point numbers (numbers with decimal points). Example: `y = 3.14` | ||
- **complex**: Represents complex numbers. Example: `z = 2 + 3j` | ||
|
||
2. **Sequence Types:** | ||
- **str**: Represents strings (sequences of characters). Example: `text = "Hello, World"` | ||
- **list**: Represents lists (ordered, mutable sequences). Example: `my_list = [1, 2, 3]` | ||
- **tuple**: Represents tuples (ordered, immutable sequences). Example: `my_tuple = (1, 2, 3)` | ||
|
||
3. **Mapping Type:** | ||
- **dict**: Represents dictionaries (key-value pairs). Example: `my_dict = {'name': 'John', 'age': 30}` | ||
|
||
4. **Set Types:** | ||
- **set**: Represents sets (unordered collections of unique elements). Example: `my_set = {1, 2, 3}` | ||
- **frozenset**: Represents immutable sets. Example: `my_frozenset = frozenset([1, 2, 3])` | ||
|
||
5. **Boolean Type:** | ||
- **bool**: Represents Boolean values (`True` or `False`). Example: `is_valid = True` | ||
|
||
6. **Binary Types:** | ||
- **bytes**: Represents immutable sequences of bytes. Example: `data = b'Hello'` | ||
- **bytearray**: Represents mutable sequences of bytes. Example: `data = bytearray(b'Hello')` | ||
|
||
7. **None Type:** | ||
- **NoneType**: Represents the `None` object, which is used to indicate the absence of a value or a null value. | ||
|
||
8. **Custom Data Types:** | ||
- You can also define your custom data types using classes and objects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Strings | ||
|
||
**1. String Data Type in Python:** | ||
|
||
- In Python, a string is a sequence of characters, enclosed within single (' '), double (" "), or triple (''' ''' or """ """) quotes. | ||
- Strings are immutable, meaning you cannot change the characters within a string directly. Instead, you create new strings. | ||
- You can access individual characters in a string using indexing, e.g., `my_string[0]` will give you the first character. | ||
- Strings support various built-in methods, such as `len()`, `upper()`, `lower()`, `strip()`, `replace()`, and more, for manipulation. | ||
|
||
**2. String Manipulation and Formatting:** | ||
|
||
- Concatenation: You can combine strings using the `+` operator. | ||
- Substrings: Use slicing to extract portions of a string, e.g., `my_string[2:5]` will extract characters from the 2nd to the 4th position. | ||
- String interpolation: Python supports various ways to format strings, including f-strings (f"...{variable}..."), %-formatting ("%s %d" % ("string", 42)), and `str.format()`. | ||
- Escape sequences: Special characters like newline (\n), tab (\t), and others are represented using escape sequences. | ||
- String methods: Python provides many built-in methods for string manipulation, such as `split()`, `join()`, and `startswith()`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Numberic Data Type | ||
|
||
**1. Numeric Data Types in Python (int, float):** | ||
|
||
- Python supports two primary numeric data types: `int` for integers and `float` for floating-point numbers. | ||
- Integers are whole numbers, and floats can represent both whole and fractional numbers. | ||
- You can perform arithmetic operations on these types, including addition, subtraction, multiplication, division, and more. | ||
- Be aware of potential issues with floating-point precision, which can lead to small inaccuracies in calculations. | ||
- Python also provides built-in functions for mathematical operations, such as `abs()`, `round()`, and `math` module for advanced functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Regex | ||
|
||
**1. Regular Expressions for Text Processing:** | ||
|
||
- Regular expressions (regex or regexp) are a powerful tool for pattern matching and text processing. | ||
- The `re` module in Python is used for working with regular expressions. | ||
- Common metacharacters: `.` (any character), `*` (zero or more), `+` (one or more), `?` (zero or one), `[]` (character class), `|` (OR), `^` (start of a line), `$` (end of a line), etc. | ||
- Examples of regex usage: matching emails, phone numbers, or extracting data from text. | ||
- `re` module functions include `re.match()`, `re.search()`, `re.findall()`, and `re.sub()` for pattern matching and replacement. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
str1 = "Hello" | ||
str2 = "World" | ||
result = str1 + " " + str2 | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
text = "Python is awesome" | ||
length = len(text) | ||
print("Length of the string:", length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
text = "Python is awesome" | ||
uppercase = text.upper() | ||
lowercase = text.lower() | ||
print("Uppercase:", uppercase) | ||
print("Lowercase:", lowercase) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
text = "Python is awesome" | ||
new_text = text.replace("awesome", "great") | ||
print("Modified text:", new_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
text = "Python is awesome" | ||
words = text.split() | ||
print("Words:", words) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
text = " Some spaces around " | ||
stripped_text = text.strip() | ||
print("Stripped text:", stripped_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
text = "Python is awesome" | ||
substring = "is" | ||
if substring in text: | ||
print(substring, "found in the text") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Float variables | ||
num1 = 5.0 | ||
num2 = 2.5 | ||
|
||
# Basic Arithmetic | ||
result1 = num1 + num2 | ||
print("Addition:", result1) | ||
|
||
result2 = num1 - num2 | ||
print("Subtraction:", result2) | ||
|
||
result3 = num1 * num2 | ||
print("Multiplication:", result3) | ||
|
||
result4 = num1 / num2 | ||
print("Division:", result4) | ||
|
||
# Rounding | ||
result5 = round(3.14159265359, 2) # Rounds to 2 decimal places | ||
print("Rounded:", result5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Integer variables | ||
num1 = 10 | ||
num2 = 5 | ||
|
||
# Integer Division | ||
result1 = num1 // num2 | ||
print("Integer Division:", result1) | ||
|
||
# Modulus (Remainder) | ||
result2 = num1 % num2 | ||
print("Modulus (Remainder):", result2) | ||
|
||
# Absolute Value | ||
result3 = abs(-7) | ||
print("Absolute Value:", result3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import re | ||
|
||
text = "The quick brown fox" | ||
pattern = r"brown" | ||
|
||
search = re.search(pattern, text) | ||
if search: | ||
print("Pattern found:", search.group()) | ||
else: | ||
print("Pattern not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import re | ||
|
||
text = "The quick brown fox" | ||
pattern = r"quick" | ||
|
||
match = re.match(pattern, text) | ||
if match: | ||
print("Match found:", match.group()) | ||
else: | ||
print("No match") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import re | ||
|
||
text = "The quick brown fox jumps over the lazy brown dog" | ||
pattern = r"brown" | ||
|
||
replacement = "red" | ||
|
||
new_text = re.sub(pattern, replacement, text) | ||
print("Modified text:", new_text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import re | ||
|
||
text = "The quick brown fox" | ||
pattern = r"brown" | ||
|
||
search = re.search(pattern, text) | ||
if search: | ||
print("Pattern found:", search.group()) | ||
else: | ||
print("Pattern not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import re | ||
|
||
text = "apple,banana,orange,grape" | ||
pattern = r"," | ||
|
||
split_result = re.split(pattern, text) | ||
print("Split result:", split_result) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Keywords and Variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Keywords in Python: | ||
|
||
Keywords are reserved words in Python that have predefined meanings and cannot be used as variable names or identifiers. These words are used to define the structure and logic of the program. They are an integral part of the Python language and are case-sensitive, which means you must use them exactly as specified. | ||
|
||
Here are some important Python keywords: | ||
|
||
1. **and**: It is a logical operator that returns `True` if both operands are true. | ||
|
||
2. **or**: It is a logical operator that returns `True` if at least one of the operands is true. | ||
|
||
3. **not**: It is a logical operator that returns the opposite of the operand's truth value. | ||
|
||
4. **if**: It is used to start a conditional statement and is followed by a condition that determines whether the code block is executed. | ||
|
||
5. **else**: It is used in conjunction with `if` to define an alternative code block to execute when the `if` condition is `False`. | ||
|
||
6. **elif**: Short for "else if," it is used to check additional conditions after an `if` statement and is used in combination with `if` and `else`. | ||
|
||
7. **while**: It is used to create a loop that repeatedly executes a block of code as long as a specified condition is true. | ||
|
||
8. **for**: It is used to create a loop that iterates over a sequence (such as a list, tuple, or string) and executes a block of code for each item in the sequence. | ||
|
||
9. **in**: Used with `for`, it checks if a value is present in a sequence. | ||
|
||
10. **try**: It is the beginning of a block of code that is subject to exception handling. It is followed by `except` to catch and handle exceptions. | ||
|
||
11. **except**: Used with `try`, it defines a block of code to execute when an exception is raised in the corresponding `try` block. | ||
|
||
12. **finally**: Used with `try`, it defines a block of code that is always executed, whether an exception is raised or not. | ||
|
||
13. **def**: It is used to define a function in Python. | ||
|
||
14. **return**: It is used within a function to specify the value that the function should return. | ||
|
||
15. **class**: It is used to define a class, which is a blueprint for creating objects in object-oriented programming. | ||
|
||
16. **import**: It is used to import modules or libraries to access their functions, classes, or variables. | ||
|
||
17. **from**: Used with `import` to specify which specific components from a module should be imported. | ||
|
||
18. **as**: Used with `import` to create an alias for a module, making it easier to reference in the code. | ||
|
||
19. **True**: It represents a boolean value for "true." | ||
|
||
20. **False**: It represents a boolean value for "false." | ||
|
||
21. **None**: It represents a special null value or absence of value. | ||
|
||
22. **is**: It is used for identity comparison, checking if two variables refer to the same object in memory. | ||
|
||
23. **lambda**: It is used to create small, anonymous functions (lambda functions). | ||
|
||
24. **with**: It is used for context management, ensuring that certain operations are performed before and after a block of code. | ||
|
||
25. **global**: It is used to declare a global variable within a function's scope. | ||
|
||
26. **nonlocal**: It is used to declare a variable as nonlocal, which allows modifying a variable in an enclosing (but non-global) scope. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Understanding Variables in Python: | ||
|
||
In Python, a variable is a named storage location used to store data. Variables are essential for programming as they allow us to work with data, manipulate it, and make our code more flexible and reusable. | ||
|
||
#### Example: | ||
|
||
```python | ||
# Assigning a value to a variable | ||
my_variable = 42 | ||
|
||
# Accessing the value of a variable | ||
print(my_variable) # Output: 42 | ||
``` | ||
|
||
### Variable Scope and Lifetime: | ||
|
||
**Variable Scope:** In Python, variables have different scopes, which determine where in the code the variable can be accessed. There are mainly two types of variable scopes: | ||
|
||
1. **Local Scope:** Variables defined within a function have local scope and are only accessible inside that function. | ||
|
||
```python | ||
def my_function(): | ||
x = 10 # Local variable | ||
print(x) | ||
|
||
my_function() | ||
print(x) # This will raise an error since 'x' is not defined outside the function. | ||
``` | ||
|
||
2. **Global Scope:** Variables defined outside of any function have global scope and can be accessed throughout the entire code. | ||
|
||
```python | ||
y = 20 # Global variable | ||
|
||
def another_function(): | ||
print(y) # This will access the global variable 'y' | ||
|
||
another_function() | ||
print(y) # This will print 20 | ||
``` | ||
|
||
**Variable Lifetime:** The lifetime of a variable is determined by when it is created and when it is destroyed or goes out of scope. Local variables exist only while the function is being executed, while global variables exist for the entire duration of the program. | ||
|
||
### Variable Naming Conventions and Best Practices: | ||
|
||
It's important to follow naming conventions and best practices for variables to write clean and maintainable code: | ||
|
||
- Variable names should be descriptive and indicate their purpose. | ||
- Use lowercase letters and separate words with underscores (snake_case) for variable names. | ||
- Avoid using reserved words (keywords) for variable names. | ||
- Choose meaningful names for variables. | ||
|
||
#### Example: | ||
|
||
```python | ||
# Good variable naming | ||
user_name = "John" | ||
total_items = 42 | ||
|
||
# Avoid using reserved words | ||
class = "Python" # Not recommended | ||
|
||
# Use meaningful names | ||
a = 10 # Less clear | ||
num_of_students = 10 # More descriptive | ||
``` | ||
|
||
### Practice Exercises and Examples: | ||
|
||
#### Example: Using Variables to Store and Manipulate Configuration Data in a DevOps Context | ||
|
||
In a DevOps context, you often need to manage configuration data for various services or environments. Variables are essential for this purpose. Let's consider a scenario where we need to store and manipulate configuration data for a web server. | ||
|
||
```python | ||
# Define configuration variables for a web server | ||
server_name = "my_server" | ||
port = 80 | ||
is_https_enabled = True | ||
max_connections = 1000 | ||
|
||
# Print the configuration | ||
print(f"Server Name: {server_name}") | ||
print(f"Port: {port}") | ||
print(f"HTTPS Enabled: {is_https_enabled}") | ||
print(f"Max Connections: {max_connections}") | ||
|
||
# Update configuration values | ||
port = 443 | ||
is_https_enabled = False | ||
|
||
# Print the updated configuration | ||
print(f"Updated Port: {port}") | ||
print(f"Updated HTTPS Enabled: {is_https_enabled}") | ||
``` | ||
|
||
In this example, we use variables to store and manipulate configuration data for a web server. This allows us to easily update and manage the server's configuration in a DevOps context. |
Oops, something went wrong.