Search This Blog

Showing posts with label Python Basics. Show all posts
Showing posts with label Python Basics. Show all posts

Monday, 21 July 2025

Python Basics: Dictionaries

 

What Is a Dictionary in Python?

A dictionary is a built-in data type that lets you store data in key-value pairs. Instead of accessing values by a number (like in a list), you access them by their label.

Syntax



When to Use Dictionaries

Use them when:

  • When working with Json data
  • This works well with data returned from Database / backend
  • You want fast lookup by a unique key
  • You're organizing settings, user profiles, configurations, etc

Common methods used with dictionaries




See the following code and try it yourself






Thursday, 17 July 2025

Python Functions using global and comprehension

 In the earlier article, we have seen the use of global, and the different flavors of functions in Python.

Let''s combine them and use comprehensions to reduce the total lines of code.

What are comprehensions in Python?

In Python, comprehensions are a concise and expressive way to create new sequences—like lists, sets, dictionaries, or generators—from existing iterables. They let you write elegant one-liners that would otherwise require multiple lines of loops and conditionals.

Comprehensions work like the Arabic language, read from right-to-left

🔍 Observations

  • In the above screenshot, in modify(old_emp, new_emp), the execution happens in a sequential top-to-down manner.
  • In modify_smart(old,new), the same execution happens in one-line from right to left

Let's highlight the comprehension in the modify_smart()

🔍 Investigating the above code: 

  • for emp in company_employees: Iterate over each employee name in the list.
  • new if emp == old else emp: If the employee name matches old, replace it with new; otherwise, keep it unchanged.

Wednesday, 16 July 2025

Python Basics - Lists in Python

 A list is an ordered, mutable collection used to store multiple items in a single variable.

This is how a list looks like



Key Features

  • Ordered: Items remain in the order they were added.
  • Mutable: You can add, remove, or change elements.
  • Heterogeneous: Different data types in one list? No problem.
  • Iterable: Perfect for loops and comprehensions.

When to Use Lists

  • Dynamic datasets (to-do items, animation frames, user input)
  • Collections where items may change
  • GUI elements that update often
  • Grouping related values together

List Of Operations





Comparing Tuples Vs List








Python Basics - Understanding Tuples in Python: The Immutable Sidekick

 A tuple is an ordered, immutable collection of items. Once created, you can’t modify its contents—which makes it ideal for data that shouldn't change.

This is how a tuple is created

Unpacking a tuple





Key Features

  • Ordered: Elements maintain the order you assign.
  • Immutable: Unlike lists, you can't alter, add, or remove items.
  • Heterogeneous: Can hold different data types.
  • Hashable: Useful as dictionary keys or in sets (if all elements are hashable).

When to Use Tuples

  • To represent fixed collections, like RGB colors, coordinates, or database records.
  • As function return values to return multiple items cleanly.
  • When working with dictionaries as keys (since lists can't be keys).

Behind the Scenes

Tuples are lighter on memory and faster to access, making them perfect for performance-critical Python apps. Integrating tuples smartly can help you write cleaner and more efficient code.


Comparison between the collection types : List Vs Tuples