An Introduction to Interactive Programming in Python (Part 1)
An Introduction to Interactive Programming in Python (Part 1)

About Python

Python 3.0 also known as Python 3000 is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

Installing Python 3

The first program

Let’s write our first program to, print “Hello World !”

In Python, we use print(“”) function to display text on the screen.
We use quotations marks for beginning and end of the string; they don’t appear in the result.
In two ways we can run python program:

  1. Python Shell
  2. Python Script

Using Python Shell

After installing Python on your machine, to open the Python shell type in “python3” in your command prompt or terminal, this will begin an interactive python session.This interactive session doesn’t save any of your code in a file; it expires as soon as you exit from the shell.

In your Python shell, you will find the currently running version of your Python, some information and three arrows waiting for your command. Now let’s write the code to print Hello World in the Python shell.

As soon as you hit enter the Python interpreter runs the code and gives the output.

General python shell is used to test programs on the go.Use help(anything) for documentation. It’s way faster than any web interface.

Executing Python Script

To run Python scripts save your Python code in a .py file and in your command prompt or terminal use python3 file_name.py to run the program.

Example:
hello_world.py

$python3 hello_world.py
Hello, World!

Make sure you change the directory where you saved the file before doing it.

When you are writing large pieces of Python code, then we need to use Script Mode.

You can use different text editors for writing Python scripts such as Sublime Text , Atom Editor.

Number and Variables

As we know numbers are everywhere, so let’s see how Python deals with numbers.Python shell can be used as a calculator; you can perform the basic mathematical operation in python shell such as addition, division, etc.

Example:

In Python we have three distinct number types they are integers, floating point numbers, and complex numbers. Integers are numbers with no fractional part they can be either positive or negative or unsigned(zero).Float values are numbers with a fractional part.

In programming, a variable is nothing more than a name for something.Variable names are case sensitive.

To declare a variable in Python, you have to give its name followed by an equal sign and the initialisation value.

When you keep assigning the values to the same variable name Python simply replaces the older value with newer value.

Python3 supports the following data types:

1. Boolean
2. Integer
3. Float
4. Complex
5. String

datatypes.py

python3 datatypes.py

Strings

In python string is a sequence of characters enclosed in either single or double quotes.Strings are immutable sequence of Unicode points.

Example:

strings.py

python3 strings.py

Python Formatting

Using .format()

Sometimes we may want to construct strings with the value of the variable and this conceptual construction of strings is called string interpolation.

Example :

interpolation.py

python3 interpolation.py

Data Structures

To organise and process data efficiently in memory we use data structures, In Python, we have different types data structures and several operations to perform on them.

Now we will discuss three main data structures in Python(Lists, Tuples, Dictionaries)

Lists

Lists are ordered, mutable sequence of values, these are similar to arrays in C and C++.

To declare a list in Python we give the list name assigned to values separated by commas.

Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.

Example:

lists.py

python3 lists.py

Note: Negative indices starts from the end of the list with index value -1.

We have several methods on lists here are few examples.

list_methods.py

python3 list_methods.py

The index method finds the given value in the list and returns the index value. Index method is used to search where the values are incurred.

These are few examples of list methods, to find out more we can use help(list) for all the list methods.

Tuples

Lists are ordered, immutable sequence of values. Declaring tuples is similar to lists we just enclose the values in pair of parentheses.

Tuples are immutable which means we cannot add or delete items in the tuple, and also tuples have no index methods.

Why use tuples?

Tuples are faster than lists and safer.They are written -protected we cannot add new items once the declaration is made.

tuple.py

python3 tuple.py

Dictionary

Dictionaries are unordered set key-value pairs.The key and the value can be of different data types.Each value has a unique key which acts as an identifier for that value within the dictionary.

Defining a dictionary

Example:

my_dict.py

python3 my_dict.py

Conditionals (if, elif, else)

We always need the ability to check the conditions and change the behavior of the program accordingly. The conditional statements give us the ability; the simplest form is the ‘if’ statement.

A conditional statement tests an expression to see whether it is true or false and it does the operations based on the result.

Note: Python must know the number of statements which must be executed when the condition happens is true. To enable this the concept of indentation occurs.All the statements below the header (if in this case) must maintain the same level of indentation(4 spaces).

This concept of indentation is applied throughout Python programs.

Example:
temp.py

python3 temp.py

If there are two possibilities of conditions, then we use if, else statement.

Example:
movie_plan.py

python3 movie_plan.py

Multiple conditions

If we have more than two possibilities and we need more than two branches. SO we use chained conditional statements. The term elif is the abbreviation of else if.There is no limit of elif statements, but the last branch has to be an else statement.

Example:

calc_bmi.py

python3 clac_bmi.py

Suggest

☞ The Python Video Workbook: Solve 100 Exercises
 Python 3: A Beginners Quick Start Guide to Python

☞  Learn the essentials of Python and ArcGIS’s arcpy

☞  The Developers Guide to Python 3 Programming

☞  Learn to code Economic Models in Python

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here