⬅ Previous Next ➡

Introduction to Python

Overview of Python Language
- Python is a high-level, interpreted, general-purpose programming language. - Used for web apps, automation, scripting, data science, AI/ML, etc. - Focuses on simplicity and readability.
Features of Python
- Simple & readable syntax. - Interpreted (runs line-by-line). - Dynamically typed. - Supports OOP, procedural, functional. - Large standard library + pip packages. - Cross-platform support.
History of Python
- Developed by Guido van Rossum. - First released in 1991. - Name taken from "Monty Python". - Designed to make coding easier and cleaner.
Python Versions
- Python 2.x is discontinued (not recommended). - Python 3.x is the current standard. - Use Python 3 for exams, projects, and libraries.
Python Installation
Windows: 1) Download Python from python.org 2) Tick "Add Python to PATH" 3) Verify: python --version pip --version Linux (Ubuntu/Debian): sudo apt update sudo apt install python3 python3-pip python3 --version
Python Syntax Basics
- Python uses indentation instead of {}. - Blocks start with colon (:). - Newline ends a statement (no semicolon needed). Example: x = 10 y = 20 print(x + y)
Python Keywords (Reserved Words)
- Keywords cannot be used as identifiers. Examples: if, else, elif for, while, break, continue def, return, class import, from, as try, except, finally True, False, None
Identifiers (Naming Rules)
Rules: - Start with letter or underscore (_) - Cannot start with digit - Can contain letters, digits, underscore - Cannot be a keyword - Case-sensitive Valid: total _sum marks1 Invalid: 1total class my-name
Comments in Python
Single-line: # This is a comment Multi-line / Docstring: """ This is a multi-line comment/docstring """ Example: # Adding two numbers a = 5 b = 3 print(a + b)
Indentation in Python
- Indentation defines blocks (if, loop, function, class). - Standard: 4 spaces per level. - Wrong indentation gives IndentationError. Correct: if 10 > 5: print("Inside if") print("Still inside") print("Outside if") Wrong: if 10 > 5: print("Error")
Python Interpreter (REPL and Script)
REPL (Interactive Mode): - Runs command instantly Example: >>> 2 + 3 5 Script Mode: 1) Save file: test.py 2) Run: python test.py (or python3 test.py)
First Python Program (Hello World)
Program 1: print("Hello, World!") print("Welcome to Python") Program 2 (Variables): name = "Sourav" age = 25 print("Name:", name) print("Age:", age) Program 3 (Input/Output): user_name = input("Enter your name: ") print("Hello", user_name)
Overview of Python Language
  • Python is a high-level, interpreted, general-purpose programming language.
  • Used in web development, automation, data science, AI/ML, scripting, etc.
  • Famous for simple syntax and readability.
Features of Python
  • Easy to learn and write (clean syntax).
  • Interpreted language (runs line-by-line).
  • Dynamically typed (no type declaration required).
  • Supports OOP + procedural + functional programming.
  • Large standard library + huge package ecosystem (pip).
  • Cross-platform (Windows, Linux, macOS).
History of Python
  • Created by Guido van Rossum.
  • First released in 1991.
  • Name inspired by the comedy show "Monty Python".
  • Built to emphasize readability and rapid development.
Python Versions (2.x vs 3.x)
  • Python 2.x is outdated and discontinued (not recommended).
  • Python 3.x is the current standard for learning and development.
  • Most libraries and tools work best with Python 3.
Python Installation (Windows & Linux)
Windows:
  • Download from python.org
  • Tick "Add Python to PATH" during installation
  • Verify:
    python --version
    pip --version
Linux (Ubuntu/Debian):
sudo apt update
sudo apt install python3 python3-pip
python3 --version
Python Syntax Basics
  • Python uses indentation instead of braces {}.
  • Blocks start with a colon : and then indented lines.
  • Newline ends a statement (semicolon not required).
x = 10
y = 20
print(x + y)
Python Keywords (Reserved Words)
  • Keywords are reserved words and cannot be used as variable names.
Examples:
if, else, elif
for, while, break, continue
def, return, class
import, from, as
try, except, finally
True, False, None
Identifiers (Naming Rules)
  • Identifier = name of variable/function/class.
  • Must start with a letter or underscore (_).
  • Cannot start with a digit and cannot be a keyword.
  • Case-sensitive (name and Name are different).
Valid:
total_marks
_student
marks1

Invalid:
1total
class
my-name
Comments in Python
  • Comments explain code and are not executed.
  • Single-line uses #
  • Multi-line uses triple quotes (also used as docstrings).
# single-line comment

""" multi-line comment / docstring """
Indentation in Python (Most Important)
  • Indentation defines code blocks (if, loop, function, class).
  • Recommended: 4 spaces per level.
  • Wrong indentation causes IndentationError.
if 10 > 5:
    print("Inside block")
    print("Still inside")

print("Outside block")
Python Interpreter (REPL and Script Mode)
  • Python interpreter executes code line-by-line.
  • REPL (Interactive): run commands instantly.
  • Script Mode: run a .py file.
REPL Example:
>>> print("Hello")
Hello
Run Script:
python file.py
# or
python3 file.py
First Python Program (Hello World)
Hello World:
print("Hello, World!")
print("Welcome to Python")
Input Example:
name = input("Enter your name: ")
print("Hello", name)
⬅ Previous Next ➡