Site icon DataFlair

What’s New in Python 3.6 ? | New Features in Python 3.6

What's New in Python 3.6 ? | New Features in Python 3.6

What's New in Python 3.6 ? | New Features in Python 3.6

Python course with 57 real-time projects - Learn Python

1. Python 3.6 Tutorial

In this article on Python 3.6, we will discuss the following new features in Python 3.6. Moreover, we will discuss

So, let’s start the Python 3.6 Tutorial.

What’s New in Python 3.6? | New Features in Python 3.6

2. What’s new in Python 3.6?

We know that the future belongs to Python 3.x. But we have a number of versions in Python 3000. For example, Python 3.3.3 and Python 3.4.3. The latest version of Python is 3.6. So, let’s see what’s new in Python 3.6 and how it is different from older versions, Python 3.6 performance and features.

3. New Syntax Features  of Python 3.6

With Python 3.6, we see some new syntax features. Let’s take a look.

a. PEP 498 (Formatted String Literals)

You guessed it right. PEP 498 introduces f-strings.

An f-string is a string that you can format to put values inside it. For this, we precede the string with an ‘f’ or an ‘F’. We mention the variables inside curly braces.

>>> hometown,city='Ahmedabad','Indore'
>>> print(f"I love {hometown}, but I live in {city}")

I love Ahmedabad, but I live in Indore

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

For more on f-strings, read Python Strings.

b. PEP 526 (Syntax for Variable Annotations)

PEP 484 standardizes type annotations for function parameters. We also call these type hints.

>>> class A:
    name:str
>>> A.__annotations__
{'name': <class 'str'>}

Annotations do not pose meaning to the interpreter, and are of use to third-party tools and libraries.

c. PEP 515 (Underscores in Numeric Literals)

With PEP 515, we can use underscores in numeric literals- between digits and after base specifiers.

>>> 0x_FF_FE_FF_FE

4294901758

d. PEP 525 (Asynchronous Generators)

PEP 492 introduced native coroutines and async/await syntax. Unlike Python 3.5, Python 3.6 can have await and yield in the same function body. So, we can define asynchronous generators:

>>> async def ticker(delay, to):
   """Yield numbers from 0 to *to* every *delay* seconds."""
   for i in range(to):
       yield i
       await asyncio.sleep(delay)

This makes code faster and more concise.

e. PEP 530 (Asynchronous Comprehensions)

With PEP 530, you can use async for in lists, sets, dict comprehensions, and generator expressions.

result = [i async for i in aiter() if i % 2]

f. PEP 487 (Simpler Customization of Class Creation)

Now, we don’t need a metaclass to customize subclass creation. Whenever we create a new subclass, it calls the __init_subclass__ classmethod.

class PluginBase:
   subclasses = []
   def __init_subclass__(cls, **kwargs):
       super().__init_subclass__(**kwargs)
       cls.subclasses.append(cls)
class Plugin1(PluginBase): pass
class Plugin2(PluginBase): pass

g. PEP 495 (Local Time Disambiguation)

PEP 495 introduces the ‘fold’ attribute to instances of the datetime.datetime and datetime.time classes. This way, it can differentiate between two moments in time with the same local times.

h. PEP 529 (Change Windows filesystem encoding to UTF-8)

With Python 3.6, no data loss occurs when we use bytes paths on Windows.

i. PEP 528 (Change Windows console encoding to UTF-8)

Now, the default console on Windows accepts all Unicode characters. It also provides correctly-read str objects to Python code. Now, sys.stdin, sys.stdout, and sys.stderr default to utf-8 encoding.

j. PEP 520 (Preserving Class Attribute Definition Order)

With PEP 520, the natural ordering of attributes in a class is preserved in the class’ __dict__ attribute. Now, the default effective class ‘execution’ namespace is an insertion-order-preserving mapping.

k. PEP 468 (Preserving Keyword Argument Order)

Python now guarantees that **kwargs in a function signature is an insertion-order-preserving mapping.

l. PEP 523 (Adding a frame evaluation API to CPython)

PEP 523 introduces an API to make frame evaluation pluggable at the C level. This way, tools like debuggers and JITs can intercept frame evaluation before the Python code even begins to execute.

These are the Python 3.6 new feature syntax.

Read:13 Unique Features of Python Programming Language

4. Other Additions in Python 3.6

The is some this what’s extremely new in Python 3.6.

5. New Modules in Python 3.6

a. secrets

Python 3.6 introduces a new module, ‘secrets’. This module lends us a way to reliably generate cryptographically strong pseudo-random values. Using these, we can manage secrets like account authentication, tokens, and so.

What’s new in python 3.6 – Secrets

Any doubt yet in What’s new in Python 3.6 tutorial because now there is a long list of Improved modules. Also refer this article on Python Modules vs Packages.

6. Improved Python 3.6 Modules

Why stop at what we have, when we can tweak it into something even more awesome? Python 3.6 makes the following improvements:

>>> from cmath import tau
>>> tau

6.283185307179586

Collection ABC- to represent sized iterable container classes.

Reversible ABC- to represent iterable classes. These also provide the method __reversed__().

AsyncGenerator ABC- to represent asynchronous generators.

Other than these, the namedtuple() function will now accept an optional keyword argument ‘module’. Now, the arguments ‘verbose’ and ‘rename’ for namedtuple() are keyword-only. Finally, we can now pickle recursive collections.deque instances.

The function datetime.isoformat() takes an optional argument ‘timespec’.
The function datetime.combine() takes an optional argument tzinfo.

>>> Decimal('-3.14').as_integer_ratio()

(-157, 50)

>>> from enum import Enum, auto
>>> class Color(Enum):
    red=auto()
    blue=auto()
    green=auto()
>>> list(Color)
[<Color.red: 1>, <Color.blue: 2>, <Color.green: 3>]

Read: Python Collections Module

>>> def f(): f()
...
>>> f()

Traceback (most recent call last):

File “<stdin>”, line 1, in <module>

File “<stdin>”, line 1, in f

File “<stdin>”, line 1, in f

File “<stdin>”, line 1, in f

[Previous line repeated 995 more times]

RecursionError: maximum recursion depth exceeded

So, this is all on what’s new in Python 3.6 Tutorial. Hope you like our explanation.

7. Conclusion

In this article on what’s new in Python 3.6, we discussed what changes have been made to Python 3.5 to make it Python 3.6. Tell us how you like them the what’s new in Python 3.6 article.

Exit mobile version