Wednesday, 23 July 2025

Python Tuple , Set and Dict

 Tuple Methods :


t = (1, 2, 3)


t.count(2)            # 1

t.index(3)            # 2


#Set methods :

s = {1, 2, 3}

s.add(4)              # {1, 2, 3, 4}
s.remove(2)           # Removes 2
s.discard(5)          # Safe remove
s.pop()               # Removes random item
s.clear()             # Empties the set
s.update([5, 6])      # Add multiple

# Set operations:

a = {1, 2, 3}
b = {3, 4, 5}

a | b                 # Union
a & b                 # Intersection
a - b                 # Difference
a ^ b                 # Symmetric difference

a.issubset(b)
a.issuperset(b)
a.isdisjoint(b)

#Dict :

d = {'a': 1, 'b': 2}

d['a']                # 1
d.get('c', 0)         # 0 (default)
d.keys()              # dict_keys(['a', 'b'])
d.values()            # dict_values([1, 2])
d.items()             # dict_items([('a', 1), ('b', 2)])

d.update({'c': 3})    # Adds/updates keys
d.pop('a')            # Removes and returns value
d.popitem()           # Removes last inserted pair
d.clear()             # {}

# Dict comprehension:
{x: x**2 for x in range(5)}  # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


Python List Methods

 List Methods :


lst = [1, 2, 3]


lst.append(4)         # [1, 2, 3, 4]

lst.extend([5, 6])    # [1, 2, 3, 4, 5, 6]

lst.insert(1, 10)     # [1, 10, 2, 3, ...]

lst.remove(10)        # Removes first match

lst.pop()             # Removes last and returns it

lst.pop(1)            # Removes and returns index 1

lst.clear()           # []

lst.index(3)          # Returns index of 3

lst.count(2)          # Count of 2s

lst.sort()            # In-place sort

lst.reverse()         # In-place reverse

sorted(lst)           # Returns sorted copy


Python Integer methods

 Integer methods


x = 42

float(x)              # 42.0

int(3.14)             # 3

abs(-5)               # 5

round(3.75)           # 4

pow(2, 3)             # 8

divmod(9, 4)          # (2, 1)

bin(10)               # '0b1010'

hex(255)              # '0xff'

oct(8)                # '0o10'


Python String methods

 Methods : 

s = "hello world"


s.upper()             # 'HELLO WORLD'

s.lower()             # 'hello world'

s.capitalize()        # 'Hello world'

s.title()             # 'Hello World'

s.strip()             # Remove whitespace

s.lstrip()            # Left-strip

s.rstrip()            # Right-strip

s.replace("l", "x")   # 'hexxo worxd'

s.split(" ")          # ['hello', 'world']

s.join(['a', 'b'])    # 'a b'

s.startswith("he")    # True

s.endswith("ld")      # True

s.find("o")           # First index (4)

s.rfind("o")          # Last index (7)

s.count("l")          # 3

s.isalpha()           # False

s.isdigit()           # False

s.isalnum()           # False

s.islower()           # True

s.isupper()           # False

s.swapcase()          # 'HELLO WORLD'

s.zfill(12)           # '00hello world'

s.center(20, "-")     # '----hello world-----'


Python Tuple , Set and Dict

 Tuple Methods : t = (1, 2, 3) t.count(2)            # 1 t.index(3)            # 2 #Set methods : s = {1, 2, 3} s.add(4)              # {1, ...