Python provides powerful built-in data types and operations. Let’s explore:

  1. Sequences in Python
  2. Number Operations
  3. String Operations
  4. Collections (Lists, Tuples, Sets, Dictionaries)

1️⃣ Sequences in Python

A sequence is an ordered collection of items. Python has several built-in sequence types:

Sequence Type Mutable? Indexed? Example
List (list) ✅ Yes ✅ Yes [1, 2, 3]
Tuple (tuple) ❌ No ✅ Yes (1, 2, 3)
String (str) ❌ No ✅ Yes "hello"
Range (range) ❌ No ✅ Yes range(5)0,1,2,3,4

Example:

my_list = [10, 20, 30]  # List
my_tuple = (10, 20, 30)  # Tuple
my_string = "Python"  # String
my_range = range(3)  # Range (0,1,2)

print(my_list[1])  # Output: 20
print(my_tuple[2])  # Output: 30
print(my_string[0])  # Output: P
print(list(my_range))  # Output: [0, 1, 2]

2️⃣ Number Operations in Python

✔️ Numeric Types in Python

Type Description Example
int Whole numbers 5, -100, 2000
float Decimal numbers 3.14, -2.5, 0.01
complex Complex numbers 3 + 4j, -1 - 2j

Example:

a = 10      # Integer
b = 3.14    # Float
c = 2 + 3j  # Complex number

print(type(a))  # Output: <class 'int'>
print(type(b))  # Output: <class 'float'>
print(type(c))  # Output: <class 'complex'>

✔️ Mathematical Operations

Operator Description Example
+ Addition 5 + 3 = 8
- Subtraction 10 - 3 = 7
* Multiplication 4 * 2 = 8
/ Division 7 / 2 = 3.5
// Floor Division 7 // 2 = 3
% Modulus (Remainder) 7 % 2 = 1
** Exponentiation 2 ** 3 = 8

Example:

x = 10
y = 3

print(x + y)  # Output: 13
print(x // y)  # Output: 3 (Floor Division)
print(x ** y)  # Output: 1000 (10^3)

✔️ Built-in Math Functions

Function Description Example
abs(x) Absolute value abs(-5) → 5
pow(x, y) Power calculation pow(2, 3) → 8
round(x, n) Rounds to n decimal places round(3.14159, 2) → 3.14
min(a, b, c) Smallest number min(4, 9, 1) → 1
max(a, b, c) Largest number max(4, 9, 1) → 9

Example:

print(abs(-10))  # Output: 10
print(pow(3, 2))  # Output: 9
print(round(3.456, 2))  # Output: 3.46

3️⃣ String Operations in Python

A string is an immutable sequence of characters.

Example:

s = "Hello, Python!"
print(s[0])  # Output: H (Indexing)
print(s[-1])  # Output: ! (Negative Indexing)
print(s[0:5])  # Output: Hello (Slicing)

✔️ Common String Methods

Method Description Example
upper() Converts to uppercase "hello".upper()"HELLO"
lower() Converts to lowercase "HELLO".lower()"hello"
strip() Removes whitespace " hello ".strip()"hello"
replace(a, b) Replaces a with b "apple".replace('a', 'o') → "opple"
split(x) Splits string by x "a,b,c".split(',')['a', 'b', 'c']

Example:

text = "  Python is Fun  "
print(text.strip())  # Output: "Python is Fun"
print(text.replace("Python", "Java"))  # Output: "  Java is Fun  "
print(text.split())  # Output: ['Python', 'is', 'Fun']

4️⃣ Collections in Python

✔️ Lists (Mutable, Ordered)

Example:

my_list = [10, 20, 30]
my_list.append(40)  # Adds element
my_list.remove(20)  # Removes element
print(my_list)  # Output: [10, 30, 40]

✔️ Tuples (Immutable, Ordered)

Example:

my_tuple = (1, 2, 3)
print(my_tuple[0])  # Output: 1

✔️ Sets (Mutable, Unordered, Unique Elements)

Example:

my_set = {1, 2, 3, 2}
my_set.add(4)  # Adds element
print(my_set)  # Output: {1, 2, 3, 4}

✔️ Dictionaries (Key-Value Pairs)

Example:

my_dict = {"name": "Alice", "age": 25}
print(my_dict["name"])  # Output: Alice

🔹 Summary Table

Concept Key Points
Sequences Ordered collections (lists, tuples, strings, ranges)
Numbers Integer, float, complex, mathematical operations
Strings Immutable, indexing, slicing, built-in methods
Collections Lists (mutable), Tuples (immutable), Sets (unique), Dictionaries (key-value pairs)

Would you like more examples on any specific topic? 🚀


Leave a Reply

Your email address will not be published. Required fields are marked *