1.
Python Programming Language – Overview
Definition
Python is a high-level, interpreted, object-oriented, general-purpose programming language that
emphasizes code readability and simplicity.
History
• Created by Guido van Rossum
• First released in 1991
• Named after Monty Python, not the snake
Why Python?
• Easy to learn and understand
• Requires fewer lines of code
• Free and open source
• Large community support
• Portable across platforms
Characteristics
• High-level language – user-friendly
• Interpreted – executed line by line
• Dynamically typed – no need to declare variable types
• Object-oriented – supports classes and objects
• Extensive standard library
2. Python Environment
Python Interpreter
The Python interpreter reads and executes Python programs.
Execution Modes
Interactive Mode
• Commands are executed immediately
• Useful for testing
>>> 2 + 3
5
Script Mode
• Code written in .py files
• Used for large programs
print("Welcome to Python")
Python Shell
• Displays >>> prompt
• Executes commands one by one
3. Installing Python
Steps
1. Download Python
2. Install Python
3. Add Python to system PATH
4. Verify installation:
python --version
Python Editors / IDEs
• IDLE
• Visual Studio Code
• PyCharm
• Jupyter Notebook
4. Python Basic Syntax
Indentation
• Python uses indentation to define blocks of code
• Indentation replaces {} used in other languages
if True:
print("Python")
Statements
Each line is a statement
x = 10
Comments
Used to explain code
# This is a comment
"""
This is a
multi-line comment
"""
5. Variables
Definition
A variable is a name that refers to a value stored in memory.
Variable Assignment
a = 10
b = 5.5
name = "Python"
Rules for Naming Variables
• Must start with a letter or underscore
• Cannot start with a number
• Case-sensitive
• Cannot use keywords
Dynamic Typing
x = 10
x = "Hello"
6. Python Data Types
Classification of Data Types
Numeric Types
• int – integers
• float – decimal values
• complex – complex numbers
a = 10
b = 3.14
c = 2 + 3j
Boolean Type
True, False
Sequence Types
• String
• List
• Tuple
7. Computational Thinking with Python
Computational Thinking Concepts
Decomposition
Breaking a problem into smaller parts
Pattern Recognition
Identifying similarities
Abstraction
Ignoring unnecessary details
Algorithm
Step-by-step solution to a problem
Python supports computational thinking using:
• Variables
• Loops
• Conditional statements
• Functions
8. Expressions
Definition
An expression is a combination of values, variables, and operators that produces a result.
x=5+3
9. Operators
Types of Operators
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Power
// Floor Division
Relational Operators
>, <, >=, <=, ==, !=
Logical Operators
and, or, not
Assignment Operators
=, +=, -=, *=
10. Strings
Definition
Strings are sequences of characters enclosed in quotes.
s = "Python"
String Indexing
s[0] # P
String Slicing
s[1:4] # yth
String Operations
len(s)
[Link]()
[Link]()
[Link]("P", "J")
11. Lists
Definition
A list is an ordered, mutable collection of elements.
list1 = [1, 2, 3, 4]
Features
• Can store different data types
• Mutable (can be modified)
List Operations
[Link](5)
[Link](2)
[Link](1, 10)
List Slicing
list1[0:2]
12. Objects and Classes
Object-Oriented Programming (OOP)
OOP organizes programs using classes and objects.
Class
A blueprint for creating objects
class Person:
def __init__(self, name):
[Link] = name
def greet(self):
print("Hello", [Link])
Object
An instance of a class
p1 = Person("Alice")
[Link]()
13. Python Standard Library
Definition
A collection of built-in modules available in Python.
Advantages
• Saves time
• Reliable
• No installation required
Important Modules
• math
• random
• os
• sys
• datetime
import math
print([Link](25))
14. Very Simple Python Programs
Hello World
print("Hello World")
Addition of Two Numbers
a = 10
b = 20
print(a + b)
User Input
name = input("Enter name: ")
print("Welcome", name)
Even or Odd
num = int(input("Enter number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Objects & Classes – Python | Standard Library
1. Introduction to Python
Python is a high-level, interpreted, object-oriented, and general-purpose programming language.
It is known for its simple syntax, readability, and vast standard library. Python is widely used in web
development, data science, machine learning, automation, networking, and software
development.
Key Features:
• Easy to learn and use
• Interpreted language (no compilation required)
• Platform independent
• Object-Oriented
• Rich Python Standard Library
2. Objects and Classes in Python
2.1 Object-Oriented Programming (OOP)
Object-Oriented Programming is a programming paradigm based on the concept of objects, which
contain data (variables) and methods (functions).
Main OOP Concepts:
• Class
• Object
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
2.2 Class
A class is a blueprint or template for creating objects. It defines attributes (data members) and methods
(member functions).
Syntax:
class ClassName:
statements
Example:
class Student:
def display(self):
print("This is Student class")
2.3 Object
An object is an instance of a class. It represents a real-world entity.
Example:
s1 = Student()
[Link]()
Here:
• Student() creates an object
• s1 is the object reference
2.4 self Keyword
• self represents the current object of the class
• It is used to access class variables and methods
• It must be the first parameter of every instance method
Example:
class Person:
def setdata(self, name):
[Link] = name
def show(self):
print("Name:", [Link])
2.5 Constructor (__init__ method)
A constructor is a special method that is automatically called when an object is created.
Syntax:
class ClassName:
def __init__(self):
statements
Example:
class Employee:
def __init__(self, id, name):
[Link] = id
[Link] = name
def display(self):
print([Link], [Link])
emp1 = Employee(101, "Amit")
[Link]()
2.6 Types of Variables in a Class
1. Instance Variables – Unique to each object
2. Class Variables – Shared among all objects
Example:
class Demo:
x = 10 # class variable
def __init__(self, y):
self.y = y # instance variable
3. Python Standard Library
3.1 What is Python Standard Library?
The Python Standard Library is a collection of built-in modules that provide predefined functions
and classes to perform common tasks.
Advantages:
• No need to install separately
• Saves development time
• Reliable and optimized
3.2 Commonly Used Standard Library Modules
1. math Module
Used for mathematical operations.
import math
print([Link](25))
print([Link](5))
2. sys Module
Used for system-specific parameters and functions.
import sys
print([Link])
3. os Module
Used for interacting with the operating system.
import os
print([Link]())
4. datetime Module
Used to work with dates and times.
import datetime
print([Link]())
5. random Module
Used to generate random numbers.