Introduction to Python

Start your Python for Web Development journey with this complete beginner friendly Introduction to Python Chapter 1. Learn what Python is, how it works in backend development, how to install and run Python on Linux, and the fundamentals of syntax, variables, data types, strings, input, type conversion, operators, and Boolean values.

You’ll also explore Flask, Django, and FastAPI, understand how Python connects with databases and web forms, and build practical beginner projects such as a registration program, calculator, login check, and student registration system.

By the end of this chapter, you’ll have a strong foundation for building Python web applications.

Introduction to Python

Here are the official Python installation links from Python.org:

Introduction to Python with Details

1. What Is Python?

Python is a high-level, general-purpose programming language. It is used for:

  • Web development
  • Software development
  • Automation
  • Data analysis
  • Artificial intelligence
  • Machine learning
  • Cybersecurity
  • Scripting
  • API development

For web development, Python is primarily used on the backend.

Frontend vs Backend

A typical website has two major sides:

Frontend

  • HTML
  • CSS
  • JavaScript
  • Runs mainly in the user’s browser

Backend

  • Python
  • PHP
  • Node.js
  • Java
  • C#
  • Handles application logic, databases, authentication, APIs, etc.

A simplified web application looks like:

User

  ↓

Browser

  ↓

HTTP Request

  ↓

Python Backend

  ↓

Business Logic

  ↓

Database

  ↓

Python Backend

  ↓

HTTP Response

  ↓

Browser

For example, when a user logs into a website:

Username + Password

        ↓

Python Backend

        ↓

Check Database

        ↓

Verify Password

        ↓

Create Session

        ↓

Dashboard

2. Why Python for Web Development?

Python has several frameworks and libraries that make web development easier.

Flask

Flask is a lightweight web framework.

Example:

from flask import Flask

app = Flask(__name__)

@app.route(“/”)

def home():

    return “Hello World!”

app.run()

Flask is useful for learning the fundamentals of web applications.

Django

Django is a larger web framework that provides many features out of the box.

It includes:

  • Authentication
  • Admin panel
  • Database ORM
  • Forms
  • URL routing
  • Security features
  • Templates
  • Middleware

FastAPI

FastAPI is designed primarily for building modern APIs.

It is particularly useful when building:

Mobile App

     ↓

    API

     ↓

FastAPI Backend

     ↓

Database

3. Installing Python

Since you’re working with Linux, first check Python:

python3 –version

Example:

Python 3.13.5

Also check pip:

pip3 –version

And locate Python:

which python3

Example:

/usr/bin/python3

4. Python Interactive Shell

You can run Python directly from the terminal.

Type:

python3

You’ll see something similar to:

Python 3.x.x

>>>

Now try:

print(“Hello Python”)

Output:

Hello Python

Try mathematics:

10 + 20

Output:

30

Try:

5 * 10

Output:

50

Exit Python:

exit()

5. Creating Your First Python File

Create a project directory:

mkdir python-web-course

Move into it:

cd python-web-course

Create your first Python file:

nano hello.py

Add:

print(“Hello, Python!”)

Save the file.

Run:

python3 hello.py

Output:

Hello, Python!

6. Python Syntax

Python syntax is designed to be readable.

Example:

name = “Saqib”

print(name)

Notice that Python doesn’t require:

;

at the end of every statement.

For example, this is unnecessary:

name = “Saqib”;

Instead use:

name = “Saqib”

7. Indentation

Indentation is extremely important in Python.

Example:

age = 20

if age >= 18:

    print(“You are an adult”)

The indentation tells Python that the print() statement belongs to the if block.

Incorrect:

if age >= 18:

print(“You are an adult”)

Python will produce an indentation error.

Usually, Python code uses 4 spaces for indentation.

8. Comments

Comments help explain your code.

Single-line comment:

# This is a comment

print(“Hello”)

Example:

# Store user’s name

name = “Saqib”

# Display user’s name

print(name)

Comments are especially useful in larger web projects.

9. Variables

A variable stores data.

Example:

name = “Saqib”

age = 25

city = “Pakistan”

You can use the variables later:

print(name)

print(age)

print(city)

Output:

Saqib

25

Pakistan

10. Variable Naming Rules

Valid:

username = “Saqib”

user_name = “Saqib”

age2 = 25

Invalid:

2age = 25

You cannot start a variable name with a number.

Also avoid spaces:

user name = “Saqib”

Use:

user_name = “Saqib”

Python commonly uses snake_case:

first_name = “Saqib”

email_address = “example@gmail.com”

user_password = “123456”

11. Python Data Types

Python has several important data types.

String

name = “Saqib”

Type:

str

Integer

age = 25

Type:

int

Float

price = 99.50

Type:

float

Boolean

is_logged_in = True

Type:

bool

Check types:

print(type(name))

print(type(age))

print(type(price))

print(type(is_logged_in))

12. Strings

Strings contain text.

name = “Saqib”

You can use single quotes:

name = ‘Saqib’

Or double quotes:

name = “Saqib”

Both are valid.

String concatenation

first_name = “Muhammad”

last_name = “Saqib”

full_name = first_name + ” ” + last_name

print(full_name)

Output:

Muhammad Saqib

13. f-Strings

f-strings are extremely useful in web development.

name = “Saqib”

age = 25

message = f”My name is {name} and I am {age} years old.”

print(message)

Output:

My name is Saqib and I am 25 years old.

You’ll frequently use this technique when generating dynamic content.

14. User Input

Python can receive information from users.

name = input(“Enter your name: “)

print(f”Hello {name}”)

Example:

Enter your name: Saqib

Hello Saqib

15. Important: Input Is a String

Consider:

age = input(“Enter your age: “)

Even if the user enters:

25

Python initially treats it as a string.

Check:

print(type(age))

You’ll get:

<class ‘str’>

To convert it:

age = int(input(“Enter your age: “))

Now:

print(type(age))

returns:

<class ‘int’>

16. Python Type Conversion

Python provides functions for converting data.

String → Integer

age = int(“25”)

String → Float

price = float(“99.99”)

Integer → String

age = 25

age_text = str(age)

Important example

price = input(“Enter price: “)

quantity = input(“Enter quantity: “)

total = float(price) * int(quantity)

print(total)

This type of conversion becomes important when processing data submitted through web forms.

17. Python Basic Operators

Addition

10 + 5

Result:

15

Subtraction

10 – 5

Multiplication

10 * 5

Division

10 / 5

Modulus

10 % 3

Result:

1

Exponent

2 ** 3

Result:

8

18. Comparison Operators

Python supports:

==

!=

>

<

>=

<=

Example:

age = 20

print(age >= 18)

Output:

True

Example:

password = “12345”

print(password == “12345”)

Output:

True

This kind of comparison is commonly used in authentication logic.

19. Boolean Values

Python has:

True

False

Example:

is_admin = True

is_logged_in = False

These become very useful when working with:

  • Authentication
  • User permissions
  • Admin systems
  • Access control
  • Application settings

20. Your First Practical Program

Let’s create a small registration program.

print(“===== Registration =====”)

name = input(“Enter your name: “)

email = input(“Enter your email: “)

age = int(input(“Enter your age: “))

print()

print(“Registration Successful!”)

print(f”Name: {name}”)

print(f”Email: {email}”)

print(f”Age: {age}”)

Example:

===== Registration =====

Enter your name: Saqib

Enter your email: saqib@example.com

Enter your age: 25

Registration Successful!

Name: Saqib

Email: saqib@example.com

Age: 25

Later, we’ll convert this basic concept into an actual HTML form + Python backend + database.

21. Python’s Role in a Real Website

Eventually, your architecture will look something like this:

               WEBSITE

                   │

        ┌──────────┴──────────┐

        │                     │

     Frontend               Backend

        │                     │

 HTML + CSS              Python

 JavaScript                  │

        │             Flask / Django

        │                     │

        └──────────┬──────────┘

                   │

                Database

                   │

          MySQL / PostgreSQL

For example, a registration page might work like:

HTML Form

    ↓

POST Request

    ↓

Python

    ↓

Validate Data

    ↓

Hash Password

    ↓

Database

    ↓

Create User

    ↓

Return Response

This is where the Python fundamentals you’re learning become useful.

22. Practice Exercises

Exercise 1 — Personal Information

Create a program that asks:

Name

Age

City

Country

Email

Then display all information.

Exercise 2 — Calculator

Ask the user for two numbers and display:

Addition

Subtraction

Multiplication

Division

Exercise 3 — Age Calculator

Ask for the user’s birth year:

Enter your birth year:

Calculate their approximate age.

Exercise 4 — Login Check

Create:

username = “admin”

password = “12345”

Ask the user for username and password and determine whether the credentials match.

Note: This is only a Python exercise; never store real passwords in plaintext in an actual application.

23. Mini Project — Student Registration

Create a program that asks:

Student Name:

Father Name:

Age:

Email:

Phone:

Course:

Then display:

================================

       STUDENT REGISTRATION

================================

Name:

Father Name:

Age:

Email:

Phone:

Course:

================================

Registration Completed

================================

This project introduces concepts you’ll later use in real web forms.

What You Should Know Before Chapter 2

By the end of Chapter 1, you should understand:

  • What Python is
  • Why Python is useful for web development
  • Frontend vs backend
  • Python installation
  • Running Python
  • Python files
  • Syntax
  • Indentation
  • Comments
  • Variables
  • Strings
  • Integers
  • Floats
  • Booleans
  • Input
  • Output
  • Type conversion
  • Operators
  • Basic programs

Next: Chapter 2 — Python Data Types in Depth

We’ll go much deeper into:

Strings → Lists → Tuples → Sets → Dictionaries → Indexing → Slicing → Methods → Nested data → Practical exercises → Web-development examples → Mini project.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top