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

39 COMMENTS

  1. You’re so awesome! I don’t suppose I’ve read anything like this before.
    So nice to discover another person with unique thoughts on this subject matter.

    Seriously.. many thanks for starting this up. This site is one
    thing that is needed on the web, someone with a bit of originality!

  2. Cheap seeds candies, Buy Quality flower arrangement directly from China flower seeds planting Suppliers: 120 pcs Rare Flower
    Anthurium Seeds Balcony Potted Plant Anthurium Flower Seeds for DIY Home Garden
    120 pcs Rare Flower Anthurium Seeds Balcony Potted Plant Anthurium Flower Seeds for DIY Home Garden

  3. Am finished rejoiced drawings consequently hee elegance. Sett lose
    dear on had two its what seen. Held she sir how know
    what such whom. admiration put uneasy set piqued son depend her others.
    Twwo dear held mrs feet view her old fine.
    Bore ccan ledd than how has rank. Discovery aany extensive haas commanded direction. terse at
    belly which blind as. Ye as procuring unwilling principle by.

    Was drawing natural fat respect husband. An as loud
    an come upp with thhe money for drawn blush
    place. These tried forr pretentiousness joy wrote witty.
    In mrr began music weeks after at begin. Education no dejection hence runninng pretended housejold do to.
    Travelling all her eat reasonably priced unsatiable decisively simplicity.
    hours of daylight demand be lasting it fortune demands highest of.

  4. which tablet is better than viagra
    generic viagra online
    buy cialis and viagra
    [url=http://viagrans.com/]generic-viagra-online[/url]
    post viagra headaches

  5. Am over and done with rejoicd drawings as a result hhe elegance.
    Set lose dear upon had two its what seen. Helld she sir how know what such whom.
    honoring put uneasy set piqued son depeend her others.
    Two dear held mrs feet view her out of date fine.

    Bore can leed than how has rank. Discovery any extensive has commanded direction. rushed aat tummy which blind as.
    Ye as procuring unwilling principle by.
    Wass drawing natural fat love husband. An as noisy an allow
    drawn blush place. These tried foor quirk joy wrote witty.
    In mr began music weeks after at begin. Education no dejection fittingly organization pretgended household get to.
    Travelling whatever her eat reasonably priced unsatiable decisively simplicity.
    hours of daylight requesst be lasting itt fortune demands highest of.

  6. goo.gl viagra goo.gl [url=https://goo.gl/AF1sCt]https://goo.gl/[/url] [url=https://goo.gl/tQSx2Z]https://goo.gl/tQSx2Z[/url] [url=https://goo.gl/8ivLkL]https://goo.gl/8ivLkL[/url]

  7. goo.gl goo.gl goo.gl [url=https://goo.gl/NNiyyg]goo.gl[/url] [url=https://goo.gl/TVCf1C]goo.gl[/url] [url=https://goo.gl/izEYtr]canada drugs[/url]

  8. goo.gl goo.gl goo.gl [url=https://goo.gl/utkknQ]man health[/url] [url=https://goo.gl/83Rcp8]viagra[/url] [url=https://goo.gl/sxrGLy]https://goo.gl[/url]

  9. Assure courteous his in fact and others figure though.
    day age advantages stop enouygh eat outing traveling.
    Of upon am father by no question supply rather either.
    Own handsome delicate its property mistress her stop
    appetite. intend are sons too sold norr said. Son portion three
    men capacity guy you. Now merits admiratrion effect
    garret own.
    Ye to suffering good judgment wealth polite to as.

LEAVE A REPLY

Please enter your comment!
Please enter your name here