Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

MODULE 2: Python Basics & Syntax

Python is known for its simplicity and readability. This module builds the foundation required to write clean, error-free Python programs.

1. Variables & Naming Conventions

Brief

A variable is a container used to store data values in memory. Python does not require declaring the data type explicitly.

Detailed

  • Variables are created when you assign a value.
  • Python is dynamically typed, meaning the type can change during runtime.

Rules for Naming Variables

✅ Must start with a letter (a–z, A–Z) or underscore _
❌ Cannot start with a number
❌ Cannot use Python keywords
✅ Case-sensitive (ageAge)
✅ Use meaningful names

Examples

age = 25
name = "Prakash"
salary = 45000.75
_is_active = True

Best Practice (PEP-8)

student_name = "Rahul"   # snake_case

2. Data Types Overview

Brief

Data types define the type of value a variable holds.

Built-in Python Data Types

CategoryData Types
Numericint, float, complex
Textstr
Booleanbool
Sequencelist, tuple
Setset
Mappingdict
NoneNoneType

Examples

x = 10            # int
y = 10.5          # float
name = "Python"  # string
is_valid = True  # boolean
marks = [80, 90, 85]  # list

Check Data Type

print(type(x))   # <class 'int'>

3. Type Casting

Brief

Type casting means converting one data type into another.

Types of Casting

  • int()
  • float()
  • str()
  • bool()

Examples

a = "10"
b = int(a)     # string → int

x = 5
y = float(x)   # int → float

num = 100
text = str(num)  # int → string

Invalid Casting Example

int("Python")  # ValueError

4. Input & Output Functions

Brief

  • input() → Takes input from the user
  • print() → Displays output

Detailed

  • Input is always taken as a string
  • Must type cast if numeric operations are needed

Examples

name = input("Enter your name: ")
print("Welcome", name)

Numeric Input Example

age = int(input("Enter your age: "))
print("Your age is", age)

Formatted Output

print(f"My name is {name} and age is {age}")

5. Comments & Documentation

Brief

Comments are used to explain code and improve readability.

Types of Comments

Single-Line Comment

# This is a single-line comment

Multi-Line Comment

"""
This is a multi-line comment
used for documentation
"""

Docstring (Function Documentation)

def add(a, b):
    """This function returns sum of two numbers"""
    return a + b

6. Python Keywords

Brief

Keywords are reserved words with special meaning in Python.

Common Python Keywords

if, else, elif, for, while, break, continue,
def, return, class, try, except, finally,
True, False, None, import, from, as, pass

Example

if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible")

❌ Invalid:

class = 10   # Error

7. Indentation & Code Structure

Brief

Python uses indentation instead of braces {} to define blocks of code.

Why Indentation Matters

  • Mandatory
  • Improves readability
  • Incorrect indentation causes IndentationError

Correct Indentation

if 10 > 5:
    print("10 is greater")
    print("Inside if block")

Incorrect Indentation

if 10 > 5:
print("Error")  # IndentationError

Best Practice

  • Use 4 spaces for indentation
  • Avoid mixing tabs and spaces

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥