Unit 1: Introduction to Python



Introduction to Python

Python is a high-level, interpreted, and general-purpose programming language.
It is known for its simple syntax, which is close to English, making it easy to learn and use.

Key Features of Python

  • Easy to read and write
  • Interpreted language (no compilation needed)
  • Platform independent (runs on Windows, Linux, Mac)
  • Large standard library
  • Supports Object Oriented Programming
  • Free and open source

Applications of Python

  • Web development (Django, Flask)
  • Data Science & AI
  • Machine Learning
  • Automation & Scripting
  • Game Development
  • Desktop Applications

Basics of Python

Why Python is Beginner-Friendly

  • No need to declare variable type
  • Less code compared to C/C++/Java
  • Easy debugging
  • Interactive mode available

First Python Program

print("Hello World")

Explanation: print() is a built-in function used to display output on the screen.

Setting Up Python Path

Python Path tells the operating system where Python is installed, so that we can run Python from any location.

Steps to Set Python Path (Windows)

  1. Install Python from python.org
  2. Copy installation path (example: C:\Python311\)
  3. Open Environment Variables
  4. Add Python path to System Variables → Path
  5. Restart Command Prompt
  6. Check installation:

python --version

Variables in Python

A variable is a name given to a memory location that stores data.

Example

x = 10 name = "Jay"

Rules for Naming Variables

  • Must start with a letter or underscore
  • Cannot start with a number
  • No spaces allowed
  • Keywords cannot be used

Data Types in Python

Data type tells Python what type of data a variable is storing.

Main Data Types

Data TypeDescriptionExample
intInteger numbersx = 10
floatDecimal numbersy = 10.5
complexComplex numbersz = 3+4j
strText/Stringname = "Python"
boolTrue/FalseisTrue = True
listOrdered collection[1,2,3]
tupleImmutable collection(1,2,3)
setUnordered collection{1,2,3}
dictKey-value pair{"id":1}

id() Function

The id() function returns the memory address of an object.

Example

a = 10 print(id(a))

Explanation: It shows where the variable is stored in memory.

type() Function

The type() function tells the data type of a variable.

Example

x = 5.5 print(type(x))

Output: <class 'float'>

Operators in Python

Operators are used to perform operations on variables.

Types of Operators

1. Arithmetic Operators

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
**Power

Example

a = 10 b = 3 print(a + b)

2. Relational (Comparison) Operators

Used to compare values.

OperatorMeaning
==Equal
!=Not equal
>Greater
<Less

3. Logical Operators

OperatorMeaning
andBoth true
orAny one true
notReverse result

4. Assignment Operators

OperatorExample
=x = 5
+=x += 3

Coding Standards in Python

Coding standards are rules and guidelines to write clean, readable, and professional code.

Python follows PEP-8 (Python Enhancement Proposal).

Important Coding Standards

  1. Use meaningful variable names

    total_marks = 450
  2. Use proper indentation (4 spaces)

  3. Avoid unnecessary spaces

  4. Use comments for explanation

    # This is a comment
  5. Follow lowercase naming with underscore

Exam Point of View – Important Points

  • Python is interpreted and platform independent
  • Variables do not need type declaration
  • id() gives memory address
  • type() returns data type
  • PEP-8 is Python coding standard

Input–Output in Python

Printing on Screen (Output)

Python uses the print() function to display output on the screen.

Basic Syntax

print(value)

Examples

print("Welcome to Python") print(10) print("Sum is", 10 + 20)

Printing Multiple Values

a = 10 b = 20 print(a, b)

Formatted Output

name = "Jay" age = 22 print("Name:", name, "Age:", age)

Reading Data from Keyboard (Input)

Python uses the input() function to take input from the user.

Basic Syntax

variable = input("Message")

Example

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

Important: input() always returns string type, so type conversion is needed.

Type Conversion Example

age = int(input("Enter age: ")) print(age + 5)

Control Structures in Python

Control structures decide which statement will execute and how many times.

if Statement

Used to check a condition.

Syntax

if condition: statement

Example

x = 10 if x > 5: print("x is greater than 5")

if–else Statement

Executes one block if condition is true, otherwise another block.

Syntax

if condition: statement else: statement

Example

x = 4 if x % 2 == 0: print("Even number") else: print("Odd number")

elif Statement

Used to check multiple conditions.

Syntax

if condition1: statement elif condition2: statement else: statement

Example

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

Nested if Statement

An if inside another if.

Example

x = 10 if x > 0: if x % 2 == 0: print("Positive Even")

Iteration Control Structures (Loops)

Loops are used to repeat a block of code.

while Loop

Executes as long as condition is true.

Syntax

while condition: statement

Example

i = 1 while i <= 5: print(i) i += 1

for Loop

Used to iterate over a sequence.

Syntax

for variable in sequence: statement

Example

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

Loop Control Statements

Used to change the normal flow of loops.

break Statement

Used to terminate the loop immediately.

Example

for i in range(1, 10): if i == 5: break print(i)

continue Statement

Skips the current iteration and moves to next.

Example

for i in range(1, 6): if i == 3: continue print(i)

pass Statement

Used as a null statement (does nothing).

Example

for i in range(5): pass

Use Case: Used when syntax is required but logic is not yet written.

Exam Point of View – Key Differences

StatementPurpose
breakExit loop
continueSkip iteration
passDo nothing

Important Exam Notes

  • print() is output function
  • input() takes input as string
  • elif avoids multiple if statements
  • Indentation is compulsory
  • pass avoids error in empty blocks