You are doing great. By joining this interactive Python email course, you have proven that you are ambitious to learn. Well done!
I offer this course for free because I believe that you and I can work together, via email, to create a new form of swarm intelligence. It’s likely that you are facing the exact same problems as other people in the Finxter swarm. Why should we accept this painful redundancy of solving our problems separately and in isolation? It’s easier to learn together – and to become greater than the sum of our individual efforts. That’s swarm intelligence!
In the last mails, you have already learned about slicing sequences in Python. This is really basic stuff – and you have mastered it! (But you will not rest on your laurels, right?)
This mail is about a topic that is far more important and even more fundamental. Get your hot, freshly-brewed coffee and let me show you what holds you back more than anything else in your coding productivity.
Imagine you want to build your own logistics company. How would you start approaching this?
Most importantly, you need to get yourself a truck. Otherwise, you could not even start to transport stuff from A to B.
Now – you did not even consider building a truck yourself, didn’t you? Why should you reinvent the wheel – literally? After all, you lead a logistics company and not a truck manufacturing company.
Yet, when it comes to coding many developers DO reinvent the wheel on a daily basis. They implement basic algorithms on their own again and again. What a waste of resources!
That’s why today’s mail is all about Python libraries. Thanks, Stepan, for your great idea to address Python libraries as part of this email course.
Let’s start slowly. The coding world is full of thoughtful metaphors to make coding more intuitive. One useful metaphor is the concept of a code library. In the physical world, if you don’t want to reinvent the wheel, you go to the library and look it up. Similarly, in the digital world, if you don’t want to invest hours implementing a web crawler, you simply import a web crawling library.
If you think about it, everything is a library in coding – even the programming language itself. The simple statement x = 3 + 2 is only a high-level representation of low-level functionality. In this case, addition is really only a shortcut for multiple machine code instructions.
Coding is nothing but to create high-level functionality out of low-level functions.
Ok, let’s dive in the concrete world of Python libraries.
What is a Python library?
A Python library is a collection of modules. Many libraries consist of dozens of modules. Some libraries consist of only a single module. This brings us to the next question.
What is a module?
Have you ever put code into a file ‘yourFile.py’ and executed this code via ‘python yourFile.py’? Congrats, you have already created and executed your own module!
Let’s make the concept of modules 100% clear. Say, you want to implement your own module. How do you do this? Simply create two files (just in your head): ‘main.py’ which contains your main program and ‘module.py’ which contains your minimal module.
Puzzle 1: What is the result of executing ‘python main.py’ in your shell? (Find the solution at the end of this mail.)
# file module.py print("hello world") # file main.py import module
(Spoiler alert: solve the puzzle first before continue reading.)
The import statement in Python is simply a convenient way to “copy and paste” the content of the module into your file (without ACTUALLY doing this). Note that you must skip the suffix “.py” when importing a module. (Don’t ask me why.)
Puzzle 2: What is the result of executing ‘main.py’?
# file italian_greetings.py def hi(): print("Ciao bella!") def bye(): print("Ciao bella!!") # file main.py import italian_greetings italian_greetings.hi()
Clearly, we are not seeing the result of function bye(). The reason is that we have only defined but not yet executed bye().
Great, now you can import modules from a library (=collection of modules) and you can even create your own modules and libraries! Not too bad.
However, you will quickly become annoyed by always writing the module name like in italian_greetings.hi().
That’s where a new command comes into play: import x as y. Replace the placeholder x with your (shorter) module name y.
Puzzle 3: What is the result of executing ‘main.py’?
# file italian_greetings.py def hi(): print("Ciao bella!") # file german_greetings.py def hi(): print("Guten Tag!") # file main_3.py import german_greetings as de import italian_greetings as it de.hi() it.hi()
But how do we get rid of these prefixes completely?
It’s time to introduce a third command (after import x, and import x as y): from x import z. The placeholder x is still a module but z is a specific function in this module. This allows us to do fine-grained imports of just the functions we need. This is useful if we have huge modules with hundreds of functions or if we want to laser-focus our imports.
Puzzle 4: What is the result of executing main.py?
# file main.py from german_greetings import hi from italian_greetings import bye hi() bye()
Ok, I think that’s enough for today (after all, it’s your “Coffee Break Python“).
I will definitely go a bit deeper into modules, libraries, and namespaces in the next mail(s).
But make sure to keep filling my backlog with Python (and computer science) topics YOU struggle with!
Thanks for your time and your attention. It’s my priority to give you a positive return on your invested time!
PS: If you like code puzzles, you will love my book ‘Coffee Break Python: 50 Workouts to Kickstart Your Rapid Code Understanding’. It’s the only puzzle-based learning book on the market (crazy, I know). Check it out!
(If you have already bought the book – it would be great if you could take the time to rate it on Amazon. Thanks!)
(Elsif you have already bought and rated the book – YOU MADE MY DAY!)
PPS: In case you are more the visual/interactive learner, I am currently creating an interactive online course based on the book, so have a bit of patience…
Here are the solutions to the puzzles in this mail.