🔧 Essential Python Built-in Functions You Should Know

 🔧 Essential Python Built-in Functions You Should Know

  1. len() – Get the length

  • Returns the number of elements in strings, lists, tuples, dictionaries, etc.

  • Example:

len("Python") # Output: 6 len([10, 20, 30]) # Output: 3
  1. range() – Generate a sequence of numbers

  • Commonly used in loops and iterations.

  • Example:

for i in range(4): print(i) # Output: 0 1 2 3
  1. type() – Check the data type

  • Example:

type(3.14) # Output: <class 'float'> type([1,2,3]) # Output: <class 'list'>
  1. enumerate() – Get index and value while looping

  • Example:

fruits = ['apple', 'banana'] for idx, fruit in enumerate(fruits): print(idx, fruit) # Output: # 0 apple # 1 banana
  1. zip() – Combine multiple iterables

  • Example:

numbers = [1, 2] letters = ['a', 'b'] list(zip(numbers, letters)) # Output: [(1, 'a'), (2, 'b')]
  1. map() – Apply a function to each item in an iterable

  • Example:

list(map(str.upper, ['x', 'y'])) # Output: ['X', 'Y']
  1. filter() – Select items based on a condition

  • Example:

list(filter(lambda x: x%2==0, [1,2,3,4])) # Output: [2, 4]
  1. sorted() – Sort any iterable

  • Example:

sorted([5, 1, 3]) # Output: [1, 3, 5]
  1. reversed() – Reverse an iterable

  • Example:

list(reversed([10, 20, 30])) # Output: [30, 20, 10]
  1. any() / all() – Logical checks

  • any() returns True if any element is True

  • all() returns True only if all elements are True

  • Example:

any([False, True, False]) # Output: True all([True, True, True]) # Output: True

Comments

Popular posts from this blog

🔁 Top 30 Python List Methods You Should Know

Python Project 7: Read & Write JSON Files

✅ Python – Quick Start Guide for Beginners