What is New in Python 3
Python 3 is an updated version of the popular high-level programming language, intended to be backward compatible with Python 2 while introducing several new features and changes. While Python 2 is still supported, Python 3 is where the future lies. Python 3 brings a lot of changes and improvements, making coding easier and more efficient. Here’s a look at some of Python 3’s most significant changes:
Unicode by Default
One of the most significant changes in Python 3 is that all strings are Unicode by default. This means that, unlike in Python 2, users don’t have to worry about encoding and decoding to modify strings. For instance, the following code in Python 2:
print "Hello, world!"
would become:
print("Hello, world!")
in Python 3.
Print Function
In Python 2, the print statement is used to output content to the screen. In Python 3, the print statement has been replaced with the print function. The print function takes zero or more arguments, formats them following the rules of the format operator, and then writes the resulting string to the standard output stream.
# Output: Hello, world!
print("Hello, world!")
Type Annotations
Python 3 supports function annotations, which is used to provide additional information about a function (or a method) to Python itself, the developer, and tools. Function annotations, in this case, can be used to specify parameter and return types.
def add(x:int, y:int) -> int:
return x+y
Immutable Dictionaries
Python 3 provides the new dictionary type that preserves insertion order. Dictionaries are now a bit more efficient as Python 3 uses a hashtable-based implementation.
new_dict = {"one":1, "two":2, "three":3}
print(new_dict)
# Output: {'one': 1, 'two': 2, 'three': 3}
updated_dict = {**new_dict, "four":4}
print(updated_dict)
# Output: {'one': 1, 'two': 2, 'three': 3, 'four': 4}
Exception Chains
When Python raises an exception, it’s possible to catch it and then raise a different exception or the same exception with a different error message. Exception chains allow you to keep track of what happened and where.
try:
2/0
except ZeroDivisionError as e:
raise ValueError("Invalid Input") from e
Asyncio
Python 3 comes with built-in support for asynchronous programming. The asyncio module offers a flexible framework for writing event-driven programs. It aims to simplify writing concurrent code by using coroutines, which are an alternative to threads. This allows developers to write highly concurrent and scalable applications with ease.
import asyncio
async def internet():
await asyncio.sleep(5)
print("Connected to the internet")
loop = asyncio.get_event_loop()
loop.run_until_complete(internet())
f-Strings
f-strings are a new way to format strings in Python 3. They are concise, readable, and more powerful than the older ways of formatting strings.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Conclusion
Python 3 brought big changes to the world of programming. It’s Unicode support, the switch from print statements to print functions, and more efficient dictionaries enhance Python 3’s flexibility and performance. Python 3 also supports function annotations, which provides developers with more information about the functions they’re working on.
Exception chains now allow developers to see how and why exceptions are happening, making debugging easier. asyncio was introduced to help make concurrent programming and highly scalable applications with coroutines. Finally, f-strings were added as a more concise and efficient way to format strings. All these new features make Python 3 a go-to for any developer!