Wednesday, 5 February 2025

7 Python Power Moves: Cool Tricks I Use Every Day

 


1. Unpacking Multiple Values

Unpacking simplifies handling multiple values in lists, tuples, or dictionaries.


a, b, c = [1, 2, 3]
print(a, b, c) # Output: 1 2 3

Explanation:

  • You assign values from the list [1, 2, 3] directly to a, b, and c. No need for index-based access.

2. Swap Two Variables Without a Temp Variable

Python allows swapping variables in one line.


x, y = 5, 10
x, y = y, x
print(x, y) # Output: 10 5

Explanation:

  • Python internally uses tuple packing/unpacking to achieve swapping in one line.

3. List Comprehension for One-Liners

Condense loops into one-liners using list comprehensions.


squares = [x*x for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]

Explanation:

  • Instead of a loop, you create a list of squares with concise syntax [x*x for x in range(5)].

4. Use zip to Pair Two Lists

Combine two lists element-wise into tuples using zip.


names = ["Alice", "Bob"]
scores = [85, 90] pairs = list(zip(names, scores))
print(pairs) # Output: [('Alice', 85), ('Bob', 90)]

Explanation:

  • zip pairs the first elements of both lists, then the second elements, and so on.

5. Dictionary Comprehension

Quickly create dictionaries from lists or ranges.


squares_dict = {x: x*x for x in range(5)}
print(squares_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Explanation:

  • {key: value for item in iterable} creates a dictionary where the key is x and the value is x*x.

6. Use enumerate to Track Indices

Get the index and value from an iterable in one loop.


fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits): print(index, fruit) # Output: # 0 apple # 1 banana
# 2 cherry

Explanation:

  • enumerate returns both the index and the value during iteration.

7. Use *args and **kwargs in Functions

Handle a variable number of arguments in your functions.


def greet(*args, **kwargs):
for name in args: print(f"Hello, {name}!") for key, value in kwargs.items(): print(f"{key}: {value}") greet("Alice", "Bob", age=25, location="NYC") # Output: # Hello, Alice! # Hello, Bob! # age: 25
# location: NYC

Explanation:

  • *args: Collects positional arguments into a tuple.
  • **kwargs: Collects keyword arguments into a dictionary.

These tricks save time and make your code concise. Which one is your favorite? 😊

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (186) C (77) C# (12) C++ (83) Course (67) Coursera (246) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (141) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (29) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (76) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (991) Python Coding Challenge (428) Python Quiz (71) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)