Found 10783 Articles for Python

Parallel Courses in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:21:33

170 Views

Suppose there are N courses, and these are labelled from 1 to N. We also gave a relation array, where relations[i] = [X, Y], is representing a prerequisite relationship between course X and course Y. So, this means course X has to be studied before course Y.In one semester we can study any number of courses as long as we have studied all the prerequisites for the course we are studying. We have to find the minimum number of semesters needed to study all courses. And if there is no way to study all the courses, then return -1.So, if ... Read More

Divide Array Into Increasing Sequences in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:12:47

153 Views

Suppose we have a non-decreasing array of positive integers called nums and an integer K, we have to find out if this array can be divided into one or more number of disjoint increasing subsequences of length at least K.So, if the input is like nums = [1, 2, 2, 3, 3, 4, 4], K = 3, then the output will be true, as this array can be divided into the two subsequences like [1, 2, 3, 4] and [2, 3, 4] with lengths at least 3 each.To solve this, we will follow these steps −d := a new mapreq ... Read More

trunc() in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:22:56

304 Views

In this tutorial, we are going to learn about the math.trunc() method.The method math.trunc() is used to truncate the float values. It will act as math.floor() method for positive values and math.ceil() method for negative values.Example Live Demo# importing math module import math # floor value print(math.floor(3.5)) # trunc for positive number print(math.trunc(3.5))OutputIf you run the above code, then you will get the similar result as follows.3 3Example Live Demo# importing math module import math # ceil value print(math.ceil(-3.5)) # trunc for negative number print(math.trunc(-3.5))OutputIf you run the above code, then you will get the similar result as follows.-3 -3ConclusionIf you have ... Read More

time.process_time() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:21:15

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 Live Demo# 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 Live Demo# importing the time module import time # program to find ... Read More

time.perf_counter() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:19:13

3K+ 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 Live Demo# 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 Live Demo# 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:       ... Read More

The most occurring number in a string using Regex in python

Hafeezul Kareem
Updated on 11-Jul-2020 08:18:27

156 Views

In this tutorial, we are going to write a regex that finds the most occurring number in the string. We will check the regex in Python.Follow the below steps to write the program.Import the re and collections modules.Initialize the string with numbers.4Find all the numbers using regex and store them in the array.Find the most occurring number using Counter from collections module.Example Live Demo# importing the modules import re import collections # initializing the string string = '1222tutorials321232point3442' # regex to find all the numbers regex = r'[0-9]' # getting all the numbers from the string numbers = re.findall(regex, string) # ... Read More

Taking multiple inputs from user in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:16:43

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.Let's take the multiple strings from the user.Example# taking the input from the user strings = input("Enter multiple names space-separated:- ") # spliting the data strings = strings.split() # printing the data print(strings)OutputIf you run the above code, then you will get the following result.Enter multiple names space-separated:- Python JavaScript Django React ['Python', 'JavaScript', 'Django', 'React']What if we want ... Read More

Taking input from console in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:14:12

3K+ Views

In this tutorial, we are going to learn how to take input from the console in Python.The interactive shell in Python is treated as a console. We can take the user entered data the console using input() function.Example# taking input from the user a = input() # printing the data print("User data:-", a) Tutorialspoint User data:- TutorialspointOutputIf you run the above code, then you will get the following result.Tutorialspoint User data:- TutorialspointConclusionThe data that is entered by the user will be in the string format. If you have any doubts in the tutorial, mention them in the comment section.Read More

Take Matrix input from user in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:09:51

6K+ Views

In this tutorial, we are going to learn how to take matric input in Python from the user. We can take input from the user in two different ways. Let's see two of them.Method 1Taking all numbers of the matric one by one from the user. See the code below.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):       # asking the user to input the number       # converts the input to ... Read More

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Updated on 11-Jul-2020 08:06:36

507 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example.Inputa = 3 b = 5Outputa = 5 b = 3Let's see one by one.PythonWe can swap the variable with one line of code in Python. Let's see the code.Example Live Demo# initializing the variables a, b = 3, 5 # printing before swaping print("Before swapping:-", a, b) # swapping a, b = b, a # printing after swapping print("After swapping:-", a, b)OutputIf you run the above code, then you will get the following ... Read More

Advertisements