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
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)
- Install Python from python.org
- Copy installation path (example:
C:\Python311\) - Open Environment Variables
- Add Python path to System Variables → Path
- Restart Command Prompt
- Check installation:
Variables in Python
A variable is a name given to a memory location that stores data.
Example
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 Type | Description | Example |
|---|---|---|
| int | Integer numbers | x = 10 |
| float | Decimal numbers | y = 10.5 |
| complex | Complex numbers | z = 3+4j |
| str | Text/String | name = "Python" |
| bool | True/False | isTrue = True |
| list | Ordered collection | [1,2,3] |
| tuple | Immutable collection | (1,2,3) |
| set | Unordered collection | {1,2,3} |
| dict | Key-value pair | {"id":1} |
id() Function
The id() function returns the memory address of an object.
Example
Explanation: It shows where the variable is stored in memory.
type() Function
The type() function tells the data type of a variable.
Example
Output: <class 'float'>
Operators in Python
Operators are used to perform operations on variables.
Types of Operators
1. Arithmetic Operators
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
| ** | Power |
Example
2. Relational (Comparison) Operators
Used to compare values.
| Operator | Meaning |
|---|---|
| == | Equal |
| != | Not equal |
| > | Greater |
| < | Less |
3. Logical Operators
| Operator | Meaning |
|---|---|
| and | Both true |
| or | Any one true |
| not | Reverse result |
4. Assignment Operators
| Operator | Example |
|---|---|
| = | 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
-
Use meaningful variable names
-
Use proper indentation (4 spaces)
-
Avoid unnecessary spaces
-
Use comments for explanation
-
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 addresstype()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
Examples
Printing Multiple Values
Formatted Output
Reading Data from Keyboard (Input)
Python uses the input() function to take input from the user.
Basic Syntax
Example
Important: input() always returns string type, so type conversion is needed.
Type Conversion Example
Control Structures in Python
Control structures decide which statement will execute and how many times.
if Statement
Used to check a condition.
Syntax
Example
if–else Statement
Executes one block if condition is true, otherwise another block.
Syntax
Example
elif Statement
Used to check multiple conditions.
Syntax
Example
Nested if Statement
An if inside another if.
Example
Iteration Control Structures (Loops)
Loops are used to repeat a block of code.
while Loop
Executes as long as condition is true.
Syntax
Example
for Loop
Used to iterate over a sequence.
Syntax
Example
Loop Control Statements
Used to change the normal flow of loops.
break Statement
Used to terminate the loop immediately.
Example
continue Statement
Skips the current iteration and moves to next.
Example
pass Statement
Used as a null statement (does nothing).
Example
Use Case: Used when syntax is required but logic is not yet written.
Exam Point of View – Key Differences
| Statement | Purpose |
|---|---|
| break | Exit loop |
| continue | Skip iteration |
| pass | Do nothing |
Important Exam Notes
print()is output functioninput()takes input as stringelifavoids multiple if statements- Indentation is compulsory
passavoids error in empty blocks