What/Why? I want to write a simple Python script that warns me if crypto price data (e.g., BTC) crosses a certain threshold. This can be useful for trading or some other apps, so I thought it would be fun to do it.
The tutorial in front of you simply documents my learnings on creating a sinus tone in my Jupyter Notebook—so it may benefit you as well.
If you want the whole tutorial on my mini project, you can check it out here on the Finxter blog:
👉 Recommended Tutorial: I Made a Python Script That Beeps When BTC or ETH Prices Drop
💬 Challenge: Write Python code in a Jupyter Notebook that creates a sinus tone when executed.
The easy way to solve this challenge is the following. 👇
This code creates a sine wave with a frequency of 500 Hz and plays it in the IPython environment. The wave is created using the NumPy library by specifying the frequency and the length of the wave (15000*2). The rate of the wave is set to 10000 Hz and autoplay is set to
True so that the wave will start playing immediately.
import numpy as np from IPython.display import Audio # Create the tone as a NumPy Sinus Wave wave = np.sin(2*np.pi*500*np.arange(15000*2)/15000) # Play the Sinus Wave (tone) Audio(wave, rate=10000, autoplay=True)
This generates the following beep sound in your Jupyter Notebook:
What happens if you change the
rate argument of the
Audio() function call to be 20000 instead of 10000?
# Play the Sinus Wave (tone) Audio(wave, rate=20000, autoplay=True)
The beep sound tone gets higher:
You can play around with the Jupyter notebook here:
But what if you don’t have a Jupyter notebook but a normal Python script (Win/Linux/macOS)?
In that case, you cannot use the
IPython library. Instead, follow the steps outlined in the following tutorial on the Finxter blog—you still can play beep sounds!
👉 Recommended Tutorial: How to Make a Beep Sound in Python?
Thanks for Reading! ♥️
You’re welcome to join our free email academy where I share all our coding projects and cheat sheets on a weekly basis:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.