You are on page 1of 6

1.

4 Python – The new generation Language


https://www.geeksforgeeks.org/python-the-new-generation-language/

Python designed by Guido van Rossum at CWI has become a widely used general-
purpose, high-level programming language.

Prerequisites:
Knowledge of any programming language can be a plus.

Reason for increasing popularity


1. Emphasis on code readability, shorter codes, ease of writing
2. Programmers can express logical concepts in fewer lines of code in
comparison to languages such as C++ or Java.
3. Python supports multiple programming paradigms, like object-
oriented, imperative and functional programming or procedural.
4. There exists inbuilt functions for almost all of the frequently used
concepts.
5. Philosophy is “Simplicity is the best”.
>>> import this #Zen of Python Philosophy of Python

LANGUAGE FEATURES

Interpreted
1. There are no separate compilation and execution steps like C and C+
+.
2. Directly run the program from the source code.
3. Internally, Python converts the source code into an intermediate
form called bytecodes
which is then translated into native language of specific
computer to run it.
4. No need to worry about linking and loading with libraries, etc.

Platform Independent
1. Python programs can be developed and executed on multiple operating
system platforms.
2. Python can be used on Linux, Windows, Macintosh, Solaris and many
more.

Free and Open Source


1. Redistributable

High-level Language
1. In Python, no need to take care about low-level details such as
managing the memory used by the program.

Simple
1. Closer to English language;Easy to Learn
2. More emphasis on the solution to the problem rather than the syntax

Embeddable
1. Python can be used within C/C++ program to give scripting
capabilities for the program’s users.

Robust
1. Exceptional handling features
2. Memory management techniques in built

Rich Library Support


1. The Python has immense Standard Library.
2. Known as the “batteries included” philosophy of Python ;It can help
do various things involving regular expressions, documentation generation, unit
testing, threading, databases, web browsers, CGI, email, XML, HTML, WAV files,
cryptography, GUI and many more.
3. Besides the standard library, there are various other high-quality
libraries such as the Python Imaging Library which is an amazingly simple image
manipulation library.

Python vs JAVA
Python

Java

Dynamically Typed

Statically Typed
1.No need to declare anything.

1.All variable names (along with their types) must be


explicitly declared.
An assignment statement binds a name to an object, and the object
can be of any type.
Attempting to assign an object of the wrong type to a variable name triggers
a type exception
2.No type casting required when using container objects

2.Type casting is required when using container objects.


Concise; Express much in limited words

Verbose; Contains more words


Compact

Less Compact
Uses Indentation for structuring code

Uses braces for structuring code

The standard "Hello World" reveals the absolute verbosity of a Java and Python
program.

Java Code

public class HelloWorld


{
public static void main (String[] args)
{
System.out.println("Hello, world!");
}
}

Python Code

print("Hello, world!")
Similarity with Java
1. Require some form of runtime on your system (JVM/Python runtime)
2. Can probably be compiled to executables without the runtime (this is
situational, none of them are designed to work this way)

LOOK and FEEL of the Python


--> GUI : IDLE, or some application
--> Command Line interface

Softwares making use of Python

Python has been successfully embedded in a number of software products as a


scripting language.

1. GNU Debugger uses Python as a pretty printer to show complex


structures such as C++ containers.
2. Python has also been used in artificial intelligence
3. Python is often used for natural language processing tasks.

Current Applications of Python

A number of Linux distributions use installers written in Python


example in Ubuntu we have the Ubiquity
Python has seen extensive use in the information security industry,
including in exploit development.
Raspberry Pi– single board computer uses Python as its principal user-
programming language.
Python is now being used Game Development areas also.

Pros:

1. Ease of use
2. Multi-paradigm Approach

Cons:

1. Slow speed of execution compared to C,C++


2. Absence from mobile computing and browsers
3. For the C,C++ programmers switching to python can be irritating as
the language requires proper indentation of code. Certain variable names commonly
used like sum are functions in python. So C, C++ programmers have to look out for
these.

Industrial Importance!

https://www.geeksforgeeks.org/important-differences-between-python-2-x-and-python-
3-x-with-examples/
1. Division operator
2. print function
3. Unicode
4. xrange
5. Error Handling
6. _future_ module

Division operator
If we are porting our code or executing python 3.x code in python 2.x,
it can be dangerous if integer division changes go unnoticed (since it
doesn’t raise any error).
It is preferred to use the floating value (like 7.0/5 or 7/5.0) to get the
expected result when porting our code.

>>> print 7 / 5
>>> print -7 / 5

Output in Python 2.x


1
-2
Output in Python 3.x :
1.4
-1.4

print function

This is the most well-known change.


In this, the print keyword in Python 2.x is replaced by the print() function
in Python 3.x.
However, parentheses work in Python 2 if a space is added after print keyword
because the interpreter evaluates it as an expression.

>>> print 'Hello, Geeks' # Python 3.x doesn't support


>>> print('Hope You like these facts')

Output in Python 2.x :

Hello, Geeks
Hope You like these facts

Output in Python 3.x :


File "a.py", line 1
print 'Hello, Geeks'
^
SyntaxError: invalid syntax
As we can see, if we use parentheses in python 2.x then there is no issue but
if we don’t use parentheses in python 3.x, we get SyntaxError.

Unicode
In Python 2, implicit str type is ASCII. But in Python 3.x implicit str type
is Unicode.

>>> print(type('default string '))


>>> print(type(b'string with b '))
'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>
Output in Python 3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''

xrange

1. xrange() of Python 2.x doesn’t exist in Python 3.x.


2. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while
xrange returns a xrange object i. e., xrange(3) returns iterator object which work
similar to Java iterator and generates number when needed.
3. If we need to iterate over the same sequence multiple times, we prefer
range() as range provides a static list.
4. xrange() reconstructs the sequence every time. xrange() doesn’t support
slices and other list methods. The advantage of xrange() is, it saves memory when
the task is to iterate over a large range.
5. In Python 3.x, the range function now does what xrange does in Python 2.x,
so to keep our code portable, we might want to stick to using range instead. So
Python 3.x’s range function is xrange from Python 2.x.

for x in xrange(1, 5):


print(x),

for x in range(1, 5):


print(x),
'''

Output in Python 2.x


1 2 3 4 1 2 3 4

Output in Python 3.x


NameError: name 'xrange' is not defined

'''

Error Handling

There is a small change in error handling in both versions. In python 3.x,


‘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
'''

__future__ module

This is basically not a difference between the two versions, but a useful
thing to mention here.
The idea of __future__ module is to help migrate to Python 3.x.

If we are planning to have Python 3.x support in our 2.x code, we can use
_future_ imports in our code.
For example, in the Python 2.x code below, we use Python 3.x’s integer
division behavior using the __future__ module.

# In below python 2.x code, division works


# same as Python 3.x because we use __future__
from __future__ import division
print 7 / 5
print -7 / 5

Output :
1.4
-1.4

Another example where we use brackets in Python 2.x using __future__ module:

from __future__ import print_function


print('GeeksforGeeks')

Output:
GeeksforGeeks

You might also like