Learn Python From Scratch, With One Article
Python is, in my opinion, the simplest and most effective programming language you can learn, it is readable and open source, and the possibilities are just endless.
What This Article Trying To Achieve
Well, pretty simple — teach Python, right?
Yes, and no, this article will teach you functional Python, the real fundamentals of Python, no fancy libraries, or email automation. Still, if you’ll stick through, you’ll have the understanding to dive into most articles, and benefit something from them.
With the first 1000 most common words of a language, you can understand somewhere between 60–70% of an every day chat, the second thousand — will only take you up to 70–80%.
Why am I telling you this?
There will be a lot of small points that I won’t dig dive into, and there is a limit for how much the human brain can swallow at once, I promise to stick to what’s important, and hope you’ll find it interesting.
And as a final note — I’m Teaching Python3, I would say Python 2 is dead, but I might hurt some feelings in the crowd, let’s pretend I never wrote that ;)
How Do I Start?
Hold your horses. First, we need to understand what Python is. I’m not too fond of buzz words without an explanation.
Keeping things simple, Python is a way you can tell your computer what to do, how can he understand English you might ask?
Python comes with an interpreter, and he reads your code during runtime. And translate it for computer operations, about the open-source part — ill touch that later.
Hello World is that the first thing you want to write in any language you learn, besides the excitement of running your first code, it also checks that the environment your working on is working as expected.
Our first running program!
Readability — notice that printing to the screen uses the name “print.” this might seems obvious now, but trust me, in other languages, it can be annoying as “System.out.println(text);”
The structure of this command is important, print is a function, a function in the computer worlds receives an input, and gives an output, this function returns nothing, but it prints whatever you want to the screen!
Variables are the building blocks of coding, some are standards, like numbers, strings, lists of values, some will be custom made, that we together will build by the end of this article!
A variable is just a container, a name the represents a value or multiple values. The important part is — It’s just a container.
Now, it’s mostly self-explanatory so far, declaring a variable, is merely assigning a value for it, the power operation is simply a double *, and the order is like junior-high math — left to write, with parenthesis coming first. Divisions and multiplication, followed by addition and subtraction, note the order! it’s important.
Now strings is a much broader topic, the number of functions is endless, but the fundamentals are right here, adding strings is simply concatenating them, you can view a sub-string from a string, just specifying the location of it (from 0 to 2, or any range you’ll like)
The “len” function gives you the length of an item, and this way, we were able to extract the hello world substring from the initial string, ponder that for a bit, I don’t want to spoon-feed you entirely, it will ruin the learning experience.
Lists and tuples, Dictionaries and sets
List is one of the most commonly used objects in Python, it is fast, great for storing large amounts of data and operate on.
remember that most of the time, an operation on a list change the list itself, and copying a list doesn’t indeed copy it, this is a bit more advances, but if you want to copy a list, do this:
You’ll thank me later, trust me.
Tuples, Sets and Dictionaries has so much to offer, but there is a limit to what we can put out in one article, but ill put some general guidelines for usages
Count with dictionaries (or even counter from the standard library), protect values from changing with tuples, and have no recurrences and fast data access with sets.
Here are some neat tricks on the topic!
Python Collections You Should Always Be Using
Python collections is an underrated library, and it can step up your coding to the next level.
If you’re not making any major-size application, efficiency shouldn’t scare you. The list will be just fine for starting.
Flow control is just a fancy name for if-else statements.
Elif — only if the If fails.
Indentation, Indentation, indentation!
I can’t stress how many errors new developers in Python get from this reason only, and the four spaces dictate the statement block, note how the print statement is in the if block, if you’ll move it from the if block, the if statement won’t do anything.
Another important note, == does not assign, it evaluates only to True and False.
The “elif” only occurs if the if statement fails, it is to avoid a test if a number is greater than five and then test again if he is greater than 8 (example)
And & Or combining are ways to check multiple statements.
Loops and Iterables
You can iterate on a list, a tuple, a dictionary, and even a string, but here is just how it works —
looping gives you the power not to rewrite everything you want to do 1000 times, you write it once, and make it stop at the event you want to stop it in (such as a while loop).
Or you can process each data in an iterable at a time, using the animal method.
We’re getting close to the end of what I wanted to show today, so bear with me.
Functions receive inputs, do some crazy stuff in between, and give the user back — an output.
If you can remember this sentence by itself, its most of the work you’ll need to do.
The function lets us operate multiple operations over and over again, without restating them! Also, it gives the code more readability, when you read the line if test_even, it is simple to understand this function checks if a number is even, this way giving the reader a better understanding of what you wrote.
Now Classes are the building blocks of custom objects, think about it like a building blueprint, that teaches you how to make a building.
You can make two buildings from that blueprint, they will both be a building, but not the same one, this is important, a class describes a thing, an object, but it is not the object itself.
OOP (object-oriented programming) is a profound and advanced topic that the first paragraph represents the fundamental concept of it, but an issue that worth a series of articles, not one.
Now an object has two main features. It has attributes, and functions (methods), “things that he is, and things that he can do.”
The last thing, the blueprint is the class Cat, using this Class I can create objects (cats) as demonstrated afterward.
The self attribute refers to the object, not to the Class, basically saying, this cat age is, or this cat gender is, that is why it is called self, it refers to the object.
We can create custom functions like play, in this case, it doesn’t do much, but we can also override built-in python function, such as add (which in this case, I made them give birth to a new cat) or __repr__ to replace the print function.
This explanation is just the tip of the iceberg, but you have to start somewhere.
I know this article is a lot to take. Still, I mean — a lot, but if you give it enough attention, it can teach you the fundamentals of Python in a short period, this way, you can dive deep down into more advanced topics.
Maybe changing your career completely, automating everything you do, this article might have been a small step — but a much-needed one!
I hope you enjoyed it!