Python has relatively easy syntax and is easy to learn. In this single fast track tutorial, you’ll learn following basics of Python programming.
- Execute a Sequence of simple (
print
) statements - Variables and arithmetic expressions
- Iteration using
for
loop - Taking Input from user
- Selection (
if/else
) - Nesting Iteration inside Iteration (
for
loop block insidewhile
loop block) - Organising code with Functions
- Add Code Docs
On this path you’ll also see…
- How to format strings.
- Nested blocks of code.
List
syntax.- How to add code docs.
What is our goal?
The goal is to print the table of a given number. We’ll start with getting results in console like this…
Start Goal:

… and after some iterations of improvement, we’ll get to this end result. 🙂
End Goal:

Step-by-step approach
Let’s take a step-by-step approach and start with a simplest implementation and improve it further with every next step.
Step-1: Sequence of simple statements
Look at the code below. It is simply a sequence of 11 print
statements. (See print function documentation). These 11 statements will execute one by one in the order as written in the program and eventually we’ll see the output as shown in ‘Start Goal’ image.
Problems (Step-1):
This program has definitely brought us to our start goal but there is one limitation here. This program shows only the table for number 2
. With this approach, to generate the table of another number, either we’ll have to modify this program or we’ll have to create another one for the new number and so on. You see that this approach will not let us create something useful.
Step-2: Variables and expressions
To solve this, let’s introduce you to variables (https://www.w3schools.com/python/python_variables.asp). In step-1 we had everything ‘hardcoded’. Let’s add variable num
to the same program and then we have once again those 11 statements but this time a bit changed.
String formatting:
We are using string
formatting here and with {}
we are telling print
function that we’ll provide a value using format
function (https://pyformat.info/).
In format
function we are passing two values num
and num*1
in first print
statement of the table. Both of these values will replace {}
in the order they are specified.
Arithmetic Expressions:
num*1
, num*2
and so on are arithmetic expressions using the product arithmetic operator (*
). These evaluate to the product of both operands. Read more about Python operators. You can use those different types operators to create different type of expressions.
Now that we have introduced num
variable in to our program, we only need to change the value of this variable and execute the program again to see the table of the number assigned to num
. This solves the above mentioned problem.
Problems (Step-2):
Those 10 statements look quite identical which means we are repeating similar statements over and again. Let’s fix this in step-3.
Step-3: Iteration using for
loop
See the code below, how we have reduced those ten lines of code to two lines.
We are using for
loop which repeats the statement under it 10 times. i
variable takes the values from 1
to 10
(not up to 11
as upper bound is excluded in range
function), incrementing the value by 1
on each iteration.
In the print
statement, now we have three {}
and accordingly we have three value passed into format
function, two of which are same as before where as i
replaces the number 1-10 used in statements in step-2.
Problems (step-3):
We’ve fixed the code repetition issue but now we want to improve it further. Problem we still have is that we have to change the value of num
in code every time and execute the program. Instead we shouldn’t need to change the program (change the hardcoded value of variable)
Step-4: Taking Input from user
In this step, we simply change the hardcoded value of num
into an input
statement. (See Python input function documentation)
Problems (Step-4):
We have allowed user to input a number but user can mistakenly or intentionally input a non-numeric values which will result in unhandled exception.
Step-5: Selection (if/else
)
To make sure that our program doesn’t terminate with an unhandled exception error, we’ve introduced if/else
block right after input. If num.isnumeric()
evaluates to True
, the table will be generated, otherwise ‘Wrong input’ message will be displayed.
Nested blocks:
In this example, you can see that we have nested for
loop block inside if
block.
Problems (Step-5):
The next limitation we want to remove is that until this step user has to execute the program again to see the table of next number. We want to improve this in a way that user can choose to terminate the program when they want to and until then they should be able to input the next number and see the new results.
Step-6: Nesting Iteration inside Iteration (for
loop block inside while
loop block)
We’ve introduced a new variable terminate
with initial value False
so that the program executes at least once. Inside while
, you can see the same block of code till line 18 so this performs the same input and output functionality.
The we prompt user to tell program if it should continue or terminate and in the last statement we are determining the value of terminate
variable based on user input. If user enters Y
or y
, terminate
will be evaluated to False
in which case while
condition will pass and program will execute again. In case of inputs other than Y
and y
, program will terminate!
List syntax:
We are creating a list of valid options for Yes response ['y', 'Y']
. In Python, lists can be created using the pair of opening and closing square brackets and writing the comma separated values inside both. To learn more about Python lists: https://www.w3schools.com/python/python_lists.asp
Problems (Step-6):
The code in step-6 brings us to our end goal from output perspective but there is still more to improve from code quality perspective. In the code above, the program is doing everything in single block of code. If we continue to add more logic into it this way, the code will become unreadable and unmaintainable.
Step-7: Organising code with functions
To keep the code structured, modular and easily maintainable, we can break our program in to multiple functions.
We have defined print_table
function which takes num
as an argument. This function takes the responsibility of printing the table of input number.
The we have defined, should_terminate
method which takes user input to determine if the user wants to continue executing the program or wants to terminate. This function returns a bool
value (True
/ False
)
On top we have defined execute
function, which controls the main logic of our program which also includes the calls to print_table
and should_terminate
functions.
At the end of the code, we make a call to execute
function which triggers the execution of the program.
Step-8: Add Code Docs
In this step again, we are focusing on code quality for which code docs are an important feature. Please note the format of code docs we added to all three functions.
I hope you’ve enjoyed this simple but content-ful tutorial and I hope this will help you in quickly understanding the basics of Python. 🙂