0% found this document useful (0 votes)
5 views40 pages

UNIT 1

The document provides an introduction to Python, covering its features, basic syntax, and key concepts such as functions, modules, packages, and data types. It also explains control structures, exception handling, and object-oriented programming principles like encapsulation, inheritance, and polymorphism. Additionally, it includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

iyonahpoloko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views40 pages

UNIT 1

The document provides an introduction to Python, covering its features, basic syntax, and key concepts such as functions, modules, packages, and data types. It also explains control structures, exception handling, and object-oriented programming principles like encapsulation, inheritance, and polymorphism. Additionally, it includes examples and exercises to reinforce understanding of these concepts.

Uploaded by

iyonahpoloko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Introduction to Python

• Python is an interpreted, high-level


programming language.
• It is widely used in web development,
automation, AI, and data science.
• Features:
- Simple syntax and easy to learn.
- Cross-platform and open-source.
- Supports Object-Oriented and Functional
Programming.
Basic Syntax in Python

• Python uses indentation for defining code


blocks.
Understanding Functions, Modules, and Packages in
Python

Functions in Python
• A function is a block of reusable code that
performs a specific task. It helps in organizing
code and improving reusability.
Functions in Python

•Built-in Functions: Python provides many built-in functions like len(), print(), type() etc. .

•User-Defined Functions: Functions created by users for specific tasks.


•Lambda Functions: Anonymous functions defined using the lambda keyword.

Example of a lambda function:


Functions in Python

• Activity: Create a Function to Calculate Final


Marks
Functions in Python
• Solution:
Modules in Python
• A module is a file containing Python code
(functions, classes, or variables) that can be
reused in different programs.
• Creating a Module
Make a file named math_utils.py:
Modules in Python
• Importing a Module
• We can use the import statement to include a
module in another Python file.
Modules in Python

• Types of Imports
• Import Entire Module: import math_utils
• Import Specific Functions: from math_utils
import add
• Rename Module: import math_utils as mu
• Import All Functions: from math_utils import *
Packages in Python
• A package in Python is a collection of modules organized in a directory
with an __init__.py file. It helps in structuring large programs by grouping
related modules together.
Variables & Data Types
• Python automatically detects data types.
• Example:
• x = 10 # Integer
• y = 3.14 # Float
• name = 'AI' # String
• is_valid = True # Boolean
Variables & Data Types
• Numeric Data Type

• Sequence Data Type


Variables & Data Types
• Set Type

• Mapping Type

• Boolean Type
Introduction to Control Structures

• Control structures allow decision-making and


repetition in programs.
• Three main types:
• 1. Conditional Statements (if-else)
• 2. Loops (for, while)
• 3. Exception Handling (try-except)
if-else Statements
• Used for decision-making in Python.
• Example:
• age = 18
• if age >= 18:
• print('You are an adult.')
• else:
• print('You are a minor.')
if-else Statements
• Question:

Write a Python program that asks the user to


enter a number. The program should check
whether the number is even or odd and print
the result.
if-else Statements
• Solution:
Loops in Python
• Loops allow repeated execution of code.
• Two types:
• 1. for loop
• 2. while loop
For Loop Example
• The for loop iterates over a sequence.
• Example:
While Loop
• The while loop runs as long as a condition is
true.
• Example:
Questions
• Write a Python program that prints the
multiplication table of a given number (N)
using a for loop.

• Write a Python program that prints all even


numbers from 1 to 20 using a while loop.
Solutions
1.

2.
Exception Handling in Python
• Exception handling is a mechanism that allows a program to
handle errors gracefully instead of crashing. It helps in
debugging and ensures smooth execution.

• Runtime Errors (Exceptions) – Errors that occur during


execution.
• ZeroDivisionError: Dividing by zero
• ValueError: Invalid data type conversion
• IndexError: Accessing an invalid index in a list
• KeyError: Accessing a non-existent key in a dictionary
Exception Handling in Python
Handling Exceptions using try-except

Using else with try-except


Exception Handling in Python
Using finally Block

Raising Custom Exceptions (raise keyword)


Object-Oriented Programming (OOP) in
Python
• Object-Oriented Programming (OOP) is a way
of writing programs by grouping related data
and functions into objects. Instead of writing
everything separately, we create classes
(blueprints) and make objects from them. This
helps keep the code organized, reusable, and
easy to manage.
Key Principles of OOP
• Encapsulation: Bundling data and methods
that operate on the data.
• Abstraction: Hiding implementation details
and showing only the necessary parts.
• Inheritance: Creating new classes from
existing ones to promote code reusability.
• Polymorphism: Ability to use a common
interface for multiple data types.
Classes and Objects
• Class: A blueprint for creating objects.
• Object: An instance of a class.
Classes and Objects
Question:
• Create a class called Car with the following
attributes: brand (e.g., "Toyota")model (e.g.,
"Corolla")color (e.g., "Red").
• Write a method show_details() that prints the
car’s brand, model, and color.
• In your main.py, create at least two different
Car objects and call the show_details() method
for each.
Solution
Encapsulation
• Encapsulation is a mechanism where the
data(variables) and the code(methods) that
act on data will bind together.

• Example: A programmer can declare and use


the variables like id, name, and address in
different classes like Employee, Customer, or
Student classes.
Encapsulation
Encapsulation
Question:
• Create a Python class named BankAccount with the
following attributes:account_holder
(string)account_number (integer)balance (float, starting
at 0).
• Implement two methods: deposit(amount): adds the
given amount to the balance. withdraw(amount):
subtracts the amount from the balance only if there are
sufficient funds; otherwise, print "Insufficient funds.
• Then, create a BankAccount object, perform a deposit
and a withdrawal, and print the final balance.
Encapsulation
• Solution:
Inheritance
• Creating new classes from existing classes so
that the new classes will acquire all the
features of the existing class.
Inheritance
• Create a class Employee with attributes name
and salary. Define a method show_details().

• Derive a class Manager that adds department


and overrides show_details(). Instantiate both
classes and call show_details().
Inheritance
Polymorphism
• Polymorphism means "many forms" in
programming. It allows the same function or
method to work differently for different
objects.
Abstraction
• Hiding implementation details and exposing
only essential features.
Constructor
• In Python, a constructor is a special method
used to initialize objects of a class. It is defined
using the __init__() method and is
automatically called when a new instance of
the class is created.

You might also like