Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Asif Rahaman
Page 3 of 6
Python - Get Function Signature
Understanding function signatures in Python is essential for analyzing function parameters, data types, and default values. The inspect module provides powerful methods like signature() and getfullargspec() to retrieve detailed function information programmatically. Using inspect.signature() Method The inspect.signature() method provides comprehensive access to function parameter details including annotations, default values, and parameter kinds ? Example import inspect def my_function(arg1: int, arg2: str = "default", *args: int, **kwargs: float) -> bool: pass signature = inspect.signature(my_function) params = signature.parameters for name, param in params.items(): print(f"Parameter: {name}") ...
Read MorePython - Get Even indexed elements in Tuple
Getting even-indexed elements from a tuple means extracting elements at positions 0, 2, 4, 6, etc. Python provides several efficient approaches to accomplish this task using slicing, list comprehension, and functional programming methods. Using range() with List Conversion We can iterate through the tuple using range() with a step of 2, collect even-indexed elements in a list, then convert back to a tuple ? def get_even_elements(t): even_elements = [] for i in range(0, len(t), 2): even_elements.append(t[i]) ...
Read MoreHow to Replace the Kth word in a String using Python?
Replacing the kth word in a string is a common string manipulation task in Python. This involves identifying the word at a specific position (k) and substituting it with a new word. Python offers several approaches using built-in methods like split(), list comprehension, and regular expressions. Using Split Method and Indexing The simplest approach uses split() to convert the string into a list of words, then directly accesses the kth word using indexing. Syntax string.split(delimiter) The split() method divides a string based on a delimiter (space by default) and returns a list of ...
Read MoreHow to Get the list of Running Processes using Python?
The operating system runs with hundreds of tasks/processes at a single time. As a Python developer, this is important because often while dealing with programs that consume huge amounts of memory, we may want to delete some unimportant tasks. This article will explore how to get the running process list using Python. Using psutil Module The psutil module is a powerful cross−platform library for system monitoring and process management in Python. It provides a convenient and consistent API to access system−related information, such as CPU usage, memory utilization, disk usage, network statistics, etc. Being cross−platform, the same code ...
Read MoreHow to Get the Last N characters of a String in Python
Getting the last N characters from a string is a common task in Python programming. This is useful for file extensions, data validation, text processing, and many other applications. Python provides several approaches to extract the last N characters efficiently. Using String Slicing (Recommended) String slicing is the most Pythonic and efficient method to get the last N characters. It uses negative indexing where -n: starts from the nth character from the end. Example def get_last_n_characters(text, n): return text[-n:] text = "Hello, world!" n = 5 result = get_last_n_characters(text, n) ...
Read MoreHow to Get the First Element in List of Tuples in Python?
When working with a list of tuples in Python, extracting the first element from each tuple is a common task. Python provides several approaches including loops, list comprehension, map() function, unpacking, and the zip() method. Using For Loop A simple loop iterates through each tuple and accesses the first element using index [0] − def get_first_element_using_loop(tuples_list): first_elements = [] for item in tuples_list: first_elements.append(item[0]) return first_elements fruits = [ ('Apple', 5, ...
Read MoreGet the List of Files in a Directory Sorted by Size Using Python.
Getting a list of files in a directory sorted by size is a common task in Python file operations. Python provides several approaches using the os module, glob module, and sorting techniques like lambda functions and the operator module. Using os.listdir() with Operator Module The os module provides functions to interact with the operating system, while the operator module offers built-in operators for cleaner code ? import os import operator # Create sample files for demonstration os.makedirs('files', exist_ok=True) with open('files/small.txt', 'w') as f: f.write('Small file') with open('files/medium.txt', 'w') as f: ...
Read MoreGet Month from year and weekday using Python
Finding months that start with a specific weekday is a common task in calendar applications. Python provides several approaches using the calendar and datetime modules to determine which month in a given year starts with your desired weekday. Using the Calendar Module The calendar module provides useful functions for working with calendars and dates. It offers methods to generate calendars, calculate weekdays, and perform calendar-related operations without worrying about leap years. Example The following function iterates through all 12 months and checks which month starts with the specified weekday ? import calendar def ...
Read MoreGet Hardware and System information using the Python Platform Module.
Python is a versatile language that was built as a general-purpose scripting language. Hence a lot of automation tasks, along with scripting, can be done. Getting the system information becomes an important task in many applications such as machine learning, deep learning, etc., where hardware plays a crucial role. Python provides several methods to gather information about the operating system and hardware. Getting Overall System Configuration The platform module in Python provides a way to obtain the overall system configuration in a platform-independent manner. So we can run the same methods to get the system configuration without knowing ...
Read MoreGet Confirmed, Recovered, Deaths cases of Corona around the globe using Python
The COVID-19 pandemic has impacted billions of lives worldwide, creating widespread concern among people. Several applications were built to track and analyze accurate information about deaths, recovered cases, and confirmed cases. Fetching and analyzing this information is crucial for developers building pandemic-related applications. In this article, we will explore three different methods to retrieve statistical data about COVID-19 cases using Python. Method 1: Using APIs APIs (Application Programming Interfaces) enable software applications to interact with each other by defining protocols for data exchange and functionality access. Web APIs, often based on HTTP, allow developers to access data and ...
Read More