PROGRAMMING ALGORITHMS
Introduction to Python
Python is a high-level, easy-to-read programming language.
Simple syntax (reads like English).
No need to compile code (runs directly)
Variables in Python
What is a Variable?
A variable is like a labeled box that stores data. it is a named storage location in a computer's
memory that holds a value which can change (vary) during the execution of a program.
Key Features of Variables:
Name (Identifier) → Used to refer to the stored value (e.g., age, score, name).
Value → The actual data stored (e.g., 10, "Alice", 3.14).
Data Type → Defines the kind of data the variable can hold (e.g., integer, string, float).
Mutable (Changeable) → The value can be updated during the program.
How to Declare Variables?
age = 25
name = "Alice"
price = 19.99
Rules for Naming Variables
Must start with a letter or _ (e.g., name, _count).
Can contain letters, numbers, and _ (e.g., user1, total_score).
Cannot use spaces or special symbols (@, #, %).
Cannot use Python keywords (if, for, while`).
Data Types in Python
Python has different types of data that variables can hold:
A. Primitive Data Types
Integer - Whole numbers (no decimals).
Float - Decimal numbers.
String - Text (enclosed in quotes).
Boolean- Represents logical values. (True/False)
Character – a single letter string.
Operators in Python
Operators are symbols that perform operations on variables.
A. Arithmetic Operators
+ It is used for Addition (a + b)
- It is used for Subtraction (a – b)
* It is used for Multiplication (a * b)
/ It is used for Division (float) (a / b)
% It is used for Modulus (remainder) (a %b)
Example:
a = 10
b=3
sum = a + b
print(sum)
a = 13
b =2
multiplication = a* b
print (multiplication)
Comparison Operators
They are used to compare values (returns True or False):
== a == b Equal to
!= a != b Not equal to
> a> b Greater than
< a<b Less than
>= a >= b Greater than or equal to
<= a <= b Less than or equal to
Example:
x, y = 5, 10
print(x == y)
print(x < y)
Logical Operators
They are fundamental for building blocks in programming that allows you to combine and
modify Boolean (true/false) values.
Used to combine multiple conditions:
AND a and b will return True if only both operands are True
OR a or b will return True if at least one operand is True
NOT not a Inverts the condition, returns the opposite Boolean value (True→False)
Example
age = 20
is_student = True
print(age >= 18 and is_student)
True
has_ticket = False
print(age > 12 or has_ticket)
True
Control Flow in Python
Control flow lets you make decisions and repeat actions.
Conditional Statements (if, elseif, else)
Used to execute code based on conditions.
Simple if Statement
age = 18
if age >= 18:
print("You are an adult.")
if-else Statement
if age >= 18:
print("Adult")
else:
print("Minor")