Featured Post

How to Read a CSV File from Amazon S3 Using Python (With Headers and Rows Displayed)

Image
  Introduction If you’re working with cloud data, especially on AWS, chances are you’ll encounter data stored in CSV files inside an Amazon S3 bucket . Whether you're building a data pipeline or a quick analysis tool, reading data directly from S3 in Python is a fast, reliable, and scalable way to get started. In this blog post, we’ll walk through: Setting up access to S3 Reading a CSV file using Python and Boto3 Displaying headers and rows Tips to handle larger datasets Let’s jump in! What You’ll Need An AWS account An S3 bucket with a CSV file uploaded AWS credentials (access key and secret key) Python 3.x installed boto3 and pandas libraries installed (you can install them via pip) pip install boto3 pandas Step-by-Step: Read CSV from S3 Let’s say your S3 bucket is named my-data-bucket , and your CSV file is sample-data/employees.csv . ✅ Step 1: Import Required Libraries import boto3 import pandas as pd from io import StringIO boto3 is...

15 Python Tips : How to Write Code Effectively

 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.

 

Top tips

 

Python Tips for Effective Coding


1. Code Readability and PEP 8


  •  Always aim for clean and readable code by following PEP 8 guidelines.
  •  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly.

2. Use List Comprehensions


  • List comprehensions are concise and often faster than regular for-loops.
  • Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value.


3. Take Advantage of Python’s Built-in Libraries


  •  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.
  •   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists.


4. Use enumerate Instead of Range


    When you need both the index and the value in a loop, enumerate is a more Pythonic approach:



    for i, value in enumerate(my_list):
        print(f"Index: {i}, Value: {value}")



5. Use F-strings for String Formatting



    F-strings are more readable and faster than format() or % for formatting strings.
    Example: name = "Alice"; print(f"Hello, {name}!")



6. Learn About Lambda Functions and map, filter, reduce

Lambdas are useful for small, anonymous functions, while map, filter, and reduce (from functools) can make functional programming more concise.

 

Example: squared = list(map(lambda x: x**2, range(10))

 

7. Unpack Multiple Variables

Use multiple assignments to unpack values directly. This is especially helpful when working with tuples or lists:



    x, y, z = (1, 2, 3)

 

8. Handle Exceptions Properly


 Use try...except blocks to catch and handle exceptions gracefully. Avoid using broad exception types like Exception unless absolutely necessary.
    Example:



    try:
        result = 10 / 0
    except ZeroDivisionError:
        print("You can't divide by zero!")

 

9. Leverage Generators for Large Data


    Generators (yield) allow you to iterate over large datasets without consuming too much memory.
    Example:



    def my_generator():
        for i in range(1000):
            yield i

 

10. Use with Statements for File Handling and Resource Management


    The with statement automatically handles closing files or releasing resources.
    Example:



    with open('file.txt', 'r') as file:
        data = file.read()

 

11. Use Default Dicts and Sets for Cleaner Code


 collections.defaultdict can help manage dictionary keys without checking if the key already exists. Example:

     from collections import defaultdict
    dd = defaultdict(list)
    dd['key'].append('value')

 

12. Use Docstrings for Documentation

Write docstrings for functions and classes to make your code understandable for others (and your future self).

 

13. Keep Functions Short and Focused

A function should ideally do one thing and do it well. It improves readability and makes debugging easier.

 

14. Avoid Mutable Default Arguments


 Using mutable default arguments (e.g., lists or dictionaries) can lead to unexpected behavior.
 Instead of this:

   

def func(my_list=[]):
    my_list.append(1)

Do this:



    def func(my_list=None):
        if my_list is None:
            my_list = []



15. Profile and Optimize Only When Necessary

Use tools like cProfile and timeit to check performance.

 

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

5 SQL Queries That Popularly Used in Data Analysis

Big Data: Top Cloud Computing Interview Questions (1 of 4)