Back to Python
2026-04-106 min read

Python 3.6 (EOL)

Learn Python 3.6 (EOL) step by step with clear examples and exercises.

Title: Mastering Python 3.6 (EOL) - A full guide for Real-World Applications

Why This Matters

Python 3.6 was a significant leap forward in Python's development, introducing numerous new features and enhancements that boosted its power and efficiency. Although it has reached its end of life (EOL), mastering Python 3.6 is essential for debugging legacy code, preparing for interviews, and gaining valuable insights into the foundational principles that have been refined in subsequent versions.

Python 3.6 introduced several key changes that made it more efficient, flexible, and powerful:

  1. Syntax Changes: The introduction of the async and await keywords in Python 3.6 simplified asynchronous programming, allowing developers to write non-blocking concurrent code more effectively without hampering the event loop.
  2. New Built-in Functions: Python 3.6 added several new built-in functions like bytes.fromhex(), bytes.hex(), and bytearray.hex() for handling hexadecimal representations of bytes, making it easier to work with hex data in Python.
  3. Improved Performance: Python 3.6 saw performance improvements across various areas, such as the built-in sort function, which now utilized Timsort instead of Mergesort, leading to faster sorting times.
  4. Division Operator Changes: In Python 3.6, the division operator (/) always performs floating-point division, even when both operands are integers. This change helps prevent unexpected results and ensures consistency with other programming languages' behavior.

Prerequisites

To derive maximum benefit from this lesson, you should possess a strong foundation in programming fundamentals such as variables, data structures, control flow, functions, classes, and object-oriented programming (OOP). Familiarity with Python syntax is also crucial. If you're new to Python, consider first reviewing our Python for Beginners series before diving into this lesson.

Core Concept

Python 3.6 brought about several remarkable changes and improvements:

Syntax Changes

The introduction of the async and await keywords in Python 3.6 simplified asynchronous programming, allowing developers to write non-blocking concurrent code more effectively without hampering the event loop.

async def greet(name):
await asyncio.sleep(1)
print(f"Hello, {name}!")

import asyncio

async def main():
await asyncio.gather(*[greet(name) for name in ['Alice', 'Bob']])

asyncio.run(main())

In this example, we create an asynchronous function greet() that waits for one second before printing a personalized greeting. The asyncio.gather() function allows us to run multiple asynchronous tasks concurrently in the same script. Using the new asyncio.run() function, we can execute this asynchronous code with ease.

New Built-in Functions

Python 3.6 added several new built-in functions like bytes.fromhex(), bytes.hex(), and bytearray.hex() for handling hexadecimal representations of bytes, making it easier to work with hex data in Python.

hex_string = '48656c6c6f'
bytes_object = bytes.fromhex(hex_string)
print(bytes_object) # Output: b'Hello'

In this example, we convert a hexadecimal string to bytes using the bytes.fromhex() function.

Improved Performance

Python 3.6 saw performance improvements across various areas, such as the built-in sort function, which now utilized Timsort instead of Mergesort, leading to faster sorting times.

numbers = list(range(10000))
sorted_numbers = sorted(numbers)
print("Sorted numbers:", sorted_numbers)

In this example, we sort a large list of numbers using the built-in sort() function in Python 3.6.

Division Operator Changes

In Python 3.6, the division operator (/) always performs floating-point division, even when both operands are integers. This change helps prevent unexpected results and ensures consistency with other programming languages' behavior.

print(5 / 2) # Output: 2.5

In this example, we perform integer division in Python 3.6 using the division operator (/).

Worked Example

Let's dive into a practical example that demonstrates how to use some of the new features introduced in Python 3.6:

Creating an Asynchronous Web Scraper

In this worked example, we will create an asynchronous web scraper using Python 3.6's built-in aiohttp library and the async and await keywords. Our goal is to fetch the titles of the top 10 articles from a popular news website.

import asyncio
import aiohttp

async def get_article_titles(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
html = await response.text()
soup = BeautifulSoup(html, 'lxml')
titles = [title.text for title in soup.find_all('h3')]
return titles

async def main():
urls = ['https://www.example.com/news', 'https://www.example.com/sports']
tasks = [get_article_titles(url) for url in urls]
titles = await asyncio.gather(*tasks)
for titles_list in titles:
print('\nArticles from:')
print(urls[titles.index(titles_list)])
for title in titles_list[:10]:
print(title)

asyncio.run(main())

In this example, we create an asynchronous function get_article_titles() that fetches the HTML content of a given URL using the aiohttp library and then extracts the titles using BeautifulSoup. The asyncio.gather() function allows us to run multiple tasks concurrently in the same script, ensuring faster execution times.

Common Mistakes

  1. Asynchronous Programming Misconceptions: Newcomers to Python 3.6 often grapple with understanding the intricacies of asynchronous programming and effectively utilizing the async and await keywords in their code.
  2. Using Deprecated Functions: Some functions that were deprecated in earlier versions of Python, such as raw_input(), are still present in Python 3.6 but have been replaced by newer alternatives like input(). Using deprecated functions can lead to unexpected behavior or errors.
  3. Neglecting to Import Required Modules: Always ensure that you import any necessary modules before using them in your code. For example, if you're working with asynchronous programming, make sure to import the asyncio module.
  4. Ignoring Context Managers: Proper use of context managers like with statements can help optimize resource usage and improve performance in Python 3.6.
  5. ### Subheadings under Common Mistakes:
  • Misuse of async/await keywords
  • Overlooking deprecated functions
  • Neglecting to import required modules
  • Ignoring context managers

Practice Questions

  1. Write an asynchronous function that prints "Hello" after a 2-second delay and "World" after a 4-second delay.
  1. Convert the following hexadecimal string to bytes: '48656c6c6f'
  1. Implement a context manager for a file that automatically closes the file handle when it is no longer in use.

FAQ

  1. Why was Python 3.6 significant?
  • Python 3.6 introduced several new features, improved performance, and made asynchronous programming easier, making it a crucial milestone in Python's evolution.
  1. What are some common mistakes when working with Python 3.6?
  • Common mistakes include misunderstanding asynchronous programming, using deprecated functions, neglecting to import required modules, ignoring context managers, misuse of async/await keywords, overlooking deprecated functions, and neglecting to properly close file handles.
  1. How can I convert a hexadecimal string to bytes in Python 3.6?
  • You can use the bytes.fromhex() function to convert a hexadecimal string to bytes:
hex_string = '48656c6c6f'
bytes_object = bytes.fromhex(hex_string)
print(bytes_object) # Output: b'Hello'
  1. What is a context manager, and why are they important in Python 3.6?
  • A context manager is an object that controls the execution of a block of code, ensuring resources like file handles are properly managed. In Python 3.6, using context managers can help optimize resource usage and improve performance by automatically closing resources when they're no longer needed.
  1. ### Subheadings under FAQ:
  • Significance of Python 3.6
  • Common mistakes in Python 3.6
  • Converting hexadecimal strings to bytes in Python 3.6
  • Importance and usage of context managers in Python 3.6
Python 3.6 (EOL) | Python | XQA Learn