Python Programming
Topics:

Introduction

Programming is playing with data. Any programming language you use is focused on data input, data processing and data output.

Concepts in almost every programming language is usually explained with below chapters.
1) Basic input and output
2) Variables and Operators
3) Data types and data structures
4) Control flow statements
5) Functions, modules and libraries
6) Exception handling
7) File handling 8) Database connections
9) Classes and objects (if it is object oriented)
10) Regular expressions

For a selcted programming language the source of data might be a) console input (std.in), b) file input in different formats like txt, csv, xml, json etc., c) databases like MySQL, PostgreSQL, MongoDB etc., d) webpage scrapping, e) socket streaming and many more. Same is also true for data output destination. When the data comes from any source it is stored randomly in memomy, because serial storage is ineffective for a multi-tasking systems. The data scattered around memory needs to maintain a reference to be called further, which is variable. We also need operators to manipulate the data.

The data that we deal with is mostly numbers or string or a collection of them. Programming languages differ in the way they structure and organize data as different data types. This is what happens at input/output level.

For data processing as we desire and to make complex programming logics we may need to alter the flow of program control. By default program code is executed top to bottom for the file and left to right on every line with a possible exception of assignment operator where it goes right to left. Altering this is necessary to make better and logical decision. This task is done by control flow statements, typically if-else conditions, for loop, while loop etc.

Futhermore, a program structure is needed to seperate one code block from another. This comes with function definition. In the absence of functions your whole file will be executed whereas with a function defition you can bundle few lines of code as one reusable structure and give it a callable name.

If the program reuseability is to be increased then one can use classes and objects. If the data produced is too much to understand, one can use regular expression. And even after all the efforts if some error comes we can add exception handling to our code. This makes up the basics of any programming language you select.

Introduction to Python and it's greatest features


Python has gained popularity as a simple but powerful programming language. Python is also an interactive, interpreted and object-oriented.

Interactive because there is an additional mode of executing Python code, other than running .py files. The mode with prompt like this '>>>' is called interactive mode or python shell because it mimics the human interaction and gives immediate reply for your instructions.

Python is also an interpreted language where code is run line by line and a lot of assignments are made at runtime. It is a good feature considering that interpreted languages are more flexible and portable.

Object orientation is not new to Python, it has been there since it's invention in 1989 by Guido Van Rossum. Some languages associate the data with action(function) and some other with behavioural template(classes), python in reality does both. It is called object oriented bacause it offers full features of object orientation like inheritance, overriding, overloading and data hiding. Despite this a class definition is not compulsory in python and most of the data manipulation is done with functions.

With all these great features there is no price tag attached and Python comes as an open source with great community support. What makes python simpler is the use of english dictionary words as python syntax and even the code is written in plain and straightforward manner.

Installing Python


Step 1:

Download python from: www.python.org/downloads/ or visit http://devu.in/downloads/

Step 2:

If you are a windows user goto command mode and run:

C:\Python34\Scripts\pip.exe install jupyter

Step 3:

Start jupyter notebook at any folder path where you want to save your files. Jupyter notebook opens in web browser.

jupyter notebook

Step 4:

Make new Python3 notebook and start coding.

First Python Program: "Hello World"


Try it!print("Hello World")
Hello World

Basic Input/Output


input() is the function responsible for data input from user via <std.in>. The data type though is always string and an additional type casting is required if you mean anything else. Other primitive data types in python are:

  1. Numbers (int, float, complex)
  2. String
  3. List
  4. Tuple
  5. Dictionary
age = input("Enter your age: ")
print(type(age))

# <class 'str'>

age = int(input("Enter your age: "))
print(type(age))

# <class 'int'>

print() is the function to display any value at <std.out>

a = 10
b = 90
print("Sum",a+b)

# Sum 100

Objective Questions

  1. Which of the following is a not a DataType in Python.
    a) int
    b) list
    c) array
    d) dictionary

  2. Which of the following is not the correct way to define variable in Python.
    a) x = 100;
    b) x = 100
    c) int x = 100;
    d) x = 10,20,30

  3. Which of the following is not a feature of Python programming?
    a) Object-oriented
    b) Interactive
    c) Interpreted
    d) None of these