Python Introduction

python

What is Python?

Python is an interpreted, high-level, general-purpose programming language. Created by Guido Van Rossum and first released in 1991. Python is typed dynamically and garbage-collected. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

Python is simple and easy to learn and provides lots of high-level data structures. It is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application development.

Python is often described as a “batteries installed” language due to its comprehensive standard library. The language’s core philosophy is summarized in the document. The Zen of Python that comes within python, when we execute the command “import this” we can see :

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

Python programs generally are smaller than other programming languages like Java,C. Instead of semicolons and curly braces python uses indentation i.e. white spaces. rather than having all of its functionality built into its core, Python was designed to be highly extensible.

Facts about Python

  • Python is a general-purpose, high-level programming language.
  • Allows programming in Object-oriented and procedural paradigms.
  • Designed for readability and has some similarities to the English language with influence from mathematics.
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolon or parenthesis.
  • It relies on indentation, using white spaces, to define scopes, such as the scope of loops, functions, and classes. Other programming languages often use curly-brackets for this purpose.
  • Python language is being used by almost all tech-giant companies like – Google, Amazon, Facebook, Instagram, Dropbox, Uber ….etc.

What can Python do?

  • Python can be used on a server to create web applications.
  • It is used alongside software to create workflows.
  • Connect, read and modify database systems.
  • Complex mathematics and handling of big data can be done with python.
  • Rapid prototyping or production-ready software development.

Why use Python?

  • Python works on different platforms like Windows, Mac, Linux, Freebsd, Raspberry Pi, etc.
  • Development and debugging is fast because there is no compilation step included in Python development, and edit-test-debug cycle is very fast.
  • Python language can be treated in procedural way, an object oriented way or a functional way.
  • It has simple syntax similar to the English language.
  • The Python syntax allows developers to write programs with fewer lines than some other programming languages.

Python 2 vs. Python 3

In most of the programming languages, whenever a new version is releases, it supports the features and syntax of the existing version of the language, therefore,it is easier for the projects to switch in the newer version. However, in case of Python, the two versions Python2 and Python3 are very much different from each other.

print function

A list of difference between Python2 and Python3 are given below:

This is the most well-known change. the print function in Python 2.x is replaced by print() in Python3.x,i.e. to print in Python3.x an extra pair of parenthesis is required.

print 'Hello, World!'  # Not supported in Python3.x
print ('How are you?')

'''
Output in Python2.x:
Hello, World!
How are you?

Output in Python3.x :
File "hello.py", line 1
	print 'Hello, World!'
                        ^
SyntaxError: invalid syntax

{{cause python is an interpreted language, when it encounters an error it stops the execution of next commands. So, now we will need to type our command again}}

Output in Python3.x:
How are you?

As we can see, if we use parenthesis in python 2.x then there is no issue but if we don’t use parenthesis in python3.x, we get SyntaxxError.

input function

In Python2.x raw_input() function is used to take user’s input. It returns the string representing the value, To convert it into string we need to use int() function. On the other hand, in Python3.x input() function is used to get user input which automatically interprets the type of input entered by user. However, we can cast this value to any type by using primitive functions (int(), str(), etc).

# python 2.x.x
n = raw_input("Enter anythnig : ")
r = input("Enter anything : ")
print type(n)
print type(r)

'''
Output :-
Enter anything : 45
Enter anything : 45
type 'str'
type 'int'
'''

# python 3.x.x
n = input("Enter anything : ")
r = raw_input("Enter anything : ")

'''
Output :-
Enter anything : 45
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined

'''

Unicode

In Python2, the implicit string type is ASCII, whereas, in Python3, the implicit string type is Unicode.

print (type('default string'))
print (type(b'string with b'))

'''
Output :-
Python2.x (Bytes is same as str)
<type 'str'>
<type 'str'>

Output :-
Python3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''

# Python 2.x  also supports Unicode
print(type('default string'))
print(type(u'string with u'))

'''
Output :-
Python 2.x (Unicode and str are different)
<type 'str'>
<type 'unicode'>

Python 3.x (Unicode and str are same)
<class 'str'>
<class 'str'>
'''

Error Handling

There is a small change in error handling in both versions. in python 3, ‘as’ keyword is required

try: 
    trying_to_check_error 
except NameError, err: 
    print err, 'Error Caused'   # Would not work in Python 3.x 
  
''' 
Output in Python 2.x: 
name 'trying_to_check_error' is not defined Error Caused 
  
Output in Python 3.x : 
File "a.py", line 3 
    except NameError, err: 
                    ^ 
SyntaxError: invalid syntax 
'''
try: 
     trying_to_check_error 
except NameError as err: # 'as' is needed in Python 3.x 
     print (err, 'Error Caused') 
  
''' 
Output in Python 2.x: 
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused') 
  
Output in Python 3.x : 
name 'trying_to_check_error' is not defined Error Caused 
'''

Conclusion

Python is a simple, general-purpose, high level, and object-oriented programming language. Python is an interpreted scripting language also. Guido Van Rossum is known as the founder of Python programming. it is easy to learn yet powerful and has a simple syntax.

Python has two versions python 2.x and python 3.x. those who are new to programming or python should learn python3.x cause python 2.x is not getting any updates and is going to be discontinued in 2020.