DEV Community

Cover image for This Week's Learning: Python and Git Basics for DevOps (Week 2)
Ali Fareed
Ali Fareed

Posted on

This Week's Learning: Python and Git Basics for DevOps (Week 2)

Python

What is Python?

In the realm of programming languages, Python has emerged as a prominent and versatile choice for developers worldwide. Renowned for its simplicity and readability.

Python was designed with an emphasis on code clarity and developer productivity. Its syntax closely mimics the English Language, making it accessible for beginners while retaining the depth necessary for advanced users.

Why Python?

Python's extensive standard library empowers programmers to perform various tasks without needing external libraries. Additionally, its large ecosystem of third-party packages, facilitated by the Python Package Index (PyPI), enables developers to extend its functionality seamlessly.

What I learned this Week in Python

  • Python vs Shell Scripting?
  • Comments
  • Print
  • Data Types
  • How to change Data Type
  • String Handling Functions
  • Keywords and Variables
  • Functions
  • Modules and Packages
  • Conditional Handling
  • Operators
  • Getting Input from the User
  • Loops
  • Arrays

Python vs Shell Scripting?

Python code tends to be more readable and powerful for complex tasks, while shell scripts are efficient for simple system-level operations.

Comments

Comments are lines of code that are ignored by the interpreter during program execution. They are used to explain code and improve readability.

# This is for a Single line Comment

""" This is Use
    for Multiple
    Lines comment """
Enter fullscreen mode Exit fullscreen mode

Print

The print() function in Python is used to output messages to the screen or other standard output devices.

# Simple Print and comment
print("")
print("First Program to print Hello World!")
print ("Hello", "World!")
Enter fullscreen mode Exit fullscreen mode

Data Types

Data type refers to the type of value a variable has and what type of mathematical, relational, or logical operations can be applied without causing an error. Python supports several data types, including integers, floats, strings, and booleans.

# Simple Print and comment
integer_var = 10         # Integer
float_var = 20.5         # Float
string_var = "Hello"     # String
boolean_var = True       # Boolean

Enter fullscreen mode Exit fullscreen mode

How to change Data Type

#This program takes input from the user and then converts the data type
print("")
print("This is program is used to take INPUT from the user and the convert it to INTEGER then MULTIPLY.")
number1 = int(input("Input number 1 >> "))
number2 = int(input("Input number 2 >> "))
product = number1 * number2
print("The product is ", product)

Enter fullscreen mode Exit fullscreen mode

String Handling Functions

Python provides a variety of string methods to manipulate and analyze strings.

text = "Hello, World!"

# Convert to uppercase
print(text.upper())  # Output: HELLO, WORLD!

# Find a substring
print(text.find("World"))  # Output: 7

# Replace a substring
print(text.replace("World", "Python"))  # Output: Hello, Python!
Enter fullscreen mode Exit fullscreen mode

Keywords and Variables

Python has reserved keywords that cannot be used as variable names (e.g., if, else, while). Variables are used to store data.

# Valid variable names
my_variable = 5
another_var = "Python"

# Invalid variable names (keywords)
# if = 10  # SyntaxError
# else = 20  # SyntaxError
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are defined using the def keyword and can be used to encapsulate reusable code.

def greet(name):
    return f"Hello, {name}!"

# calling function
print(greet("Alice"))  # Output: Hello, Alice!  
Enter fullscreen mode Exit fullscreen mode

Modules and Packages

Modules are Python files with functions and variables. Packages are collections of modules.

Example of a Module (mymodule.py):

def add(a, b):
    return a + b    
Enter fullscreen mode Exit fullscreen mode

Using the Module:

import mymodule

result = mymodule.add(5, 3)
print(result)  # Output: 8  
Enter fullscreen mode Exit fullscreen mode

Conditional Handling

Conditional statements control the flow of execution based on conditions.

IF Statement

x = 10
if x > 5:
    print("x is greater than 5")
Enter fullscreen mode Exit fullscreen mode

ELSE IF Statement

x = 10
if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is greater than 5 but less than or equal to 15")
Enter fullscreen mode Exit fullscreen mode

ELSE Statement

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is 5 or less")
Enter fullscreen mode Exit fullscreen mode

Operators

Python supports various operators including arithmetic, comparison, and logical operators.

# Arithmetic Operators
a = 10
b = 5
print(a + b)  # Output: 15

# Comparison Operators
print(a > b)  # Output: True

# Logical Operators
print(a > b and b > 0)  # Output: True

Enter fullscreen mode Exit fullscreen mode

Getting Input from the User

The Python input function is used to derive input from the user. The function reads the line of input, and converts the input into a string, and returns the output.

# Getting input from the user 
print("")
print("Program to take input from user's terminal")
name = input("Enter your name: ") 

# user entered the name 
print("Hello", name) 

Enter fullscreen mode Exit fullscreen mode

Loops

Loops allow executing a block of code multiple times.

For Loop

for i in range(5):
    print(i)  # Output: 0 1 2 3 4 
Enter fullscreen mode Exit fullscreen mode

While Loop

count = 0
while count < 5:
print(count)
count += 1  # Output: 0 1 2 3 4 
Enter fullscreen mode Exit fullscreen mode

Lists and Tuples

Lists and tuples are used to store collections of items. Lists are mutable, while tuples are immutable.

Lists

my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list)  # Output: [1, 2, 3, 4, 5] 
Enter fullscreen mode Exit fullscreen mode

Tuples

my_tuple = (1, 2, 3, 4)
print(my_tuple)  # Output: (1, 2, 3, 4) 
Enter fullscreen mode Exit fullscreen mode

Git

What is Git?

Git is a distributed version control system that allows multiple people to work on a project simultaneously without stepping on each other's toes.

Why Git?

It helps manage changes to source code over time, making it easier to track and collaborate on code development. Git is widely used in software development due to its flexibility, efficiency, and robustness.

Here’s a breakdown of some of the essential Git commands along with their definitions:

  • git init
  • git clone
  • git status
  • git add
  • git commit
  • git push
  • git pull
  • git fetch
  • git merge
  • git branch
  • git checkout
  • git branch
  • git reset
  • git diff
  • git stash

git init
Initializes a new Git repository in the current directory. This command creates a new .git directory, which contains all the necessary metadata and objects for version control.

git init

git clone
Creates a copy of an existing Git repository. This command downloads the repository and its history from a remote server to your local machine.

git clone <repository-url>

git clone https://github.com/user/repo.git

git status
Shows the current status of the working directory and staging area. It displays which changes have been staged, which are still unstaged, and which files are not being tracked by Git.

git status

git add
Stages changes (new files, modifications, deletions) to be included in the next commit. It tells Git to track these changes.

git add <file>
git add index.html

To add all changes:
git add .
Enter fullscreen mode Exit fullscreen mode

git commit
Records the staged changes to the repository’s history. Each commit includes a commit message describing the changes.

git commit -m "Commit message"

git push
Uploads local repository content to a remote repository. It is used to share changes with others and update the remote repository with your commits.

git push <remote> <branch>
git push origin main
Enter fullscreen mode Exit fullscreen mode

git pull
Fetches and integrates changes from a remote repository into the local repository. This command is used to update your local branch with changes from the remote branch.

git pull <remote> <branch>
git pull origin main
Enter fullscreen mode Exit fullscreen mode

git fetch
Downloads objects and references from a remote repository but does not merge them into the current branch. It is used to see if there are new changes in the remote repository without affecting your local work.

git fetch <remote>
git fetch origin
Enter fullscreen mode Exit fullscreen mode

git merge
Integrates changes from one branch into the current branch. This command is used to combine changes from different branches.

git merge <branch>
git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

git branch
Lists, creates, or deletes branches. Branches are used to develop features or fix bugs independently of the main codebase.

List branches:
git branch

Create a new branch:
git branch <branch-name>

Delete a branch:
git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

git checkout
Switches between branches or restores working directory files. It is used to move your working directory to a different branch or commit.

Switch branches:
git checkout <branch-name>

Restore files:
git checkout -- <file>

Enter fullscreen mode Exit fullscreen mode

git log
Displays the commit history for the current branch. It shows a list of commits, including commit messages, authors, and timestamps.

git reset

Soft reset (keeps changes in the working directory):
git reset --soft <commit>

Hard reset (discards changes):
git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

git diff
Shows differences between the working directory and the index (staging area) or between commits. It helps in reviewing changes before committing.

git diff
Enter fullscreen mode Exit fullscreen mode

git stash
Temporarily saves changes that are not yet ready to be committed. It’s useful when you need to switch branches without committing the current changes.

git stash

To apply stashed changes:
git stash apply
Enter fullscreen mode Exit fullscreen mode

Resources

Python

Python for DevOps Course by Abhishek Veeramalla

Git

Git and GitHub Master Class By Shubham Londhe

Git and GitHub In One Video By MPrashant

Top comments (0)