Python notes
Python is a high-level, interpreted, object-oriented programming language created by Guido van Rossum
in 1991.
It is widely used for web development, data science, artificial intelligence, machine learning,
automation, software development, and scripting.
Python is popular because it is simple, readable, and has a large collection of libraries and
frameworks.
When you write Python programs, you save them with the extension:
.py → Standard Python file extension
Example: program.py
Main Features of Python
1. Simple & Easy to Learn – Python syntax is close to English, making it beginner-friendly.
2. Interpreted Language – Code is executed line by line; no need for compilation.
3. Dynamically Typed – You don’t need to declare variable types explicitly (x = 10 works directly).
4. High-Level Language – Easy to read and write; abstracts away complex details like memory
management.
5. Object-Oriented – Supports classes, objects, inheritance, encapsulation, and polymorphism.
6. Extensive Standard Library – Comes with built-in modules for math, file handling, networking, GUI,
etc.
7. Cross-Platform – Works on Windows, macOS, Linux, and more without modification.
8. Portable – Python programs can run on different systems with minimal or no changes.
9. Free & Open-Source – Freely available, with a huge community for support.
10. Embeddable & Extensible – Python can be embedded in C/C++ programs or extended with other
languages.
A program is a set of instructions written in Python to perform a specific task.
It is made up of statements (lines of code) that tell the computer what to do.
Example:
# A simple Python program to add two numbers
a=5
b=3
sum = a + b
print("The sum is:", sum)
👉 Here, the program’s task is to add two numbers and display the result.
✅ Variable in Python
A variable is a name that refers to a value stored in memory.
It acts like a container to store data (numbers, text, etc.).
In Python, you don’t need to declare the type explicitly—it’s determined automatically.
Example:
# Variable examples
x = 10 # integer variable
name = "Alice" # string variable
pi = 3.14 # float variable
print(x, name, pi)
What are Comments in Python?
Comments are notes or explanations in your code that the Python interpreter ignores.
They are only for humans (programmers) to make the code easier to understand.
They do not affect the execution of the program.
1. Single-line Comment
Use the # symbol.
Everything after # on that line is ignored.
Example:
# This is a single-line comment
x = 10 # assigning 10 to variable x
print(x)
Multi-line Comment
Python does not have a built-in multi-line comment syntax like /* */ in C/Java.
But we use:
Multiple # symbols, or
Triple quotes (''' or """) (though mainly used for docstrings).
"""
This is a multi-line comment
explaining the program.
It won’t be executed by Python.
"""
print("Hello, World!")
What is a Flowchart?
A flowchart is a diagrammatic representation of the sequence of steps in a program.
It uses symbols like:
o Oval → Start / End
o Parallelogram → Input / Output
o Rectangle → Process (calculation, assignment, etc.)
o Diamond → Decision (if/else condition)
Input in Python
In Python, input is taken from the keyboard using the input() function.
By default, input() returns data as a string, so we often convert it to int, float, etc.
Example:
name = input("Enter your name: ") # takes string input
age = int(input("Enter your age: ")) # takes integer input
print("Hello", name, "you are", age, "years old.")
Output in Python
Output is displayed using the print() function.
You can print strings, numbers, variables, or expressions.
Example:
x = 10
y=5
print("The value of x is", x)
print("The sum is:", x + y)
Arithmetic Operators in Python
a = 10
b=3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b) # float division
print("Floor Division:", a // b) # integer result
print("Modulus:", a % b) # remainder
print("Exponentiation:", a ** b) # 10^3
🖥️Output:
yaml
Copy code
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000
sequential Control Structure
The default way: instructions are executed one after another.
Example:
print("Step 1")
print("Step 2")
print("Step 3")
Decision-Making (Selection) Control Structure
Uses conditional statements to choose a path.
Keywords: if, if-else, if-elif-else.
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Repetition (Looping) Control Structure
Executes a block of code multiple times.
Keywords: for, while.
Example (for loop):
for i in range(1, 6):
print(i)
What is range() in Python?
range() is a built-in function used to generate a sequence of numbers.
Commonly used with loops (for and while).
It does not create a list directly, but a range object (which can be converted to a list).
Syntax of range()
range(start, stop, step)
start → (optional) the first number in the sequence (default = 0).
stop → (required) the number where the sequence ends (exclusive).
step → (optional) the difference between numbers (default = 1).
for i in range(5):
print(i)
👉 Output:
Copy code
for i in range(2, 6):
print(i)
👉 Output:
Copy code