1. What is Python?
Python is a high-level, general-purpose programming
language created by Guido van Rossum and first released
in 1991. Python is designed with a strong focus on
readability and simplicity.
Python is widely used in web development, data science,
artificial intelligence, machine learning, automation,
scripting, scientific computing and software development.
Why Learn Python?
- Easy and readable syntax.
- Large collection of libraries and frameworks.
- Suitable for beginners and professionals.
- Used in Artificial Intelligence and Machine Learning.
- Supports object-oriented programming.
- Works on Windows, Linux and macOS.
Important:
Python is dynamically typed, which means you do not
normally need to declare the data type of a variable
explicitly.
2. Features of Python
Python provides several features that make it one of
the most commonly used programming languages.
1. Simple Syntax
Python syntax is relatively simple and close to
natural language.
2. Interpreted Language
Python programs are executed by the Python interpreter.
Developers can run Python programs without explicitly
compiling them into machine code first.
3. Dynamically Typed
The type of a variable is determined at runtime.
Example
name = "John"
age = 25
salary = 45000.50
4. Object-Oriented
Python supports classes, objects, inheritance,
encapsulation and polymorphism.
5. Large Standard Library
Python provides modules for file handling, mathematics,
dates, operating system interaction, networking and
many other tasks.
3. Installing Python
To start programming in Python, Python must first be
installed on your computer.
Step 1: Install Python
Download and install Python using the official Python
distribution for your operating system.
Step 2: Verify Installation
Open Command Prompt or Terminal and execute:
Command Prompt
python --version
You may also use:
python3 --version
Tip:
During installation on Windows, enable the option
that adds Python to the system PATH.
4. First Python Program
The simplest Python program prints a message to the
screen using the print() function.
Python
print("Hello, World!")
Output
Hello, World!
Understanding the Program
print() is a built-in Python function
used to display information on the screen.
Another Example
print("Welcome to ELearnHub")
print("Learn Python Programming")
Output
Welcome to ELearnHub
Learn Python Programming
5. Python Variables
A variable is a name used to store a value in memory.
Python does not require explicit variable declarations.
Example
name = "Rahul"
age = 22
marks = 85.5
is_passed = True
Python automatically determines the type of each value.
Variable Naming Rules
- A variable name can contain letters, numbers and underscore.
- A variable cannot start with a number.
- Variable names are case-sensitive.
- Python keywords cannot be used as variable names.
Valid Variables
student_name = "Rahul"
studentAge = 21
marks1 = 90
_total = 100
Invalid Variables
1student = "Rahul"
student-name = "Rahul"
6. Python Data Types
A data type defines the kind of value stored in a
variable.
| Data Type |
Example |
Description |
| int |
10 |
Integer numbers |
| float |
10.5 |
Decimal numbers |
| str |
"Hello" |
Text |
| bool |
True |
Boolean value |
| list |
[10, 20, 30] |
Ordered mutable collection |
| tuple |
(10, 20, 30) |
Ordered immutable collection |
| set |
{10, 20, 30} |
Unordered collection of unique values |
| dict |
{"name": "Rahul"} |
Key-value collection |
Checking Data Type
age = 25
print(type(age))
Output
<class 'int'>
8. Python Operators
Operators are symbols used to perform operations
on values and variables.
Arithmetic Operators
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
Output
13
7
30
3.3333333333333335
3
1
1000
Comparison Operators
a = 10
b = 20
print(a == b)
print(a != b)
print(a > b)
print(a < b)
Logical Operators
age = 25
print(age > 18 and age < 60)
print(age < 18 or age > 60)
print(not(age > 18))
9. Conditional Statements
Conditional statements allow a program to make
decisions based on conditions.
if Statement
age = 20
if age >= 18:
print("Eligible")
if-else Statement
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")
if-elif-else
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Important:
Python uses indentation to define blocks of code.
Consistent indentation is therefore very important.
10. for Loop
A for loop is used to iterate over a sequence or
collection.
for number in range(1, 6):
print(number)
Output
1
2
3
4
5
Loop Through a List
names = ["Amit", "Rahul", "Priya"]
for name in names:
print(name)
11. while Loop
A while loop repeatedly executes a block of code
while a condition remains true.
count = 1
while count <= 5:
print(count)
count += 1
Output
1
2
3
4
5
Be careful:
Make sure the condition eventually becomes false.
Otherwise, the program may enter an infinite loop.
12. break and continue
break
The break statement immediately terminates a loop.
for number in range(1, 10):
if number == 5:
break
print(number)
Output
1
2
3
4
continue
The continue statement skips the current iteration
and continues with the next iteration.
for number in range(1, 6):
if number == 3:
continue
print(number)
Output
1
2
4
5
13. Python Functions
A function is a reusable block of code designed to
perform a specific task.
Creating a Function
def greet():
print("Welcome to Python")
greet()
Function with Parameters
def greet(name):
print("Hello", name)
greet("Rahul")
Output
Hello Rahul
14. Function Parameters
Parameters allow a function to receive data from
the calling code.
def add(a, b):
result = a + b
print("Sum =", result)
add(10, 20)
Output
Sum = 30
Default Parameter
def greet(name="Student"):
print("Hello", name)
greet()
greet("Amit")
15. Return Values
A function can return a value using the
return statement.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
Best Practice:
Functions should generally perform one clear task.
Small reusable functions make programs easier to
understand, test and maintain.
16. Lambda Functions
A lambda function is a small anonymous function.
It is useful when a simple function is required
for a short operation.
square = lambda x: x * x
print(square(5))
Output
25
The expression above creates a function that receives
one value and returns its square.
17. Python Lists
A list is an ordered and mutable collection.
Lists can contain multiple values.
students = ["Amit", "Rahul", "Priya"]
print(students)
print(students[0])
Adding an Item
students.append("Neha")
print(students)
Removing an Item
students.remove("Rahul")
print(students)
18. Python Tuples
A tuple is an ordered collection that cannot normally
be modified after it is created.
coordinates = (10, 20)
print(coordinates)
print(coordinates[0])
Remember:
Lists are mutable, while tuples are immutable.
19. Python Sets
A set is an unordered collection of unique values.
numbers = {10, 20, 30, 20, 10}
print(numbers)
Duplicate values are automatically removed.
20. Python Dictionaries
A dictionary stores data in key-value pairs.
student = {
"name": "Rahul",
"age": 21,
"course": "MCA"
}
print(student["name"])
print(student["course"])
Output
Rahul
MCA
Adding a New Value
student["marks"] = 85
print(student)
21. Classes and Objects
Python supports object-oriented programming.
A class defines the structure and behaviour of objects.
class Student:
def display(self):
print("This is a student")
student1 = Student()
student1.display()
Here, Student is the class and
student1 is an object created from
that class.
22. Constructor
The __init__() method is commonly used
to initialize an object's data when the object is created.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Rahul", 21)
print(student1.name)
print(student1.age)
Output
Rahul
21
23. Inheritance
Inheritance allows one class to acquire properties
and methods from another class.
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.sound()
dog.bark()
Output
Animal makes a sound
Dog barks
24. Polymorphism
Polymorphism means that the same interface or method
name can be used with different types of objects.
class Dog:
def sound(self):
print("Dog barks")
class Cat:
def sound(self):
print("Cat meows")
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output
Dog barks
Cat meows
25. Exception Handling
Exceptions are runtime errors that can interrupt
program execution. Python provides
try and except
for handling exceptions.
try:
number = int(input("Enter a number: "))
result = 100 / number
print(result)
except ValueError:
print("Please enter a valid number.")
except ZeroDivisionError:
print("Number cannot be zero.")
Why use exception handling?
It prevents expected runtime errors from terminating
the application unexpectedly and allows the program
to respond appropriately.
26. File Handling
Python provides built-in functions for creating,
reading and modifying files.
Writing to a File
file = open("student.txt", "w")
file.write("Welcome to Python")
file.close()
Reading a File
file = open("student.txt", "r")
content = file.read()
print(content)
file.close()
Using with
The with statement is generally
preferred because it handles closing the file
automatically.
with open("student.txt", "r") as file:
content = file.read()
print(content)
27. Python Modules
A module is a Python file containing reusable
functions, classes or variables.
Using a Built-in Module
import math
result = math.sqrt(25)
print(result)
Output
5.0
Import Specific Function
from math import sqrt
print(sqrt(100))
28. Python Mini Project
Student Marks Calculator
Let's combine variables, input, arithmetic operators,
conditions and functions to create a small student
marks calculator.
Complete Python Program
def calculate_result(marks):
total = sum(marks)
percentage = total / len(marks)
print("Total Marks:", total)
print("Percentage:", percentage)
if percentage >= 75:
print("Grade: A")
elif percentage >= 60:
print("Grade: B")
elif percentage >= 50:
print("Grade: C")
else:
print("Grade: D")
marks = []
for i in range(3):
mark = float(input("Enter marks: "))
marks.append(mark)
calculate_result(marks)
Concepts Used
- Functions
- Lists
- Variables
- Input
- for loop
- Conditional statements
- Arithmetic operations
Practice Task:
Modify this project to accept five subjects and
display the student's name, total marks, percentage,
grade and pass/fail status.
Python Course Summary
In this tutorial, you learned the fundamentals of
Python programming and gradually moved toward
object-oriented programming and practical development.
Topics Covered
- Introduction to Python
- Python Installation
- Variables and Data Types
- Input and Output
- Operators
- Conditional Statements
- Loops
- Functions
- Lists, Tuples, Sets and Dictionaries
- Classes and Objects
- Constructors
- Inheritance
- Polymorphism
- Exception Handling
- File Handling
- Modules
- Mini Project
Next Step:
Practice every example yourself. After completing
the fundamentals, build a small Python project such
as a calculator, student management system, quiz
application or expense tracker.