@ByteToBasics
#python
#python2
#python3
#difference_between_python2_and_python3
#python2vspython3
Difference between python version 2 and python version 3
Python 2 and Python 3 are two major versions of the Python programming language. Python 2 was officially discontinued on January 1, 2020, and Python 3 is the actively maintained version. Below is a comparison of their key differences:
________________________________________
1. Print Statement vs Print Function
• Python 2: print is a statement and does not require parentheses.
python
Copy code
print "Hello, World!" # Valid in Python 2
• Python 3: print is a function and requires parentheses.
python
Copy code
print("Hello, World!") # Valid in Python 3
________________________________________
2. Integer Division
• Python 2: Dividing two integers results in an integer.
python
Copy code
print(5 / 2) # Outputs 2 in Python 2
• Python 3: Dividing two integers results in a float. Use // for integer division.
python
Copy code
print(5 / 2) # Outputs 2.5 in Python 3
print(5 // 2) # Outputs 2 in Python 3
________________________________________
3. Unicode Support
• Python 2: Strings are stored as ASCII by default. Unicode strings need to be explicitly defined using a u prefix.
python
Copy code
s = u"Hello, Unicode" # Unicode in Python 2
• Python 3: Strings are stored as Unicode by default. The u prefix is optional and unnecessary.
python
Copy code
s = "Hello, Unicode" # Unicode in Python 3
________________________________________
4. xrange vs range
• Python 2: range returns a list, while xrange returns a generator (more memory efficient).
python
Copy code
for i in xrange(5): # Use xrange in Python 2 for efficiency
print(i)
• Python 3: range behaves like xrange in Python 2, returning a generator. xrange is removed.
python
Copy code
for i in range(5): # Use range in Python 3
print(i)
________________________________________
5. Input Handling
• Python 2:
o input() evaluates input as Python code.
o raw_input() takes input as a string.
python
Copy code
name = raw_input("Enter your name: ") # Use raw_input for strings
• Python 3:
o input() always takes input as a string.
python
Copy code
name = input("Enter your name: ") # Use input in Python 3
________________________________________
6. Libraries and Compatibility
• Many libraries developed for Python 2 were not immediately compatible with Python 3.
• Modern libraries now primarily support Python 3.
________________________________________
7. Exception Handling
• Python 2: Exceptions are handled using the as keyword optionally.
python
Copy code
try:
pass
except Exception, e: # Exception type and variable separated by a comma
print e
• Python 3: Use as to handle exceptions consistently.
python
Copy code
try:
pass
except Exception as e: # Use `as` keyword
print(e)
________________________________________
8. Iterators
• Python 2: Functions like dict.keys() and dict.items() return lists.
• Python 3: These functions return view objects (lazy iterators).
python
Copy code
Python 2
keys = my_dict.keys() # Returns a list
Python 3
keys = my_dict.keys() # Returns a view object
________________________________________
9. Community and Maintenance
• Python 2: No longer supported (end-of-life in 2020).
• Python 3: Actively supported and improved with new features.
________________________________________
Why Python 3?
• Python 3 is the present and future of the language, with ongoing development and enhancements.
• It offers better performance, more features, and improved compatibility with modern libraries and tools.
На этой странице сайта вы можете посмотреть видео онлайн Difference between python version 2 and python version 3 (python2 vs python3) длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь ByteToBasics 07 Декабрь 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 11 раз и оно понравилось 3 зрителям. Приятного просмотра!