Basic programming (Python, Bash scripting)

Basic Programming: Python and Bash Scripting

Basic Programming -  Python and Bash Scripting

Basic programming is an essential skill for beginners, students, developers, system administrators, and cybersecurity professionals. Among all programming languages and scripting tools, Python and Bash scripting are the most widely used due to their simplicity, power, and versatility. Python is a high-level, general-purpose programming language used in web development, data analytics, automation, machine learning, cybersecurity, and artificial intelligence. Bash, on the other hand, is a command-line scripting language used primarily in Linux and Unix environments for task automation, file management, system operations, and server administration.

This document provides a detailed explanation of the basics of Python programming and Bash scripting. It covers syntax, keywords, data types, control flow statements, loops, functions, file handling, automation scripts, and practical examples designed for learners. This is a highly comprehensive, SEO-friendly learning material covering essential keywords such as Python basics, Bash scripting basics, Python code examples, Bash automation, shell scripting tutorials, Python beginner guide, and more.

Introduction to Python Programming

Python is a popular, powerful, and easy-to-learn programming language. It is widely used for:

  • Automation
  • Web development
  • Machine learning
  • Data analysis
  • Cybersecurity scripting
  • IoT and embedded systems

Its simple syntax makes it ideal for beginners, while its extensive libraries and frameworks make it suitable for advanced programming. Python's readability, flexibility, and community support have made it one of the most in-demand languages today.

Python Basic Syntax

Python syntax refers to how the language is written. A few basic rules include:

  • Python uses indentation (spaces or tabs) to define code blocks.
  • It is case-sensitive.
  • Statements usually end without semicolons.
  • Comments start with a # symbol.

Hello World in Python


print("Hello, World!")

Python Comments


# This is a single-line comment

"""
This is a 
multi-line comment
in Python
"""

Python Variables and Data Types

Python supports multiple data types including:

  • int
  • float
  • str
  • bool
  • list
  • tuple
  • dict
  • set

Example: Creating Variables


name = "Alice"
age = 25
height = 5.6
is_student = True

print(name, age, height, is_student)

Python Lists


fruits = ["apple", "banana", "orange"]
print(fruits[0])  # Access first element

Python Operators

Python supports several operator types:

  • Arithmetic (+, -, *, /)
  • Comparison (==, !=, >, <)
  • Logical (and, or, not)
  • Assignment (=, +=, -=)

Arithmetic Example


a = 10
b = 5
print(a + b)
print(a - b)

Python Conditional Statements

Conditional statements help make decisions in code.

Example: if-else


age = 18

if age >= 18:
    print("You are eligible to vote.")
else:
    print("Not eligible.")

Example: elif


score = 85

if score >= 90:
    print("Grade A")
elif score >= 75:
    print("Grade B")
else:
    print("Grade C")

Python Loops

For Loop Example


for i in range(1, 6):
    print("Number:", i)

While Loop Example


count = 1
while count <= 5:
    print("Count:", count)
    count += 1

Python Functions

Functions help reuse code and improve program structure.

Simple Function Example


def greet(name):
    print("Hello,", name)

greet("Alice")

Python File Handling

Writing to a File


file = open("data.txt", "w")
file.write("Hello from Python!")
file.close()

Reading from a File


file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

Python Practical Mini Project

Basic Calculator Program


def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))

print("Addition:", add(n1, n2))
print("Subtraction:", subtract(n1, n2))

Introduction to Bash Scripting

Bash (Bourne Again SHell) is a command-line interpreter used in Linux and Unix systems. Bash scripting is widely used for automation, system administration, DevOps tasks, server management, and cybersecurity operations. Bash makes it easy to automate repetitive tasks such as file handling, user management, backups, and log monitoring.

Learning Bash scripting is essential for Linux users, ethical hackers, cloud engineers, and system administrators. Key SEO keywords include Bash scripting tutorial, Linux shell scripting for beginners, Bash automation, Linux commands, and shell script examples.

Bash Syntax Basics

Bash scripts follow some basic rules:

  • Every script starts with a shebang line: #!/bin/bash
  • Scripts must be given execute permission using chmod +x
  • Variables do not use data types
  • Comments start with #

Hello World in Bash


#!/bin/bash
echo "Hello, World!"

Running a Bash Script

To run a script:


chmod +x script.sh
./script.sh

Bash Variables

Example of Variables


name="Alice"
echo "Hello $name"

Bash Input and Output

Reading User Input


echo "Enter your name:"
read username
echo "Welcome, $username!"

Bash Conditional Statements

If-Else Example


#!/bin/bash

age=18

if [ $age -ge 18 ]
then
    echo "Adult"
else
    echo "Minor"
fi

Bash Loops

For Loop


for i in {1..5}
do
    echo "Number: $i"
done

While Loop


count=1

while [ $count -le 5 ]
do
    echo "Count: $count"
    count=$((count + 1))
done

Bash Functions


greet() {
    echo "Hello, $1"
}

greet "Alice"

Bash File Handling

Create and Write File


echo "This is a test file" > file.txt

Read File


cat file.txt

Bash Automation Mini Project

Backup Script


#!/bin/bash

src="/home/user/documents"
dest="/backup/documents_$(date +%F)"

mkdir -p "$dest"
cp -r "$src" "$dest"

echo "Backup completed successfully!"

Python vs Bash: When to Use What?

Python Bash
Best for automation, data processing, app development Best for system-level operations
Cross-platform Mainly Linux-based
Large library ecosystem Lightweight and fast

Both Python and Bash scripting are foundational skills for beginners and professionals. Python is perfect for long, complex automation and application development, while Bash is ideal for system administration and shell-level automation. Learning both gives you strong control over programming and Linux environments. Mastering Python and Bash opens opportunities in DevOps, cybersecurity, cloud computing, automation, and software engineering.

logo

General

Beginner 5 Hours
Basic Programming: Python and Bash Scripting

Basic Programming -  Python and Bash Scripting

Basic programming is an essential skill for beginners, students, developers, system administrators, and cybersecurity professionals. Among all programming languages and scripting tools, Python and Bash scripting are the most widely used due to their simplicity, power, and versatility. Python is a high-level, general-purpose programming language used in web development, data analytics, automation, machine learning, cybersecurity, and artificial intelligence. Bash, on the other hand, is a command-line scripting language used primarily in Linux and Unix environments for task automation, file management, system operations, and server administration.

This document provides a detailed explanation of the basics of Python programming and Bash scripting. It covers syntax, keywords, data types, control flow statements, loops, functions, file handling, automation scripts, and practical examples designed for learners. This is a highly comprehensive, SEO-friendly learning material covering essential keywords such as Python basics, Bash scripting basics, Python code examples, Bash automation, shell scripting tutorials, Python beginner guide, and more.

Introduction to Python Programming

Python is a popular, powerful, and easy-to-learn programming language. It is widely used for:

  • Automation
  • Web development
  • Machine learning
  • Data analysis
  • Cybersecurity scripting
  • IoT and embedded systems

Its simple syntax makes it ideal for beginners, while its extensive libraries and frameworks make it suitable for advanced programming. Python's readability, flexibility, and community support have made it one of the most in-demand languages today.

Python Basic Syntax

Python syntax refers to how the language is written. A few basic rules include:

  • Python uses indentation (spaces or tabs) to define code blocks.
  • It is case-sensitive.
  • Statements usually end without semicolons.
  • Comments start with a # symbol.

Hello World in Python

print("Hello, World!")

Python Comments

# This is a single-line comment """ This is a multi-line comment in Python """

Python Variables and Data Types

Python supports multiple data types including:

  • int
  • float
  • str
  • bool
  • list
  • tuple
  • dict
  • set

Example: Creating Variables

name = "Alice" age = 25 height = 5.6 is_student = True print(name, age, height, is_student)

Python Lists

fruits = ["apple", "banana", "orange"] print(fruits[0]) # Access first element

Python Operators

Python supports several operator types:

  • Arithmetic (+, -, *, /)
  • Comparison (==, !=, >, <)
  • Logical (and, or, not)
  • Assignment (=, +=, -=)

Arithmetic Example

a = 10 b = 5 print(a + b) print(a - b)

Python Conditional Statements

Conditional statements help make decisions in code.

Example: if-else

age = 18 if age >= 18: print("You are eligible to vote.") else: print("Not eligible.")

Example: elif

score = 85 if score >= 90: print("Grade A") elif score >= 75: print("Grade B") else: print("Grade C")

Python Loops

For Loop Example

for i in range(1, 6): print("Number:", i)

While Loop Example

count = 1 while count <= 5: print("Count:", count) count += 1

Python Functions

Functions help reuse code and improve program structure.

Simple Function Example

def greet(name): print("Hello,", name) greet("Alice")

Python File Handling

Writing to a File

file = open("data.txt", "w") file.write("Hello from Python!") file.close()

Reading from a File

file = open("data.txt", "r") content = file.read() print(content) file.close()

Python Practical Mini Project

Basic Calculator Program

def add(a, b): return a + b def subtract(a, b): return a - b n1 = float(input("Enter first number: ")) n2 = float(input("Enter second number: ")) print("Addition:", add(n1, n2)) print("Subtraction:", subtract(n1, n2))

Introduction to Bash Scripting

Bash (Bourne Again SHell) is a command-line interpreter used in Linux and Unix systems. Bash scripting is widely used for automation, system administration, DevOps tasks, server management, and cybersecurity operations. Bash makes it easy to automate repetitive tasks such as file handling, user management, backups, and log monitoring.

Learning Bash scripting is essential for Linux users, ethical hackers, cloud engineers, and system administrators. Key SEO keywords include Bash scripting tutorial, Linux shell scripting for beginners, Bash automation, Linux commands, and shell script examples.

Bash Syntax Basics

Bash scripts follow some basic rules:

  • Every script starts with a shebang line: #!/bin/bash
  • Scripts must be given execute permission using chmod +x
  • Variables do not use data types
  • Comments start with #

Hello World in Bash

#!/bin/bash echo "Hello, World!"

Running a Bash Script

To run a script:

chmod +x script.sh ./script.sh

Bash Variables

Example of Variables

name="Alice" echo "Hello $name"

Bash Input and Output

Reading User Input

echo "Enter your name:" read username echo "Welcome, $username!"

Bash Conditional Statements

If-Else Example

#!/bin/bash age=18 if [ $age -ge 18 ] then echo "Adult" else echo "Minor" fi

Bash Loops

For Loop

for i in {1..5} do echo "Number: $i" done

While Loop

count=1 while [ $count -le 5 ] do echo "Count: $count" count=$((count + 1)) done

Bash Functions

greet() { echo "Hello, $1" } greet "Alice"

Bash File Handling

Create and Write File

echo "This is a test file" > file.txt

Read File

cat file.txt

Bash Automation Mini Project

Backup Script

#!/bin/bash src="/home/user/documents" dest="/backup/documents_$(date +%F)" mkdir -p "$dest" cp -r "$src" "$dest" echo "Backup completed successfully!"

Python vs Bash: When to Use What?

Python Bash
Best for automation, data processing, app development Best for system-level operations
Cross-platform Mainly Linux-based
Large library ecosystem Lightweight and fast

Both Python and Bash scripting are foundational skills for beginners and professionals. Python is perfect for long, complex automation and application development, while Bash is ideal for system administration and shell-level automation. Learning both gives you strong control over programming and Linux environments. Mastering Python and Bash opens opportunities in DevOps, cybersecurity, cloud computing, automation, and software engineering.

Related Tutorials

Frequently Asked Questions for General

line

Copyrights © 2024 letsupdateskills All rights reserved