Learning Python the Hard Way

Learning Python the Hard Way

Intro to Modules, Prompting and Real-World Applications

Ah, so I disappeared again 🤣🤣.

At this point, I am already accustomed to the hassle of blogging. It is fun but at the same time, it's not something I am really good at. Obviously, I'd wish to be more consistent as it lets me pour out my thoughts to anyone who cares to read. Mostly, I do it for my own sanity.

Regardless, the journey to teaching oneself how to program is still challenging. Many times I have struggled with the 'shiny object' syndrome. This is where I become attracted to the fanciest teaching structure currently present. Fortunately, I think I have narrowed it down to Zed Shaw's: Learning Python 3 The Hard Way.

To be honest, the title of the book really intimidated me but once I got down to doing the actual exercises, they really weren't that bad. Zed Shaw teaches in a repetitive manner where he makes your brain repeat certain tasks and actions to a point where it turns into muscle memory. And it works incredibly. I have already noticed that even after a week of not sitting in front of my terminal, some of the fundamentals I have learned are still hardcoded into my brain.

I think I will continue with this book until I feel comfortable enough to pursue other programs and structures. For the sake of time, and even for myself, I think I'll just include one or two exercises that I did based on the book. The other stuff I literally blazed through since I had already gone through the same concepts at least two or three times.

Introduction to Modules

Intro to Modules 1.png

From this exercise, I was able to learn that modules can be added or imported from Python to improve one's program. They also help with keeping programs lightweight and give documentation to other programmers who will read my code.

Kajiado, Watamu, Uganda, Egypt = argv

In the above snippet, I was able to pass 4 arguments into the argv variable. When running the file from the command line, you are prompted to pass 4 arguments. Anything less than that and you will receive an error. In this particular case, this particular script (ex13a.py) is included in the arguments so I only included Kajiado, Watamu, and Uganda as the other arguments.

I was able to make use of the input() syntax to get further information from the user.

Modules and Prompting

I dug a little deeper and proceeded to combine modules, inputs, and prompting. I was always curious as to how hackers in movies communicate in such a seamless manner. By passing the variable prompt as an argument within the input syntax, I was able to minimize on redundancies of repeating my code each time the user gave me information.

Modules & Prompting.png

For example, after the user has added the script name, user name and age as arguments, the command line returns a series of questions. Finally, it promts the user to answer whether they like the person mentioned as user_name This information is then stored and assigned to the likes variable. The same instance occurs when the user is asked to input where they lives and age.

Prompting Exercise.png

The command line then returns a formatted string based on the information that the user provided. Noteworthy, you can assign prompt any character you wish.

I had tons of fun playing around with this and can't wait for when I will be able to build an interactive program or chatbox that I can use to communicate with a few friends to begin with. I can already see the number of instances where this concept can be applied. For example, in queueing systems. I could prompt a train user to give information such as date of departure, destination desired, return the appropriate price, and output all of this information in their ticket.

Cheers to more exploring!

Happy coding folks.