Python Programming Introduction

This python tutorial is trying to cover basic concepts, all the features in Python language and also has some hands on in python programming.
Python is easy to use, and python can be installed on Windows, Mac OS X, and Unix operating systems.

What is python?

  • Who is developed python?
    Developed by Guido van Rossum at the National Research Institute for Mathematics and Computer Science in the Netherlands.
  • Python is a high-level, interpreted, interactive and object-oriented scripting language.
  • Interpreted - python code is processed at runtime by the interpreter and do not need to compile your program before executing it. Python interpreter can be easily extended with new functions and data types implemented in C or C++ programming languages.
  • Interactive - python prompt is available to interact with the interpreter directly.
  • Object-Oriented -python supports Object-Oriented programming.
  • Highly readable.
  • Provides the support to write functional and structured programming as well.
  • Automatic garbage collection.
  • High-level dynamic data types.

Python advantages

  • Python is a real programming language, provides more structure and support for large programs.
  • Python provides more error checking than C.
  • Python is an interpreted language, so it reduces considerable time during program development because no compilation and linking.
  • Python supports to write program compactly and readably. Python programs are typically much shorter than equivalent program in C, C++, or Java beacuase
    • high-level data types help to write complex operations in a single line.
    • no beginning and ending brackets, statements are grouped using indentation.
    • variable or argument declarations are not available.
  • Python is extensible because new built in functions or module can be added in python interpreter if you know how to write C program.

Python installation

Below command is used to install python on fedora 24
sudo dnf install python
Download python package and install on the required environment manually.

Python interpreter

  • Python interpreter is written in C program which directly executes or performs python scripts.
  • Python interpreter is usually installed under /usr/local/bin/python.
  • Python interpreter can be invoked using command
    python
    
  • Arguments can be passed to python program. After python scripts name, remaining arguments are converted as list of strings and assigned to variable argv and it is available in sys module. This argv variable can be accessed in python program
    import sys
    # Empty string if no arguments, otherwise written argument string.
    print sys.argv[0]
    
  • Python interactive mode: python interpreter provides interactive mode where we can run python scripts in command line.
    python command is used to get into python interactive mode.
    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36) 
    [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    Python scripts can be executed using interactive mode as below.
    >>> print "welcome"
    welcome
    >>> 
    
    Python interactive mode may also be used as calculator.
    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36) 
    [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 2+2
    4
    >>> 
    
    To exit the python interactive mode: type exit() or quit() or press ctrl+D
    >>>exit()
    or
    >>>quit()
    
  • Source Code Encoding - Python scripts files are encoded in UTF-8 format default. A special comment line should be added in first line of python scritps to declare an encoding other than the default format.
    # -*- coding: encoding -*-
    
    if source code starts with a UNIX “shebang” line, then encoding comment should be in second line.
    #!/usr/bin/env python
    # -*- coding: cp-1252 -*-
    

How to format json data in python?

#!/usr/bin/python

import json

with open('compute-0.json', 'r') as rf:
      json_str = rf.read()

parsed = json.loads(json_str)

print json.dumps(parsed, indent=4, sort_keys=True)

How to invoke shell command in pyhton?

#!/usr/bin/python
import subprocess

#proc=subprocess.Popen('ls -l',stdout=subprocess.PIPE)
#tmp=proc.stdout.read()
#print tmp
# To run shell command in python code
cmd="python program.py"
p=subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).wait()
#p.wait()
#p.communicate()
while True:
  out = p.stderr.read(1)
  if out == '' and p.poll() != None:
      break
  if out != '':
      sys.stdout.write(out)
      sys.stdout.flush() 

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^