Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: Ravishankar ChavareWelcome to Python
#1
hi guys today we are learning python from scratch

What is Python?

[Image: python-logo.png]
Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.

Should I use Python 2.x.x or Python 3.x.x for my development activity?

Python 2.x is legacy,
Python 3.x is the present and future of the language

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range.

If you want to port your python code from one version to another version then its hard to conversion so before creating python project first decide the what version you should use. read following tutorial carefully to understand how both version working and what is the difference between them

Which version should I use legacy or present?

Important difference between python 2.x and 3.x 

1.division operator
   when you are dividing 9 number with 2 then expected result should be 4.5

python 2.x:
9/2               --the answer is  4

python 3.x:
9/2               --the answer is  4.5

You can also same result in python 2.x but you need to modify the code as follows
Code:
To get desired output you need to try any one of the method from following
Python 2.7.14

>>> print 9/2
4
>>> print 9/2.0
4.5
>>> print 9.0/2
4.5
>>> print 9.0/2.0
4.5


2.Print Function
     The main difference of print 
           print from python 2.x replaced with print() function in python 3.x
Code:
print 'Hello, Python'      # Python 3.x doesn't support
print('Hello, Python')


3.local value Replaced to Global Variable

List comprehensions also "leak" their loop variable into the surrounding scope.
Code:
Python 2.x
>>> x = 'before'
>>> a = [x for x in (1, 2, 3)]
>>> x
3

This bug is fixed in Python3.x

Python 3.x
>>> x = 'before'
>>> a = [x for x in (1, 2, 3)]
>>> x
'before'

4.xrange

xrange() of Python 2.x doesn’t exist in Python 3.x. 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.
Code:
for x in xrange(1, 5):
   print(x),

Output in Python 2.x
1 2 3 4 1 2 3 4


for x in range(1, 5):
   print(x),

Output in Python 3.x
NameError: name 'xrange' is not defined

5.Error handling

  there is  a small change in both version of python . Python 3.x  uses 'as' keyword
Code:
Python 2.x
try:
   trying_to_check_error
except NameError, err:
   print err, 'Error Caused'  


Python 3.x
try:
    trying_to_check_error
except NameError as err: # 'as' is needed in Python 3.x
    print (err, 'Error Caused')


Note:
_future_module


_future_module help you for migration purposes.
We can use Python 3.x If we are planning Python 3.x support in our 2.x code,we can use _future_ imports it in our code
for example you want to add integer division module of python 3.x into python 2.x then you can use _future_module as following
Code:
from __future_import division

print 9/2 now this will generate output as 4.5


Today we Just learned What is python And information about two different version of python in next tutorial we will learn how to install python and how to set python environment in your local machine
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)