Part 2: Get Started with Python


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.

Part 3: Get started with Python: Functions and File Handling

Part 4: Get started with Python: Object Oriented Approach

Part 5: Get started with Python: Debugging in PTVS

Part 6: Get started with Python: Build your first Django Application in PTVS

In this section, Part 2, we will learn the basic constructs in Python which will cover:

  • Output
  • Input
  • Variables
  • Control Flow Statements
    • Conditional Statements
    • Loops

Let’s get started !

Output

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:

 1: print(“This is a tutorial to show the basic constructs in Python”)

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.

 1: #This will print the text in quotes in the console

Once you Run the code, the output will be displayed in a console window as follows:

image

Input

In most cases you will also need user input. To do so, you can use the function input(). For example,

 1: input("Enter a number:")

 

Variables

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.

 1: number=input('Enter a number:')

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:

Dos

Don’ts

Variable

First Name

First_Name

1userName

FirstName

Friend’sName

firstName

 

FirstName

 

userName1

 

 

An example of using the input function to store a value in a variable is as follows:

image

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:

If statement:
 1: if (<condition>):
 2:     #execute code
 1: userNum=input('enter a number:')
 2: if (int(userNum) < 10):
 3:     print('the number '+userNum+' is less than 10')

The output of the above code is as follows:

image

 

If Else statement:

The syntax to use this construct is as follows:

 1: if (condition1):
 2:         #execute the code 1
 3:     elif (condition2):
 4:         #execute the code 2
 5:     else:
 6:         #execute the code 3

An example of this flow is shown below:

image

You could also use multiple conditional statements as shown below:

 1: if (condition) and (condition):
 2:         #execute code here
 3:     elif (condition) or (condition):
 4:         #execute code here
 5:     else(condition):
 6:         #execute code here

When executing multiple conditional statements, order of precedence of operators is important. The order of precedence is as follows:

image

For :

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:

 1: for x in range(y):
 2:         #do the following

The code in the loop will be executed y – 1 times as you can see in the following example:

image

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:

 1: >>> range(5, 10)
 2: [5, 6, 7, 8, 9]
 3: >>> range(0, 10, 3)
 4: [0, 3, 6, 9]
 5: >>> range(-10, -100, -30)
 6: [-10, -40, -70]

Or, it can also used as:

image

While loop:

The syntax of the while loop is as follows:

 1: while (condition):
 2:         #do the following

An example of this is as follows:

 

image

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:

image

Summary

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!

Comments (0)

Skip to main content