This content originally appeared on DEV Community and was authored by Ali Fareed
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
- 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 """
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!")
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
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)
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!
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
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!
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
Using the Module:
import mymodule
result = mymodule.add(5, 3)
print(result) # Output: 8
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")
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")
ELSE Statement
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
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
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)
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
While Loop
count = 0
while count < 5:
print(count)
count += 1 # Output: 0 1 2 3 4
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]
Tuples
my_tuple = (1, 2, 3, 4)
print(my_tuple) # Output: (1, 2, 3, 4)
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 .
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
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
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
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
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>
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>
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>
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
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
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
This content originally appeared on DEV Community and was authored by Ali Fareed
Ali Fareed | Sciencx (2024-07-28T10:34:25+00:00) This Week’s Learning: Python and Git Basics for DevOps (Week 2). Retrieved from https://www.scien.cx/2024/07/28/this-weeks-learning-python-and-git-basics-for-devops-week-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.