Python Articles

Page 173 of 855

How to get the file name from the file path in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 9K+ Views

When working with file paths in Python, you often need to extract just the filename from a complete file path. Python provides several methods to accomplish this task using different modules. Method 1: Using os.path.basename() The most common and straightforward approach is using the os.path.basename() function, which returns the final component of a file path ? import os # Input file path file_path = 'C:/Users/Documents/tutorial.pdf' # Extract filename using os.path.basename() filename = os.path.basename(file_path) print("The filename is:", filename) The filename is: tutorial.pdf Method 2: Using os.path.split() The split() function ...

Read More

How to Get a Negation of a Boolean in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 7K+ Views

In this article, we will learn how to get a negation of a Boolean in Python. In Python, the Boolean datatype is a built-in data type that represents True or False values. For example, 520 is False. Boolean negation means converting True to False and False to True. The following are the various methods to accomplish this task ? Using the "not" operator Using the "~" operator Using operator module Using arithmetic subtraction from 1 Using NumPy functions Method 1: Using the "not" Operator The not operator is the most common and pythonic ...

Read More

How to generate IP addresses from a CIDR address using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 5K+ Views

In this article, we will learn how to generate IP addresses from a CIDR (Classless Inter-Domain Routing) address using Python's built-in ipaddress module. CIDR notation represents a network address and its subnet mask, allowing us to define IP address ranges efficiently. What is CIDR Notation? CIDR notation combines an IP address with a prefix length (e.g., 192.168.1.0/24). The number after the slash indicates how many bits are used for the network portion, determining the range of available host addresses. Using IPv4Network The ipaddress.ip_network() function creates a network object from CIDR notation, allowing us to iterate through ...

Read More

How to find Profit or loss using Python when CP of N items is equal to SP of M

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 658 Views

In this article, we will learn a Python program to find the profit or loss when the cost price (CP) of N items is equal to the selling price (SP) of M items. When CP of N items equals SP of M items, we can calculate profit or loss percentage using a mathematical relationship between these quantities. Understanding the Problem Given that Cost Price of N items = Selling Price of M items, we need to determine whether there's a profit or loss and calculate the percentage. What is Cost Price (CP)? Cost price is ...

Read More

How to Determine Last Friday's Date using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 4K+ Views

In this article, we will learn how to determine last Friday's date using Python. This is useful for scheduling, reporting, and date-based calculations in business applications. Methods Used The following are the various methods to accomplish this task − Using datetime module Using dateutil package Method 1: Using Datetime Module This approach uses Python's built-in datetime module to calculate the previous Friday by determining the current weekday and counting backwards. Algorithm Following are the steps to perform this task − Import datetime and timedelta from datetime module Create a ...

Read More

How to detect whether a Python variable is a function?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 6K+ Views

In this article, we will learn how to detect whether a Python variable is a function. Sometimes, it's essential to figure out whether a Python variable is a function, especially when working with large codebases or dynamically generated code. Methods to Check if a Variable is a Function The following are the methods to check whether a Python variable is a function − Using the built-in callable() function Using the isfunction() of the inspect module Using the type() function Using the built-in hasattr() function Using the isinstance() function Using the callable() Function The ...

Read More

How to create a dictionary of sets in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 3K+ Views

A dictionary of sets in Python stores sets as values for dictionary keys, automatically eliminating duplicates. This structure is useful for grouping unique items under different categories. Using Dictionary Literals The simplest way is to create a dictionary with sets as values using curly braces ? # Creating a dictionary with sets as values employee_data = { 'Employee_ID': {10, 11, 12, 14, 15}, 'Employee_Age': {25, 30, 40, 35, 28} } print("Dictionary of sets:") print(employee_data) Dictionary of sets: {'Employee_ID': {10, 11, 12, 14, 15}, 'Employee_Age': ...

Read More

How to capture SIGINT in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 1K+ Views

In this article, we will learn how to capture SIGINT in Python and what has to be done after capturing it. SIGINT is a signal sent when the user presses Ctrl+C (or Ctrl+F2 on Windows) to interrupt a running program. When the signal module receives signals, it performs specific actions. It can capture the user's keyboard interruption through SIGINT and allow you to define custom behavior instead of just terminating the program. Required Modules The following modules are needed for signal handling in Python − Signal Module The signal module provides mechanisms to handle asynchronous ...

Read More

How to Align Text Strings using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 36K+ Views

In this article, we will learn how to align text strings using Python. Python provides multiple ways to align text: f-strings with alignment specifiers and built-in string methods like ljust(), rjust(), and center(). Python's text alignment feature helps create well-formatted output. When data varies in length, proper alignment makes output more readable and professional-looking. You can specify left, right, or center alignment with a reserved width for consistent formatting. F-String Alignment Syntax F-strings use alignment symbols followed by width numbers ? − Right alignment ^ − Center alignment Left Alignment using F-strings ...

Read More

Find the maximum element of each row in a matrix using Python

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 3K+ Views

In this article, we will learn Python programs to find the maximum element of each row in a matrix. We'll explore three efficient approaches to solve this common problem. Assume we have taken an NxN input matrix. We will now find the maximum element of each row in an input matrix using the below methods. Methods Used The following are the various methods to accomplish this task − Using Nested For Loops Using max() function Using map() and lambda functions Method 1: Using Nested For Loops Algorithm Following are the steps ...

Read More
Showing 1721–1730 of 8,549 articles
« Prev 1 171 172 173 174 175 855 Next »
Advertisements