Usually, after going through courses or books like Python Crash Course or Automate the Boring Stuff, you might get stuck. You’re able to follow tutorials, but not able to create your own project from start to finish.
I’ll quickly teach you how to create a small project that “looks complex,” but is pretty simple. This is so that you don’t get demoralized by looking at cool projects that you’re not able to create. The reason is not that you’re not able to write “big” code, the reason is that you have a very limited vocabulary of useful Python libraries.
Let’s build a python app that can speak
Sounds cool, huh? This is how or app would look (it’s command line):
$ python speaker.py What do you want me to say?: This is a great python tutorial! Saved as audio.mp3!
And this is the audio file that it generates!
How much effort went into creating this?
This sounds pretty complex, right? Don’t you see others creating projects like these all day, but you don’t have a clue as to how to do it?
Surprisingly, this takes less than 10 lines of code:
from gtts import gTTS text = input("What do you want me to say?: ") audio = gTTS(text) audio.save('audio.mp3') print("Saved as audio.mp3!")
You need to make sure that you do
pip install gTTS first. Even the usage is inspired verbatim from the library’s official documentation: https://pypi.org/project/gTTS/
Basically, this program saves a file called
audio.mp3 that you’ll have to manually play.
Wait… How does something complex take so few lines of code?
Experienced programmers avoid creating “big things.” Even a large software project that you see often comprises smaller parts. A production quality Python application uses several libraries, that in turn have a lot of distinct features.
As an experienced programmer, I rely on a web framework (which has lots of moving parts), a library to deal with databases (so that I don’t have to do it directly), and a lot more.
Once you understand the basics of Python, it’s just a matter of finding the right library and using it. That’s the whole point of software engineering: deliver features fast.
But what about understanding things under the hood?
gTTS library that you used calls the Google text-to-speech API that does the actual conversion. So, even the person who created this library just called an API and made it convenient for you. The actual text-to-speech thing is an active research area, and Google would be using lots of complex AI to get this done. If you want to learn how that happens, there are courses that will teach you that too. Yes, it involves a lot of math.
For now, remember these things:
- You don’t need to completely understand something to use it. You drive your car without knowing how the engine works, right? This is why libraries give you documentation on how to use it, while they manage how it works inside.
- You can build pretty complex stuff by “discovering more libraries” and using them creatively. You will be creating awesome applications in no time.
- You can always do a deep-dive later to understand how things work under the hood. But you can creating something cool without doing the deep dive.
- Number of lines of code does not dictate the complexity of a project. There are lots of algorithm-heavy libraries that have less number of lines, but it’s very tricky to get them all right while avoiding edge cases.
Try to do this project now…
A program that reminds you to take a break from using the computer every hour. The user can configure his name when he starts the app, so that his name is spoken in this manner: “Hey Richard, time to take a break!”
Hint: Search for a library for Python that will allow you to play mp3 files. Then, you just need a loop that gets triggered every hour.
The program is pretty simple in theory. But the fact that it “speaks” is what makes it cool.
The project now looks very easy to do because you have the right libraries. That’s how it is in the real world. Whenever you’re faced with a problem, always try to find a library that does the work for you. Read its documentation, and then apply it, the same way we applied the text to speech library. Google is your friend.
Make Python’s package repository your friend: https://pypi.org/