Course Topics
Introduction Python Overview Setting Up Python Python Syntax Basics First Steps with Python Comparing Python with Other Languages Basics Variables and Data Types Input and Output Type Conversion Comments and Code Readability Naming Conventions Control Flow Conditional Statements Loops in Python Loop Control Mechanisms Nested Control Structures Data Structures Working with Strings Lists Tuples Sets Dictionaries Comprehensions Iterators and Generators Functions Defining and Calling Functions Function Arguments and Parameters Lambda Functions Return Values Recursion Variable Scope in Functions Modules and Packages Importing Modules Built-in Modules Creating Custom Modules Working with Packages Virtual Environments Managing Packages with pip Object-Oriented Programming Classes and Objects Attributes and Methods Constructors and Initializers Inheritance and Polymorphism Encapsulation and Abstraction Class Methods and Static Methods Using super() and Method Resolution File Handling Reading and Writing Text Files File Modes and File Pointers Using Context Managers (with) Working with CSV Files Handling JSON Data Error Handling Types of Errors and Exceptions Try, Except, Finally Blocks Raising Exceptions Built-in vs Custom Exceptions Exception Handling Best Practices Advanced Python Decorators Advanced Generators Context Managers Functional Programming Tools Coroutines and Async Programming Introduction to Metaclasses Memory Management in Python Useful Libraries Math Module Random Module Date and Time Handling Regular Expressions (re) File and OS Operations (os, sys, shutil) Data Structures Enhancements (collections, itertools) Web APIs (requests) Data Analysis Libraries (NumPy, Pandas) Visualization Tools (matplotlib) Database Access SQLite in Python Connecting to MySQL/PostgreSQL Executing SQL Queries Using ORMs (SQLAlchemy Intro) Transactions and Error Handling Web Development Introduction to Web Frameworks Flask Basics (Routing, Templates) Django Overview Handling Forms and Requests Creating REST APIs Working with JSON and HTTP Methods Testing and Debugging Debugging Techniques Using assert and Logging Writing Unit Tests (unittest) Introduction to pytest Handling and Fixing Common Bugs Automation and Scripting Automating File Operations Web Scraping with BeautifulSoup Automating Excel Tasks (openpyxl) Sending Emails with Python Task Scheduling and Timers System Automation with subprocess

Python Syntax Basics

Python Syntax Basics

Introduction to Syntax

Syntax refers to the set of rules that define how a Python program is written and interpreted. Python’s syntax is known for being clean, readable, and simple. Unlike many other programming languages, Python uses indentation to define code blocks, which makes it easier to read and maintain.

Let’s look at a simple example:

example:

print("Hello, Python!")

This line of code prints text to the screen. There’s no need to use semicolons (;) or curly braces ({}) like in other languages.


Indentation in Python

In Python, indentation is not optional. It defines the structure of your code. For example, blocks of code within an if statement or a loop must be indented.

example:

x = 10

if x > 5:
    print("x is greater than 5")
    print("Still inside the if block")

print("Outside the if block")

If you forget to indent properly, Python will show an IndentationError.

Incorrect example:

if x > 5:
print("This will cause an error")

Code Comments

Comments are used to explain code and are ignored by Python when the program runs.

  • Single-line comment:

python # This is a comment print("Hello") # This prints Hello

  • Multi-line comments (conventionally using triple quotes):

python """ This is a multi-line comment explaining what this code does. """ print("Welcome")


Python Statements

In Python, a statement is a line of code that performs an action.

  • Single-line statement:

python name = "Alice" print(name)

  • Multi-line statement: Use \ to split long lines.

python total = 1 + 2 + 3 + \ 4 + 5 + 6 print(total)

Or use parentheses for cleaner syntax:

total = (
    1 + 2 + 3 +
    4 + 5 + 6
)
print(total)

Variables and Assignment

Variables store data that your program can use and manipulate.

example:

x = 5
name = "Ravi"
is_active = True
  • No need to declare types.
  • Variable names should be descriptive.

You can also assign multiple values in one line:

a, b, c = 1, 2, 3

Or assign the same value to multiple variables:

x = y = z = 10

Data Types (Basic Overview)

Python automatically detects the type of data stored in a variable.

example:

num = 10         # Integer
price = 10.5     # Float
name = "Ravi"    # String
is_valid = True  # Boolean

Use type() to check the type of a variable:

print(type(name))

Input and Output

  • Getting user input:

python name = input("What is your name? ") print("Hello,", name)

  • Output with print:

python print("The total is", 10 + 5)

You can also use f-strings (formatted strings):

age = 25
print(f"You are {age} years old")

Python Identifiers and Keywords

  • Identifiers are names used for variables, functions, classes, etc.

  • Must start with a letter or _

  • Cannot start with a number
  • Can contain letters, numbers, and underscores

example:

my_var = 100
_user_name = "Ravi"
  • Keywords are reserved words in Python. You can’t use them as variable names.

Examples of keywords: if, else, while, for, class, def, True, False, None

To see all keywords:

import keyword
print(keyword.kwlist)

Case Sensitivity

Python is case-sensitive. This means:

name = "Ravi"
Name = "Raj"
print(name)  # Ravi
print(Name)  # Raj

These are treated as two different variables.


Ending Statements

Unlike some other languages, Python does not require semicolons (;) to end a line. Each new line is treated as a new statement.

However, you can write multiple statements on one line using ; (though it's not recommended):

x = 5; y = 10; print(x + y)

Stick to one statement per line for better readability.


Summary

Python’s syntax is designed to be easy to read and beginner-friendly. Understanding the basics — such as indentation, variables, comments, and statements — sets the stage for everything else you'll learn in Python.

Next, we’ll look at how to work with variables and data types in more detail, including strings, numbers, and booleans.