Have the Python learning tutorials forwarded by your circle of friends been forgotten in your favorites? The original author, William Koehrsen, is an applied data science researcher and ultramarathon runner-up. This article is his introductory experience in learning Python, which may help you who have not yet taken action.
Reminiscing about my first Python program
For the sake of nostalgia, I want to share my first Python program two years ago. I first learned Python as an aerospace engineering student who wanted to avoid learning Spreadsheets, and I didn’t know why I chose Python.
My Python education began with Al Sweigart's book "Automated the Boring Stuff with Python". It is an excellent introductory book on programming that teaches you to use simple programs to complete useful tasks. When I take a new course, I look for opportunities to use it and try to solve a new problem in Python.
Before the first assignment, I was very eager to get this book worth 200 US dollars, but I couldn't even afford the rent of 20 US dollars. I saw that Amazon had a free one-week trial opportunity, so I got the right to use the book for a week through Amazon and completed the homework. While I could create a new account every time I need it, it would be too much trouble. I figured out a more efficient way to do some programming using Python.
One of the many useful libraries in Automate the Boring Stuff is pyautogui, which allows you to control the keyboard and mouse through Python. Some people say that when you have a hammer, every problem looks like a nail.
Python and pyautogui will allow me to press the arrow keys and take screenshots, solving the "unlimited free viewing of books problem".
I wrote the first program to automatically flip through each page in the book and take screenshots. Although there are only 10 lines, I am very proud!
The following is the entire content of this code:
Import pyautogui
Import time
#Sleep for 5 seconds, let me open the book time
sleep.leep(5)
# The range can be changed according to the number of pages (1000):
for i in range(1000):
# Turn page
pyautogui.keyDown('right')
pyautogui.keyUp('right')
# Save screenshot
pyautogui.screenshot(' images/page_%d.pdf'%i)
time.sleep (0.05)
It's very simple to run the program (I encourage anyone to try it). I saved the script as book_screenshot.py, then pulled up a command prompt in the same folder and typed:
python book_screenshot.py
Then, I would have 5 Flip the book in seconds and bring it to full screen. The program does the rest, going through each page and taking screenshots that are saved as pdf. I can then merge all the pdf files into one file and have a (albeit of dubious legality) copy of the book! Admittedly, this is a copy that does not respect copyright enough, but it is the method I have to use in order to read the book.
This example shows two key points for me to learn new skills:
1. The best way to learn new skills is to find the problem that needs to be solved!
2. You don’t need to fully master a skill before it is useful.
With just a few lines of code and a free online book, I wrote a program that I actually use. Learning the basics can be tedious as I get bogged down in data structures and loops. My first attempt at learning Python failed within a few hours. But once I changed my strategy and started developing solutions to real-world problems, I ended up learning some basic principles.
There is a lot to master in the world of programming and data science, but you don't need to learn everything at once. Pick a problem you need to solve and get started!