Python Programming
Transition from Flowchart and Algorithm to Programming
Algorithm: A step-by-step logical sequence of instructions designed to solve a specific
problem. Algorithms are language-independent and focus on logic, not syntax.
Example Algorithm:
1. Start
2. Input number A
3. Input number B
4. Compute Sum = A + B
5. Display Sum
6. Stop
Flowchart: A graphical representation of an algorithm using symbols such as oval
(Start/End), parallelogram (Input/Output), rectangle (Process), and diamond (Decision).
These help in visualizing logic before programming. Python makes it easy to convert this
logic into executable code.
3.1 Introduction to Python
Python was developed by Guido van Rossum in 1991 at CWI, Netherlands. It emphasizes
simplicity, readability, and productivity. Named after 'Monty Python’s Flying Circus'.
Variable Declaration
Variables store data values. They are created when a value is assigned, and types need
not be declared explicitly since Python is dynamically typed.
Example:
x = 10
name = 'Ravi'
pi = 3.14
Variable Casting
Casting converts one data type to another. Example:
x = int(3.9)
y = float(5)
z = str(10)
Indentation, Comments, and Operators
Python uses indentation to define code blocks, unlike braces in C. Comments begin with
'#'. Multi-line comments use triple quotes.
Operator Type Examples Description
Arithmetic + - * / % // ** Basic math operations
Comparison == != > < >= <= Compare two values
Logical and, or, not Combine conditions
Assignment = += -= *= /= Assign values
Membership in, not in Check membership
Identity is, is not Compare memory addresses
Bitwise & | ^ ~ << >> Bit-level operations
3.2 Python Commands, Loops, and Functions
Command Purpose Example
print() Display output print('Hello')
input() Take user input name = input('Enter name: ')
len() Find length len('Python')
type() Show data type type(3.14)
Loops: Used for repetition.
Example:
for i in range(5):
print(i)
while i < 5:
print(i)
i += 1
Functions: Reusable blocks of code defined using 'def'.
def add(a, b):
return a + b
3.3 Data Structures
Type Description Example
Array Stores elements of same type import array<br/>a=[Link]('i',[1,2,3])
List Ordered, mutable fruits=['apple','banana']
Tuple Ordered, immutable colors=('red','blue')
Dictionary Key-value pairs student={'name':'Ravi','age':21}
Set Unordered, unique nums={1,2,3}
3.4 Objects and Classes
A class is a blueprint for creating objects that combine data and methods.
class Student:
def __init__(self,name):
[Link]=name
3.5 Inheritance and Polymorphism
Inheritance allows a class to use features of another class. Polymorphism enables one
interface with many implementations.
class Animal:
def speak(self): print('Animal')
class Dog(Animal):
def speak(self): print('Dog')
3.6 Python Libraries
Library Purpose Example
math Mathematical functions [Link](16)
numpy Numerical arrays [Link]([1,2,3])
scipy Scientific tools [Link]()
pandas Data analysis [Link](data)
Django Web development django-admin startproject mysite
turtle Graphics & drawing [Link](100)
3.7 Applications in Data Science
Python supports data analysis, visualization, and machine learning using libraries like
pandas, numpy, matplotlib, and scikit-learn.
import pandas as pd
data = pd.read_csv('[Link]')
print([Link]())
3.8 Optimization Tools in Python
Library Function
[Link] minimize(), curve_fit()
cvxpy Convex optimization
pulp Linear programming
pyomo Mathematical modeling
tensorflow Gradient-based optimization
from [Link] import minimize
def f(x): return x**2+5*x+6
result=minimize(f,0)
print(result.x)
Summary
Python enables smooth transition from algorithmic thinking to real-world programming.
With its readability and vast library support, it is ideal for education, data science, and
optimization tasks.