Skip to main content

Posts

Featured

Advanced Python - VI

Comprehensions in Python Comprehensions in Python are concise ways to create new sequences like lists or dictionaries based on existing iterables. They transform an iterative for-loop into a single readable expression. Advantages of Comprehensions  More time-efficient and space-efficient than regular loops Require fewer lines of code Turn iterative logic into a clean, readable “formula” Syntax of List Comprehension new_list = [expression(element) for element in iterable if condition] expression(element) → the operation applied to each element iterable → the collection you loop over (list, string, etc.) condition → optional filter that selects which elements to include Examples of List Comprehensions Example 1 – Squaring Numbers # Square each number from 1 to 5 squares = [n ** 2 for n in range(1, 6)] print(squares) Output: [1, 4, 9, 16, 25] Example 2 – Filtering Words # Keep words longer than 3 letters words = ["hi", "hello", "bye...

Latest posts

Advance Python - V

Advanced Python - IV

Advance Python - III

Advance Python - II

Advanced Python - I

String comparison

String operation - II

String operation - I

Escape sequencing

Strings in python