📘 Python Programming: Introduction and Basics
🐍 Introduction to Python
Python is a high-level, interpreted, general-purpose programming language.
Created by Guido van Rossum and first released in 1991.
Known for its readability, simplicity, and extensive libraries.
It supports multiple programming paradigms: procedural, object-oriented,
and functional.
✨ Features of Python
1. Simple and Easy to Learn – Python syntax is clean and readable.
2. Interpreted Language – Code is executed line by line (no need for
compilation).
3. Platform Independent – Write once, run anywhere.
4. Open Source – Free to use and distribute.
5. High-Level Language – Focuses on logic, not memory management.
6. Extensive Libraries – Rich set of built-in modules and third-party packages.
7. Dynamic Typing – No need to declare variable types.
8. Large Community Support – Strong online support and documentation.
9. Portability – Code can run on various operating systems without change.
10. Integrated – Can easily integrate with other languages like C, C++, Java.
Executing a Simple “Hello World” Program
🔹 Using Interactive Mode:
>>> print("Hello, World!")
Hello, World!
🔹 Using Script Mode:
1. Open a text editor (e.g., Notepad or VS Code).
2. Write the code:
print("Hello, World!")
3. Save it as [Link].
4. Run it from the terminal:
python [Link]
🧩 Python Execution Modes
1. Interactive Mode
Executed line-by-line.
Useful for testing small code snippets.
Access via Python shell or terminal.
>>> 2 + 2
2. Script Mode
Write code in a file with .py extension.
Allows writing multi-line programs.
Run using:
python [Link]
🔤 Python Character Set
The Python character set includes:
Letters: A-Z, a-z
Digits: 0-9
Special Symbols: +, -, *, /, %, @, etc.
White spaces: space, tab, newline
Other characters: Unicode characters (e.g., emojis, symbols)
🧱 Python Tokens
Tokens are the smallest building blocks of a Python program. They are:
1. Keywords
Reserved words with special meaning.
Examples: if, else, while, for, def, return, True, False, etc.
Python 3.10 has 36 keywords. Use keyword module to list them:
import keyword
print([Link])
2. Identifiers
Names used to identify variables, functions, classes, etc.
Rules:
o Must begin with a letter (A–Z or a–z) or an underscore (_)
o Followed by letters, digits, or underscores
o Case-sensitive
o Cannot use keywords as identifiers
3. Literals
Fixed values used in Python code.
String literal: 'Hello', "World"
Numeric literal: 100, 3.14
Boolean literal: True, False
Special literal: None
Collection literals: [1,2], (3,4), {‘a’:1}
4. Operators
Symbols that perform operations on variables and values.
Arithmetic Operators: +, -, *, /, //, %, **
Comparison Operators: ==, !=, >, <, >=, <=
Logical Operators: and, or, not
Assignment Operators: =, +=, -=, etc.
Bitwise Operators: &, |, ^, ~, <<, >>
5. Punctuators (Delimiters)
Used to structure the code.
Symbol Use
: Block declaration
() Function call or grouping
{} Dictionary or set
[] List or index
, Separator
. Attribute or method access
# Comment
\ Line continuation
'/" String literals
🧮 Variables in Python
What is a Variable?
A variable is a name that refers to a value stored in memory.
It is created when you assign a value to it.
Example:
x = 10
name = "Alice"
Rules for Naming Variables:
Must start with a letter or underscore.
Can contain letters, numbers, and underscores.
Case-sensitive (age and Age are different).
Avoid using Python keywords as variable names.
Dynamic Typing:
x = 10 # x is int
x = "Hello" # x is now str
📘 Python Data Types: Detailed Notes
🧮 1. Number Data Types
Python supports three main types of numeric values:
🔹 a. Integer (int)
Whole numbers, positive or negative, without decimals.
Example: 10, -5, 0
x = 25
print(type(x)) # Output: <class 'int'>
🔹 b. Floating Point (float)
Numbers with decimal points.
Example: 3.14, -0.99, 10.0
y = 3.1415
print(type(y)) # Output: <class 'float'>
🔹 c. Complex (complex)
Numbers with a real and an imaginary part.
Represented as: a + bj
Example: 3 + 5j
z = 3 + 5j
print(type(z)) # Output: <class 'complex'>
✅ 2. Boolean (bool)
Represents one of two values: True or False.
Internally, True is treated as 1 and False as 0.
flag = True
print(type(flag)) # Output: <class 'bool'>
print(True + False) # Output: 1
📚 3. Sequence Data Types
🔹 a. String (str)
Immutable sequence of Unicode characters.
Created using single, double, or triple quotes.
name = "Alice"
print(type(name)) # Output: <class 'str'>
🔹 b. List (list)
Ordered, mutable (changeable) sequence of elements.
Elements can be of different data types.
fruits = ["apple", "banana", 42]
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 42]
print(type(fruits)) # Output: <class 'list'>
🔹 c. Tuple (tuple)
Ordered, immutable sequence of elements.
Faster and safer than lists for read-only data.
colors = ("red", "green", "blue")
print(type(colors)) # Output: <class 'tuple'>
❌ 4. None Type
Represents the absence of a value or a null value.
Often used as a default return value.
x = None
print(type(x)) # Output: <class 'NoneType'>
5. Mapping Data Type
🔹 Dictionary (dict)
Collection of key-value pairs.
Keys must be unique and immutable (e.g., string, number, tuple).
Values can be any data type.
student = {"name": "Alice", "age": 20, "marks": 95}
print(student["name"]) # Output: Alice
print(type(student)) # Output: <class 'dict'>
🔁 6. Mutable vs Immutable Data Types
🔸 Mutable Data Types (Can be changed after creation)
Examples: list, dict, set
Can change contents without changing their identity (memory address).
x = [1, 2, 3]
x[0] = 100
print(x) # Output: [100, 2, 3]
🔹 Immutable Data Types (Cannot be changed after creation)
Examples: int, float, str, tuple, bool, NoneType
Any change results in a new object.
a = "hello"
a = a + " world" # New string object created
print(a) # Output: "hello world"
📊 Summary Table
Data Mutabl
Example
Type e?
int ❌ 10, -2
3.14, -
float ❌
0.5
complex ❌ 2 + 3j
True,
bool ❌
False
str ❌ "Python"
list ✅ [1, 2, 3]
tuple ❌ (4, 5, 6)
dict ✅ {"a": 1}
NoneTyp
❌ None
e
🔧 Python Operators: Complete Notes
➕ 1. Arithmetic Operators
Used to perform basic mathematical operations.
Operat Examp Outp
Name
or le ut
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
Multiplicatio
* 10 * 5 50
n
/ Division 10 / 5 2.0
Floor
// 10 // 3 3
Division
% Modulus 10 % 3 1
Exponentiati
** 2 ** 3 8
on
🔍 2. Relational (Comparison) Operators
Used to compare two values. Returns a Boolean value (True or False).
Operat Examp Outp
Meaning
or le ut
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 5<3 False
Greater than or
>= 5 >= 5 True
equal
<= Less than or equal 3 <= 5 True
⚙️3. Logical Operators
Used to combine conditional statements.
Operat Outp
Description Example
or ut
True and
and True if both are true False
False
True if at least one is
or True or False True
true
not Inverts the result not True False
📝 4. Assignment Operators
Used to assign values to variables.
Operat Examp
Meaning
or le
Assigns 10
= x = 10
to x
➕= 5. Augmented Assignment Operators
Combines an operator with assignment.
Operat Examp Equivalent
or le To
+= x += 5 x = x + 5
-= x -= 3 x=x-3
Operat Examp Equivalent
or le To
*= x *= 2 x = x * 2
/= x /= 4 x=x/4
//= x //= 2 x = x // 2
%= x %= 3 x = x % 3
**= x **= 2 x = x ** 2
🆔 6. Identity Operators
Used to compare memory locations of two objects.
Operat Examp
Description Output
or le
True if both refer to same True/
is x is y
object False
True if they refer to different x is not True/
is not
objects y False
Example:
x = [1, 2]
y=x
z = [1, 2]
print(x is y) # True (same object)
print(x is z) # False (same value, different object)
🔍 7. Membership Operators
Used to check membership in a sequence (like list, string, etc.)
Operat Outp
Description Example
or ut
in True if value is found 'a' in 'apple' True
True if value is not 3 not in
not in True
found [1,2,4]
🧠 Quick Summary Table
Category Operators
Arithmetic +, -, *, /, //, %, **
==, !=, >, <, >=,
Relational
<=
Logical and, or, not
Assignmen
=
t
Augmente +=, -=, *=, /=, //=,
d %=, **=
Identity is, is not
Membershi
in, not in
p
🧮 Python Expressions and Data Handling: Detailed Notes
🔢 1. Precedence of Operators
Operator precedence determines the order in which operations are performed.
🔽 Precedence Order (High to Low)
Preceden
Operators Description
ce
1 () Parentheses
2 ** Exponentiation
Unary plus and
3 +, - (unary)
minus
Multiplication,
4 *, /, //, %
Division
Addition,
5 +, -
Subtraction
==, !=, >, <, Comparison
6
>=, <= operators
7 not Logical NOT
8 and Logical AND
9 or Logical OR
10 =, +=, -=, etc. Assignment
Preceden
Operators Description
ce
operators
👉 Operators with the same precedence are evaluated from left to right,
except ** (right to left).
🧾 2. Expression
An expression is a combination of values, variables, operators, and function
calls that Python can evaluate to produce a result.
Examples:
3+4 # Arithmetic expression
a * (b + c) # Expression with variables and parentheses
x > 10 and x < 20 # Logical expression
🔁 3. Evaluation of an Expression
Python evaluates expressions based on operator precedence and associativity.
Example:
result = 3 + 4 * 2
# First 4 * 2 = 8, then 3 + 8 = 11
You can use print() or type() to evaluate and understand the output:
x = 10
y=5
print(x + y * 2) # Output: 20
🔄 4. Type Conversion
Python automatically or manually converts data from one type to another.
🔹 a. Implicit Type Conversion
Done automatically by Python.
Converts lower data types to higher types to avoid data loss.
x = 10 # int
y = 3.5 # float
z=x+y # int + float → float
print(z) # Output: 13.5
🔸 b. Explicit Type Conversion (Type Casting)
Done manually using type conversion functions:
o int(), float(), str(), bool(), etc.
a = "123"
b = int(a) # Converts string to integer
print(b + 1) # Output: 124
🧑💻 5. Accepting Input from the Console
🔹 input() Function
Used to get user input.
Always returns string type.
name = input("Enter your name: ")
print("Hello", name)
🔸 Convert Input to Other Types:
age = int(input("Enter your age: "))
print("Next year, you'll be", age + 1)
6. Displaying Output
🔹 print() Function
Used to display output to the console.
print("Welcome to Python!")
🔸 print() with multiple arguments:
a = 10
b = 20
print("Sum =", a + b)
🔸 Formatting Output:
name = "Alice"
score = 95.5
print(f"{name} scored {score} in Math.") # f-string (recommended)
🧠 Quick Summary Table
Concept Description
Precedence Rules for order of operators
Expression A code that computes a value
Evaluation Python calculates value of an expression
Implicit Conversion Automatic type conversion
Explicit Conversion Manual conversion using int(), etc.
Input input() to take data from user
Output print() to display data