Programming Articles

Page 554 of 2547

time.process_time() function in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 2K+ Views

The time.process_time() function in Python returns the sum of system and user CPU time consumed by the current process. Unlike time.time(), it excludes time spent sleeping and focuses only on actual processing time. Syntax time.process_time() This function returns a float value representing the CPU time in seconds. Basic Example Here's how to get the current process time ? import time # Get current process time current_time = time.process_time() print(f"Current process time: {current_time} seconds") Current process time: 0.015625 seconds Measuring Execution Time The most common ...

Read More

time.perf_counter() function in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

In this tutorial, we are going to learn about the time.perf_counter() method in Python. This function is part of the time module and provides the highest available resolution to measure a short duration. The method time.perf_counter() returns a float value representing time in seconds. It includes time elapsed during sleep and is system-wide, making it ideal for measuring elapsed time in code execution. Basic Usage Let's start with a simple example to see how perf_counter() works ? import time # Get current performance counter value print("Current time:", time.perf_counter()) Current time: 263.3530349 ...

Read More

The most occurring number in a string using Regex in python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 287 Views

In this tutorial, we will write a regex that finds the most occurring number in a string using Python. We'll use the re module for pattern matching and collections.Counter for frequency counting. Steps to Find the Most Occurring Number Import the re and collections modules Initialize the string containing numbers Find all numbers using regex and store them in a list Find the most occurring number using Counter from collections module Example Here's how to implement this solution ? # importing the modules import re import collections # initializing the string ...

Read More

Taking multiple inputs from user in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 1K+ Views

In this tutorial, we are going to learn how to take multiple inputs from the user in Python. The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data. Taking Multiple String Inputs Let's take multiple strings from the user using split() ? # taking the input from the user strings = input("Enter multiple names space-separated:- ") # splitting the data strings = strings.split() # printing the data print(strings) The output of the above code is ? ...

Read More

Take Matrix input from user in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 9K+ Views

In this tutorial, we will learn how to take matrix input from the user in Python. There are two common approaches: taking individual elements one by one, or taking entire rows with space-separated values. Method 1: Taking Individual Elements This method asks the user to input each matrix element individually ? Example # initializing an empty matrix matrix = [] # taking 2x2 matrix from the user for i in range(2): # empty row row = [] for j in range(2): ...

Read More

Sum of list (with string types) in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 3K+ Views

In this tutorial, we'll write a program that adds all numbers from a list containing mixed data types. The list may contain numbers in string or integer format, along with non-numeric strings. Problem Statement Given a list with mixed data types, we need to sum only the numeric values (both integers and numeric strings) while ignoring non-numeric strings ? Input random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020] Expected Output 4051 Solution Approach Follow these steps to solve the problem: Initialize the list with mixed data ...

Read More

Structuring Python Programs

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 534 Views

In this tutorial, we are going to see some best practices for structuring Python programs. Following these conventions makes your code more readable and maintainable. Use Consistent Indentation Python uses indentation to define code blocks. Use 4 spaces per indentation level as recommended by PEP 8. Avoid mixing tabs and spaces ? def calculate_area(length, width): # 4 spaces for indentation if length > 0 and width > 0: area = length * width ...

Read More

struct module in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

The struct module in Python converts native Python data types into strings of bytes and vice versa. It's a built-in module that uses C-style format characters to specify data types and their binary representation. Format Characters The struct module uses format characters similar to C language data types ? Data Type Format Character Description int i 4-byte signed integer char c 1-byte character string s char[] (requires length prefix) float f 4-byte floating point struct.pack() - Converting to Bytes The struct.pack() method converts ...

Read More

string.whitespace in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 473 Views

In this tutorial, we are going to learn about string.whitespace in Python. The string.whitespace constant is pre-defined in the string module and contains all ASCII whitespace characters: space, tab, linefeed, return, formfeed, and vertical tab. What is string.whitespace? The string.whitespace is a string constant that contains all characters considered as whitespace in ASCII. This includes: Space character (' ') Tab character ('\t') Newline character ('') Carriage return ('\r') Form feed ('\f') Vertical tab ('\v') Basic Example Let's see what string.whitespace contains ? import string print("Content of string.whitespace:") print(repr(string.whitespace)) print("Length:", len(string.whitespace)) ...

Read More

string.punctuation in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

In this tutorial, we are going to learn about the string.punctuation constant in Python. The string.punctuation is a pre-defined string constant in Python's string module that contains all ASCII punctuation characters. We can use it for text processing, password generation, and data validation. What is string.punctuation? The string.punctuation constant contains all printable ASCII punctuation characters as a single string ? import string # Display the punctuation string print("Punctuation characters:") print(string.punctuation) print(f"Total characters: {len(string.punctuation)}") Punctuation characters: !"#$%&'()*+, -./:;?@[\]^_`{|}~ Total characters: 32 Practical Examples Removing Punctuation from Text Remove all ...

Read More
Showing 5531–5540 of 25,466 articles
« Prev 1 552 553 554 555 556 2547 Next »
Advertisements