Search This Blog

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



Python Basics : Handling Exceptions - Try-Except-Finally

 In Python exceptions can be handled using the try-except-finally block.

This is the most basic way of handling exceptions. The purpose of the try-except-finally block is "Not to expose the internal details of the exception, But present a friendly error message to the End User"

For example, a friendly message as shown here for an internal error of  "Insert into database db-name -> table-name failed View Stacktrace : ....."

⚠️ Something went wrong. Please contact Support at some-email@some-company.


In an enterprise application, try-except-finally are not sufficient.

For every exception handling scenario, there must be two perspectives that should be considered.

1. End user perspective: This is the user who is using your application through a device / browser. He need not know the technicalities and internal workings of this application.

2. The Application Support Team Members: These are the technical staff who would be supporting the end-user in case of any issues faced. This team must have a relevant idea of the internal workings of the product.

Hence, Exception handling can be done using two best practices.

1. For the End-user: We can use the Try-Except-Finally block

2. For the Application Support Team Members: We shall have an additional strategy of using Log files / Log tables


Here, in this article we will understand the flavors of  try-except-finally to present a user-friendly error.

Syntax:


try:
    your code here
except <Error Type>:
    your code here
except <Error Type>:
    your code here
finally:
    your cleanup / closure code



FLAVORS:

Single Except block: Here the error type is Value Error. The base Error type class is known as Exception. To capture errors like key-stroke errors etc, BaseException class can also be used


 Multiple Except Blocks: To catch multiple errors, and return different friendly messages.


Except with Tuple: To return a standard message for multiple error types, add the error types to a tuple.

Catch all types of Exceptions: Use the class Exception / BaseException








Python Basics : Functions in Python

 Functions are a means to aid modular programming where reusable chunks of code are grouped together, to be created once and invoked several times.

This is the first step to modular programming which then leads to creating modules, classes and packages for re-usability. This practice is common across all languages, and Python is no exception to this rule.

This article covers flavors of creating functions in the traditional way. The same functions can be created in a language agnostic way using lambdas. This will be covered in a later article.

Syntax of a function is as follows

def function_name (arg1, arg2 ,...):

       your code here...






Tuesday, 15 July 2025

Python Basics: Control Statements

 

Control statements alter the flow of program execution in any language.

Here, we shall be exploring the if-elif-else structure.


Syntax:
 if  :
     your code..
 elif  :
    your code...
 else :
     your code...



Using the match statement

The match statement is used when there are multiple cases and need to be made more readable and maintainable.

Case _: indicates the default case / else case. This case will execute when none of the other cases are matching the condition

Syntax:
 match  
        case :
            your code ...
        case :
            your code ...
        case _:
            your code for else case

Example: 








Python Basics: Getting inputs from the end user

 This post explains how to get inputs from the end user, and the flavours of inputs that can be taken.

The predominant function here is: input() 

To get sensitive data like passwords, which should not be visible to the end user, we use the getpass package and getpass() function






Python Basics: Different ways to print an output to the console

To continue with this step, please complete the step of creating a project in PyCharm. The step by step process is shown here: Create a project in Pycharm

Syntaxes of Python are similar to english language. It is readable, simple and has minimal syntax

This post explains how to create a basic python program.

The following program shows different ways to print a value to the output console. The function used is print()