Skip to content

Commit 5a310fc

Browse files
add files for day-3
1 parent 98356cb commit 5a310fc

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

Day-03/README.md

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

Day-03/keywords.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Keywords in Python:
2+
3+
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.
4+
5+
Here are some important Python keywords:
6+
7+
1. **and**: It is a logical operator that returns `True` if both operands are true.
8+
9+
2. **or**: It is a logical operator that returns `True` if at least one of the operands is true.
10+
11+
3. **not**: It is a logical operator that returns the opposite of the operand's truth value.
12+
13+
4. **if**: It is used to start a conditional statement and is followed by a condition that determines whether the code block is executed.
14+
15+
5. **else**: It is used in conjunction with `if` to define an alternative code block to execute when the `if` condition is `False`.
16+
17+
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`.
18+
19+
7. **while**: It is used to create a loop that repeatedly executes a block of code as long as a specified condition is true.
20+
21+
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.
22+
23+
9. **in**: Used with `for`, it checks if a value is present in a sequence.
24+
25+
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.
26+
27+
11. **except**: Used with `try`, it defines a block of code to execute when an exception is raised in the corresponding `try` block.
28+
29+
12. **finally**: Used with `try`, it defines a block of code that is always executed, whether an exception is raised or not.
30+
31+
13. **def**: It is used to define a function in Python.
32+
33+
14. **return**: It is used within a function to specify the value that the function should return.
34+
35+
15. **class**: It is used to define a class, which is a blueprint for creating objects in object-oriented programming.
36+
37+
16. **import**: It is used to import modules or libraries to access their functions, classes, or variables.
38+
39+
17. **from**: Used with `import` to specify which specific components from a module should be imported.
40+
41+
18. **as**: Used with `import` to create an alias for a module, making it easier to reference in the code.
42+
43+
19. **True**: It represents a boolean value for "true."
44+
45+
20. **False**: It represents a boolean value for "false."
46+
47+
21. **None**: It represents a special null value or absence of value.
48+
49+
22. **is**: It is used for identity comparison, checking if two variables refer to the same object in memory.
50+
51+
23. **lambda**: It is used to create small, anonymous functions (lambda functions).
52+
53+
24. **with**: It is used for context management, ensuring that certain operations are performed before and after a block of code.
54+
55+
25. **global**: It is used to declare a global variable within a function's scope.
56+
57+
26. **nonlocal**: It is used to declare a variable as nonlocal, which allows modifying a variable in an enclosing (but non-global) scope.

Day-03/variables.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Understanding Variables in Python:
2+
3+
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.
4+
5+
#### Example:
6+
7+
```python
8+
# Assigning a value to a variable
9+
my_variable = 42
10+
11+
# Accessing the value of a variable
12+
print(my_variable) # Output: 42
13+
```
14+
15+
### Variable Scope and Lifetime:
16+
17+
**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:
18+
19+
1. **Local Scope:** Variables defined within a function have local scope and are only accessible inside that function.
20+
21+
```python
22+
def my_function():
23+
x = 10 # Local variable
24+
print(x)
25+
26+
my_function()
27+
print(x) # This will raise an error since 'x' is not defined outside the function.
28+
```
29+
30+
2. **Global Scope:** Variables defined outside of any function have global scope and can be accessed throughout the entire code.
31+
32+
```python
33+
y = 20 # Global variable
34+
35+
def another_function():
36+
print(y) # This will access the global variable 'y'
37+
38+
another_function()
39+
print(y) # This will print 20
40+
```
41+
42+
**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.
43+
44+
### Variable Naming Conventions and Best Practices:
45+
46+
It's important to follow naming conventions and best practices for variables to write clean and maintainable code:
47+
48+
- Variable names should be descriptive and indicate their purpose.
49+
- Use lowercase letters and separate words with underscores (snake_case) for variable names.
50+
- Avoid using reserved words (keywords) for variable names.
51+
- Choose meaningful names for variables.
52+
53+
#### Example:
54+
55+
```python
56+
# Good variable naming
57+
user_name = "John"
58+
total_items = 42
59+
60+
# Avoid using reserved words
61+
class = "Python" # Not recommended
62+
63+
# Use meaningful names
64+
a = 10 # Less clear
65+
num_of_students = 10 # More descriptive
66+
```
67+
68+
### Practice Exercises and Examples:
69+
70+
#### Example: Using Variables to Store and Manipulate Configuration Data in a DevOps Context
71+
72+
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.
73+
74+
```python
75+
# Define configuration variables for a web server
76+
server_name = "my_server"
77+
port = 80
78+
is_https_enabled = True
79+
max_connections = 1000
80+
81+
# Print the configuration
82+
print(f"Server Name: {server_name}")
83+
print(f"Port: {port}")
84+
print(f"HTTPS Enabled: {is_https_enabled}")
85+
print(f"Max Connections: {max_connections}")
86+
87+
# Update configuration values
88+
port = 443
89+
is_https_enabled = False
90+
91+
# Print the updated configuration
92+
print(f"Updated Port: {port}")
93+
print(f"Updated HTTPS Enabled: {is_https_enabled}")
94+
```
95+
96+
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.

0 commit comments

Comments
 (0)