Python Programming Basics
1. Introduction to Python Programming
Python is a high-level, interpreted programming language known for its simplicity and
readability. It's widely used in web development, data science, artificial intelligence,
automation, and more.
Key Features of Python:
- Simple syntax similar to English
- Interpreted language (runs line-by-line)
- Dynamically typed
- Supports object-oriented and functional programming
Real-life Analogy: Using Python is like writing instructions in plain English for the computer to
follow.
2. Variables in Python
A variable is a container that stores data values. You don’t need to declare the type of variable
in Python; it is determined automatically.
Syntax:
variable_name = value
Example:
name = "Alice"
age = 21
Real-time Example:
Think of a variable like a labeled jar where you store something.
milk = "Amul"
quantity = 2 # liters
3. Data Types
Python has several built-in data types. Common ones include:
int - Integer numbers (e.g., x = 10)
float - Decimal numbers (e.g., price = 19.99)
str - String of characters (e.g., name = "Bob")
bool - Boolean (e.g., is_open = True)
list - Ordered collection (e.g., fruits = ["apple", "banana"])
Real-time Example:
temperature = 36.6
student_name = "John"
is_present = True
4. Operators in Python
Operators are symbols that perform operations on variables and values.
Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
// (Floor Division)
% (Modulus)
** (Exponentiation)
Comparison Operators: ==, !=, >, <, >=, <=
Assignment Operators: =, +=, -=, *=, /=, //=, %=
Logical Operators: and, or, not
Real-time Example:
price = 50
discount = 10
final_price = price - discount
age = 18
is_eligible = age >= 18
5. Simple Programs using Assignment and Input Statement
Program 1: Calculate Area of a Rectangle
length = float(input("Enter length of the rectangle: "))
breadth = float(input("Enter breadth of the rectangle: "))
area = length * breadth
print("Area of the rectangle is:", area)
Real-time Use: Used in architecture for calculating floor areas.
Program 2: Simple Interest Calculator
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time in years: "))
simple_interest = (principal * rate * time) / 100
print("Simple Interest is:", simple_interest)
Real-time Use: Used in banking for interest calculation.
Program 3: Basic Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Sum:", num1 + num2)
print("Difference:", num1 - num2)
print("Product:", num1 * num2)
print("Quotient:", num1 / num2)
Program 4: Check Voting Eligibility
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Real-time Use: Used in online registration systems.
Summary
- Variables store data.
- Data types determine the kind of data (number, text, etc.).
- Operators perform calculations and logic.
- Use input() for user interaction and assignment (=) for storing values.
- Real-time examples make programs meaningful and relevant.