Open In App

API Changes For 3.8.0 in Python

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the changes in the API after the launch of Python 3.8.0. Python, a dynamic and widely-used programming language, continuously evolves with new releases, introducing enhancements and changes to improve developer productivity and code quality. Python 3.8.0, released in October 2019, introduced several significant changes and enhancements to the language and its standard library. In this article, we will explore some of the key API changes introduced in Python 3.8.0, along with examples to illustrate their usage. Python 3.8.0 is no exception, offering numerous API changes that impact the way developers write and structure their code. In this article, we will dive into some of the API changes introduced in Python 3.8.0, providing comprehensive examples and detailed explanations.

API Changes For 3.8.0

  • The Walrus Operator (Assignment Expressions)
  • Positional-Only Parameters
  • f-strings Improvements
  • Assignment Expressions in Comprehensions

The Walrus Operator (Assignment Expressions)

The Walrus Operator, also known as Assignment Expressions, is a feature introduced in Python 3.8.0 that allows you to assign values to variables within expressions. This operator is represented by ‘:=’ and is particularly useful in situations where you want to both compute a value and use it later. It can simplify code in scenarios like while loops and list comprehensions, as it helps avoid repetitive assignments and improves code readability.

In this code, we have a simple while loop. It initializes a variable n to 0 and then repeatedly prints the value of n as long as it is less than 5. In each iteration, n is incremented by 1. The loop continues until n reaches 5.

Python3




# Without the walrus operator
n = 0
while n < 5:
    print(n)
    n += 1


Output :

0 1 2 3 4

Now, let’s see how the Walrus Operator can be used to simplify this code:

In this version, we still initialize n to 0, but the while loop condition is different. Instead of just checking if n < 5, we have (n := n + 1) < 5. Here’s how it works:

  1. (n := n + 1) is an assignment expression. It both increments n by 1 and assigns the result back to n. This means that n is updated within the expression itself.
  2. The result of the assignment expression is the value of n after the assignment, which is the updated value.
  3. The condition (n := n + 1) < 5 checks if the updated value of n is less than 5. If it is, the loop continues, and the updated n value is printed.

Python3




# With the walrus operator
n = 0
while (n := n + 1) < 5:
    print(n)


Output :

1 2 3 4 

Positional-Only Parameters

Positional-Only Parameters are a feature introduced in Python 3.8 that allows you to specify which parameters in a function’s definition must be provided as positional arguments and cannot be passed as keyword arguments. This is done by using a forward slash (/) in the function definition.

Here, the forward slash (/) in the function definition indicates that the name parameter is positional-only. This means that you must provide a value for name as a positional argument when calling the function. The message parameter, on the other hand, can be provided either as a positional argument or as a keyword argument.

Python3




def greet(name, /, message):
    return f"Hello, {name}! {message}"
 
# Call the function using positional arguments
result = greet("Geeks", "How are you?")
print(result)


Output :

Hello, Geeks! How are you?

f-strings Improvements

In Python 3.8, f-strings were enhanced with two notable features: the = specifier for debugging and the ! specifier for user-defined format specifiers. These improvements make f-strings more powerful for debugging and customizing output.

Debugging with the = specifier:

The = specifier is a feature in f-strings that allows you to embed variable names and their values directly into a formatted string for debugging purposes. It is particularly useful when you want to inspect the values of variables without explicitly printing them. Here’s how it works:

In this code, f'{name=}, {age=}' is an f-string that includes the = specifier. When you use name= and age= inside the f-string, Python automatically substitutes the variables’ names and their values into the resulting string.

Python3




name = "Geeks"
age = 20
 
# Debugging using the = specifier
print(f'{name=}, {age=}')


Output :

name='Geeks', age=20


Custom formatting with the ! specifier:

The ! specifier is another feature of f-strings that allows you to apply custom formatting to values. You can specify a format specifier after the ! to control how a value is displayed. In your example, you’re formatting a floating-point number:

In this code, f'pi={pi:.2f}' uses the :.2f format specifier after the !. Here’s what it means:

  • pi is the variable being formatted.
  • : indicates the start of the format specifier.
  • .2 specifies that you want two decimal places.
  • f specifies that you’re formatting the value as a floating-point number.

Python3




pi = 3.141592653589793
 
# Corrected format for floating-point number
print(f'pi={pi:.2f}')


Output :

pi=3.14

Assignment Expressions in Comprehensions

Assignment expressions, introduced in Python 3.8, allow you to use the := operator within list comprehensions and generator expressions. This feature simplifies complex expressions in comprehensions and can make your code more concise and easier to understand.

Without assignment expressions:

In this code, a list comprehension is used to create a list of squared numbers. It loops through the numbers from 0 to 9 and selects only the even numbers (where x % 2 == 0). Then, it calculates the square of each selected number (x ** 2) and creates a list of these squared values. The result is [0, 4, 16, 36, 64], which are the squares of even numbers from 0 to 8.

Python3




# Without assignment expressions
squared_numbers = [x ** 2 for x in range(10) if x % 2 == 0]
print(squared_numbers)


Output :

[0, 4, 16, 36, 64]


With assignment expressions:

In this code, assignment expressions are used within the list comprehension. Here’s how it works:

  • for x in range(10) is the iteration part, where x takes on values from 0 to 9.
  • (y := x ** 2) is an assignment expression that assigns the square of x to the variable y. This assignment happens within the comprehension.
  • (y := x ** 2) % 2 == 0 checks whether the squared value (y) is even.
  • y is used in the resulting list.

Python3




# With assignment expressions
squared_numbers = [y for x in range(10) if (y := x ** 2) % 2 == 0]
print(squared_numbers)


Output :

[0, 4, 16, 36, 64]

Conclusion :

Python 3.8.0 made some important improvements to the Python language and the things that come with it. They introduced new features like the walrus operator, made it clearer how certain functions should be used, and improved the way you can work with text in Python. These changes help make Python code shorter and easier to understand. They also added some alerts about possible mistakes in your code, which can help you write code that will work well in future versions of Python. These improvements, along with many others that we haven’t talked about here, show that Python is a flexible and always changing programming language.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads