This article is the first of a series of posts about learning python with examples.
It is a common question for people who wants to start learning python programming (or any other programming language): what is the best way to learn how to code?
My answer to this question is always the same:
- Start learning!
- Do not learn a language!
- Learn the fundamentals!
- Learn about algorithms!
In this article, I will explain to you a few basic concepts through a simple example. I will also introduce you to several important concepts related to programming.
Python example
The picture below shows a simple python example to calculate how many divisors has a number.
After you execute the previous example, you will have to enter a number, in the example, I used 25, see below the result.
But, what happened? What is all that code?
Keep reading to understand everything.
I’ll give you an informal introduction to python using the previous example.
All the words in blue (def, for, in, range and if) from the first picture are python keywords. It means that these words have a special meaning in python and you can only use them as defined in the python language.
Keywords description from the Python example
Let’s see a description of the keywords:
- def: this keyword defines a function. Informally, a function is a block of reusable code. You can create functions to do operations you need to do more than once. In this case, the function has a name “howManyDivisors” and one parameter “number”. A parameter is a value that the function needs as input to perform the actions that are expected. In this example, the function calculates how many divisors has a number. Therefore, the function needs a number as input to calculate how many divisors have that number.
- for: One of the main important structures in any programming language are loops. Loops allow you to execute an action (or several actions) several times without repeating the same code over and over. In this case, to know how many divisors a number has, you have to answer the following: is 1 a divisor of number? Is 2 a divisor of number? Is 3 a divisor of number? Do you see the pattern here? You are repeating the same question several times to check a different divisor. Imagine doing this for the number 999 if you don’t use loops you have to write 2×999 lines of code. However, with loops, you can do it with 6 lines of code (as in the example above).
- if: another must-have in programming: conditionals. It works the same way that in real life. Write a sentence in English using the word if, and in the same way, you use it in programming. “If I learn programming (then) I can get a job as a programmer”.
- % operator: in python, this symbol means the remainder of the division. i.e. 4%2 equals 0 because the remainder of the division of 4/2 is 0. Therefore, it is a useful operator to determine how many divisors has a number.
- return: The keyword return is used in most programming languages to return values. In our case, we want the function to return a value, how many divisors have the number used as a parameter.
- if __name__ = ‘__main__’: In this line, you are querying if the file was executed from the command line or it is being used by another program. This line is not compulsory. However, it is a good practice when you are creating reusable code.
- input: it is the way to enter information from the keyboard to use it in our program. When you execute this file, you will be prompted with the message “Enter a number:”, and the execution will be paused until you enter the number.
- int: This is the data type that represents integer values in python. A data type defines values that are store using the specific type and operations over those values. For instance, in this case, we use the type to store integer values and perform arithmetic operations using those values.
Strings are specified between quote marks. It represents any character or combination of characters.
Try the example yourself
Now that you have an eagle-view understanding of the code, do you want to execute it yourself? Just give it a try and you will see what a great experience when the computer do as you want.
The first step, install python. You can download it here and follow the official installation guide for your favourite operating system.
Now download pycharm community edition for free. When the download is complete, follow the installation guide.
Open pycharm, click on create a new project and type the example code from the first picture.
After that, you can just execute by pressing “control R”.
Could you execute your code?
Did you understand everything?
Let me know by writing a comment below.
Counting algorithm
The code within the function “howManyDivisors” is the basic algorithm for counting. As the name states, it is an algorithm that we use anytime we want to count. See bellow the general structure of this algorithm.
counter = 0
For-loop
if ( condition )
count = count + 1
Notice counter variable have an initial value of 0. Then a for loop to iterate through the elements.
After that, you check for a condition, if the condition holds, you increment in 1 the counter.