Hafeezul Kareem

Hafeezul Kareem

259 Articles Published

Articles by Hafeezul Kareem

Page 15 of 26

time.perf_counter() function in Python

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

In this tutorial, we are going to learn about the time.perf_counter() method.The method time.perf_counter() returns a float value of time in seconds. Let's see anExample# importing the time module import time # printing the time print(time.perf_counter())OutputIf you run the above code, then you will get the following result.263.3530349We can use the time.perf_counter() method to find the execution time of a program. Let's see an example.Example# importing the time module import time # program to find the prime number def is_prime(number):    for i in range(2, number):       if number % i == 0:          return ...

Read More

time.process_time() function in Python

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

In this tutorial, we are going to learn about the time.process_time() method.The method time.process_time() will return a float value of time in seconds of a system and user CPU time of the current process.Example# importing the time module import time # printing the current process time print(time.process_time()) 1.171875OutputIf you run the above code, then you will get the similar result as follows.1.171875Let's say we have a process that prints from in the given range. Let's find the time for that process. Understand the following code and run it.Example# importing the time module import time # program to find the prime ...

Read More

Program to print its script name as output in Python

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

In this tutorial, we are going to write a program that prints the name of the Python script file. We can find the script name using the sys module.The sys module will store all the command line arguments of python command in the sys.argv list. The first element in the list is the script name. We can extract it from that list. Python makes it easy.Let's see the steps involved in the program.Import the sys module.Now, print the first element of the sys.argv list.That's it. You got the script name.ExampleLet's see it practically.# importing the sys module import sys # ...

Read More

Python Dictionary Comprehension

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 487 Views

In this tutorial, we are going to learn how to use dictionary comprehensions in Python. If you are already familiar with list comprehension, then it won't take much time to learn dictionary comprehensions.We need the key: value pairs to create a dictionary. How to get these key-value pairs using dictionary comprehension? See the general statement of dictionary comprehension.{key: value for ___ in iterable}We need to fill in the above statement to complete a dictionary comprehension. There are many ways to fill it. Let's see some of the most common ways.Let's see how to generate numbers as keys and their squares ...

Read More

Lambda expression in Python Program to rearrange positive and negative numbers

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 855 Views

In this tutorial, we are going to write an anonymous function using lambda to rearrange positive and negative number in a list. We need the pick the negative numbers and then positive numbers from a list to create a new one.AlgorithmLet's see how to solve the problem step by step.1. Initialize a list with negative and positive numbers. 2. Write a lambda expression the takes a list as an argument.    2.1. Iterate over the list and get negative numbers    2.2. Same for positive numbers    2.3. Combine both using concatination operator. 3. Return the resultant list.Note - Use ...

Read More

Calculating Wind Chill Factor(WCF) or Wind Chill Index(WCI) in Python Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 573 Views

In this tutorial, we are going to learn how to calculate Wind Chill Index in Python. We have the formula to calculate the WCI and it's straightforward. We are going to use the following formula to calculate the WCI.Twc(WCI) = 13.12 + 0.6215Ta – 11.37v+0.16 + 0.3965Tav+0.16whereTwc = Wind Chill Index (Based on Celsius temperature scale)Ta = Air Temperature (in degree Celsius)v = Wind Speed (in miles per hour)We are going to use the math module function wherever we need them. Using the math module function decreases the execution time of a program.Follow the below steps to complete the program.Import the math moduleInitialize the ...

Read More

Print a Calendar in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 13K+ Views

In this tutorial, we are going to learn how to print the calendar of month and year using the calendar module of Python. It's a straightforward thing in Python. We need year and month numbers. That's it.Let's see how to print a year calendar. Follow the below steps to print the year calendar.Import the calendar module.Initialize the year number.Print the calendar using calendar.calendar(year) class.ExampleSee the below code.# importing the calendar module import calendar # initializing the year year = 2020 # printing the calendar print(calendar.calendar(year))OutputIf you run the above code, you will get the following output.          ...

Read More

Sum 2D array in Python using map() function

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 5K+ Views

In this tutorial, we are going to find the sum of a 2D array using map function in Python.The map function takes two arguments i.e., function and iterable. It passes every element of the iterable to the function and stores the result in map object. We can covert the map object into an iterable.Let's see how to find the sum of the 2D array using the map function.Initialize the 2D array using lists.Pass the function sum and 2D array to the map function.Find the sum of resultant map object and print it.ExampleSee the code below.# initializing the 2D array array ...

Read More

Number of operations required to make all array elements Equal in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 479 Views

We have given an array of elements, and we have to make them all equal by incrementing the elements by 1. We are allowed to increment n - 1 element at each step. Our goal is to calculate the total number of operations required to make all the array elements equal.For example, if you take the list [1, 2, 3], it took three operations to make all the elements equal. One solution to the problem is. Find the most significant number at each step and increment the rest of the elements by 1. Let's write the code.Exampledef main():    # ...

Read More

Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 642 Views

We are going to explore different methods of calendar module in this tutorial. Let's see one by one.calendar.monthrange(year, month)The method calendar.monthrange(year, month) returns starting weekday number and number of days of the given month. It returns two values in a tuple. Let's see one example.Example# importing the calendar module import calendar # initializing year and month year = 2019 month = 1 # getting the tuple of weekday and no. of days weekday, no_of_days = calendar.monthrange(year, month) print(f'Weekday number: {weekday}') print(f'No. of days: {no_of_days}')OutputIf you run the above code, you will get the following results.Weekday number: 1 No. of days: ...

Read More
Showing 141–150 of 259 articles
« Prev 1 13 14 15 16 17 26 Next »
Advertisements