Unit 7: Python
Programming
What is Programming?
Programming is the process of creating instructions for a computer to perform specific
tasks.
Instructions are written in programming languages like Python, JavaScript, and C++.
Example: A program can automate tasks, analyze data, or create software applications.
Programming Languages
A computer program is a set of instructions written in a programming language.
The source code is the human-readable version of the program.
Programming languages are grouped into categories based on features and usage, such
as procedural and object-oriented languages.
Translators
Translators convert human-written source code into machine-readable language.
There are three types of translators:
1. Compiler
Converts the entire source code into machine code before execution.
Faster execution after compilation (e.g., C, Java).
2. Interpreter
Translates and runs code line by line.
Slower than compilers but useful for debugging (e.g., Python).
3. Assembler
Converts assembly language into machine code.
Algorithm and Flowchart
Algorithm: A step-by-step procedure to solve a specific problem.
Example: Recipe instructions or mathematical formulas.
Flowchart: A diagram that visually represents an algorithm using symbols.
Common symbols:
Oval: Start/End
Rectangle: Process/Action
Diamond: Decision-making
Procedural vs. Object-Oriented Programming
Feature Procedural Object-Oriented
Focus Step-by-step tasks Objects with data &
behavior
Example C, Pascal Python, Java
Structure Functions Classes and Objects
Approach Top-down Bottom-up
OOP Concepts
1. Object: A real-world entity (e.g., a car or a student).
2. Class: A blueprint for creating objects (e.g., Car class with properties like color, speed).
3. Inheritance: Allows a new class to use properties and methods of an existing class (e.g., a
SportsCar inherits from Car).
4. Polymorphism: Same method behaves differently based on the context (e.g., draw() for
different shapes).
5. Abstraction: Hides complex details and shows only the necessary features.
6. Encapsulation: Combines data and methods into a single unit, restricting direct access to
some elements.
Python Programming
Python is a versatile, general-purpose programming language.
Applications: Machine learning, web development, data analysis, and desktop
applications.
Features of Python
Easy-to-read syntax.
Interpreted (code runs directly without compilation).
Supports object-oriented programming.
Extensive libraries for various applications.
Tokens
Tokens are the smallest elements in Python code.
Examples: Keywords (e.g., if, else), Identifiers, Operators, Literals, and Punctuators.
Variables
Variables are symbolic names used to store data.
Rules for declaring variables:
1. Start with a letter or underscore (not a number).
2. Avoid spaces, commas, or special characters.
3. Variables are case-sensitive (name and Name are different).
4. Keywords (e.g., def, return) cannot be used as variable names.
Operators
Symbols that perform operations on variables or values.
Types of operators:
1. Arithmetic: +, -, *, /
2. Relational: ==, <, >
3. Assignment: =, +=, -=
4. Logical: and, or, not
Control Flow of a Program
The order in which program statements are executed.
Types of control structures:
1. Sequential: Default execution line by line.
2. Selection: Decisions using if, elif, else.
3. Repetition: Loops (for, while) for repeating tasks.