Mathematically Euclidean Algorithm is an impressive way for computing the greatest common divisor (GCD) of two numbers (integers), where the largest number that divides both without having a remainder.
This Algorithm is named after the ancient Greek mathematician Euclid.
Using Euclidean Algorithm, we can compute GCD by leveraging as below.
def algoGCD(a, b):
a, b = b, a % b
print("The GCD is", algoGCD(a, b))
The GCD is 18
With math.gcd() Function
The math package provides gcd() method , which will compute the gcd of given numbers.
Its just a one line code.
print("The GCD is", math.gcd(a, b))
The GCD is 18
Using fractions.gcd() Function
The fractions package provides gcd() method, this will calculate the gcd with one line of code.
print("The GCD is", fractions.gcd(a, b))
The GCD is 18
In this article, we have discussed how can you remove the character from string in python.
Let’s discuss how to remove a specific character or unwanted character from string in this post.
The following are the different string methods to remove the character from a string element using python programming.
As String is immutable, the methods used below will return a new string and the original string remains unchanged.
In other words, we are just doing string manipulation to achieve the result.
Python Remove Character From String
remove a character from the string in python you can do by two different methods, one is using translate function and the second one is using replace functions, You can remove by using both translate and replace functions. In this article, we have discussed all the steps and methods of how can you remove it in python. Read this article fully, and if you have any doubts don’t hesitate to ask us in the comments section. You can also rate us on the bottom-left section of the vote.
Python Remove Character from String Using the translate method
Python translate() method replaces each character in the given string inline to the provided translation table.
for this, we have to provide the Unicode for the character and None as replacement value to remove the value from the final string.
ord() function used to retrieve the Unicode of a character.
sleep python method pauses/suspends the python program for certain amount of specified time.In other words, sleep method suspends the execution of the calling thread for a given number of seconds.
The method argument can be a floating point to specify a more precise sleep time.Also note that, the actual suspension time might be less than the requested time. Its because of any caught signal which will terminate the sleep() following execution.
Python time.sleep( ): time sleep method is similar to shell sleep command. It accepts the argument in seconds/milli seconds.
For precise sleep times, floating point number can be provided as the argument for python time sleep() method.
Note: time sleep method is part of time module/package in python 2 and python 3, so you need to import the “time” package in the respective python script or function.
Since python version 3.5, the sleep function now sleeps atleast seconds eventhough if the sleep is interrupted by a signal, except incase of signal handler raises an exception via script.
Syntax of Python time.sleep( ):
Following is the syntax for sleep method
time.sleep( t )
t – Number of seconds/milliseconds execution to be suspended in python code.
Following example program illustrates the exact usage of time sleep( ) method in the python script or function.
# Print the start time
print("current time start: ", end ="")
# using sleep() to pause the code execution
# Print the end time
print("current time end: ", end ="")
current time start: Wed May 29 16:50:48 2019
current time end: Wed May 29 16:50:53 2019
As per the program execution, the wait time or delay is 5 seconds.
Example 2: Delay in Python script
Create a script called sleep-demo.py:
# The program will run infinity times on screen till user hit the CTRL+C
# The program will sleep for 10 seconds before updating date and time again.
print "!!! Hit CTRL+C to stop the program !!!"
## Start loop ##
### Show today's date and time ##
print "Current date & time " + time.strftime("%c")
#### Delay for 10 seconds ####
Save, close the file and run as follows:
$ chmod +x sleep-demo.py
Current date & time Wed May 29 17:50:48 2019
Current date & time Wed May 29 17:50:58 2019
Current date & time Wed May 29 17:51:08 2019
Current date & time Wed May 29 17:51:18 2019
Current date & time Wed May 29 17:51:28 2019
Current date & time Wed May 29 17:51:38 2019
The output will be infinite loop till user interrupts(CTRL+C).
Example 3: Python Multithreading
Python supports multithreading too, here’s an example of a multithreaded Python program.
Create a script called multithread-demo.py:
for i in range(3):
for i in range(3):
t1 = threading.Thread(target=print_welcome)
t2 = threading.Thread(target=print_python)
Output: As per the above program, execution suspended for two threads at 0.6 seconds and 0.9 seconds respectively. So start exploring python sleep function in your python script, Happy scripting 🙂