This is a tutorial series which will teach you how to code in Python. We will start with the absolute basics of installation of Python in Windows and Python Tools in Visual Studio. We will then go through basic constructs in Python and write a couple of programs to summarize what we have learned. We will end with an Object Oriented Approach using Python and a specific feature of Python Tools in Visual Studio: Mixed mode C/C++/Python Debugging
Part 1: Get Started with Python summarized the steps involved to setup Python and Visual Studio for Python Development. We essentially learned how to install Python in Windows, Visual Studio and Python Tools for Visual Studio.
In this section, Part 2, we will learn the basic constructs in Python which will cover:
- Control Flow Statements
- Conditional Statements
Let’s get started !
For most basic programs or scripts you will need to print to a console, whether it’s to show information, as for user input or it’s just to display progress. The following piece of code does just that:
It’s always a good practice to comment your code as it not only helps you later when you come back to it, but also everyone else who reads your code. In Python commenting is quite simple. Just add a # before you write your comment.
Once you Run the code, the output will be displayed in a console window as follows:
In most cases you will also need user input. To do so, you can use the function input(). For example,
Variable is essentially a names entity, a container or a box to store values which can change over its lifetime. In Python, a variable can simply be declared and used without declaring or defining it.
In Python, the naming convention for variables is that you cannot start the variable name with a digit, or have a space in-between the variable name. The following table can be used as a reference for Do’s and Dont’s when naming variables:
An example of using the input function to store a value in a variable is as follows:
Control Flow Statements
Control Statements are essential in any program language. Python uses similar constructs with a slight twist. The primary difference being the addition of the ‘:’ after the conditional statement (either ‘if’ or ‘for’ or ‘while’ ). Let us deal with each of them with an example to show you how to use it:
The output of the above code is as follows:
If Else statement:
The syntax to use this construct is as follows:
An example of this flow is shown below:
You could also use multiple conditional statements as shown below:
When executing multiple conditional statements, order of precedence of operators is important. The order of precedence is as follows:
If you want to repeat something ‘n’ times you can use a for or a while loop. Which one you decide to use depends on your situation. The syntax for a for loop is:
The code in the loop will be executed y – 1 times as you can see in the following example:
Note: here I have used the range() function. It generates a sequence of numbers in arithmetic progressions. The range function can be used as follows:
Or, it can also used as:
The syntax of the while loop is as follows:
An example of this is as follows:
Some additional control statement constructs:
Break and Else Clauses on Loops (Yes, this is unique to Python and is not ‘if-else’ else but ‘else’ for a conditional loop):
The ‘break’ statement like in other programming languages breaks out from the closest enclosing ‘for’ or ‘while’ loop. The ‘conditional loop-else’ works as follows:
It is executed when the loop terminates through exhaustion of the list ( for loop) or when the condition becomes false (while loop), but not when the loop is terminated by a break.
The below example shows how the break is used along with the loop-else construct:
In this tutorial, you have learned basic constructs of how to write a simple program. Starting with how to handle input and output statements, to variables and control flow statements. We also learnt about loop-else construct which is unique to Python. With this, you should be able to write your first simple programs and start experimenting with Python.
In the next part, I will cover how to define / handle functions and basics of file handling. Stay Tuned!