5 More Awesome Python Hidden Features — Part 2 | by David Farrugia | Mar, 2023


PYTHON | PROGRAMMING | FEATURES

A look at some powerful features to unlock Python’s full potential

Photo by Joshua Reddekopp on Unsplash

Python is a strong and robust language — and one of the ways that renders Python to be on a class of its own is its versatility and dynamics. Python is famous for its cool ‘one-liners’. Undoubtedly, we all feel a rush of excitement or intrigue whenever we see a beautifully executed Pythonic line of code.

In our previous post, we discussed 5 awesome hidden features in the Python programming language. You can find that article here:

I received a lot of positive feedback on this article, and some of you even reached out to request some more of these powerful Python tricks.

So here you go, another 5 cool hidden features that you can use to become even more dangerous when programming in Python!

List stepping is used to select different parts of a list, or even select items at different intervals. The syntax is as follows:

list[start:end:step]
  • start: the index of the first element to include
  • end: the index of the first element to exclude
  • step: the number of elements that we skip between each iteration

Suppose we have a list with the numbers 0–9 and we want to return only the even numbers. We can do something as follows:

my_list: list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers: list = my_list[::2] # [0, 2, 4, 6, 8]

Here, we do not specify the start and end index; therefore, Python takes the start to be the first element and the end to be the last element (i.e., the entire list). We then specify that we want a step of 2. So, python will start with the first element and return it (i.e., 0). Python will then move 2 steps (moves to 1, and then moves to 2) and return the result (i.e., 2). This process is repeated until we go over the entire list.

Another powerful trick with list stepping is the ability to reverse a list using negative indexing.

my_list: list = [1, 2, 3, 4, 5]
reversed_list: list = my_list[::-1] # [5, 4, 3, 2, 1]

It is often the case in programming that we need to evaluate multiple comparisons as part of our logic flow.

Suppose we have a variable x and we want to ensure that x is greater than 1 but smaller than 10. We typically do something like:

x: int = 5

condition1: bool = x > 1 # check if x is greater than 1
condition2: bool = x < 10 # check if x is smaller than 10

print(condition1 and condition2) # True

In Python we can chain these comparisons as follows:

x: int = 5

print(1 < x < 10) # True
print(10 < x < 20) # False

We can also do something like:

x: int = 5

print(5 == x > 4) # True
print(x < 10 < x*10 < 100) # True

If you studied mathematics, then you are familiar with the concept of complex numbers. For those of you who didn’t and are interested, you can read the following article as a good introduction to this topic.

An interesting feature in Python that most people don’t know about is that it has full support for complex numbers. In mathematics we typically use the symbol i to represent a complex number. In Python, we use either jor call the complex() function.

# Creating complex numbers
z1 = 2 + 3j
z2 = complex(4, -2) # (4 -2j)

# Accessing real and imaginary parts
print(z1.real) # 2.0
print(z1.imag) # 3.0

# Arithmetic with complex numbers
z3 = z1 + z2 # (6+1j)
z4 = z1 * z2 # (14+8j)
z5 = z1 / z2 # (0.1+0.8j)

# Conjugate of a complex number
z6 = z1.conjugate() # (2-3j)

You might have seen that most programmers reserve the _ as a placeholder for some variable which is unused or not needed during execution.

However, what most people don’t know is that by default, Python assigns the result of your last execution to the variable _.

x: int = 5
y: int = 99

x + y # 104
print(_) # 104

Suppose we have some arbitrary function:

def my_sum(a, b, c):
return a + b + c

We have a list of 3 numbers that we want to pass to our function. We typically write:

my_list = [1, 2, 3]

result = my_sum(my_list[0], my_list[1], my_list[2])
print(result) # 6

In Python we can do the following instead:

result = my_sum(*my_list)
print(result) # 6

The * will unpack the entire list and passes every item as a parameter to the function. Subsequently, we can also unpack a dictionary using the **.

# Example of dictionary argument unpacking
def my_func(a, b, c):
print(f"a={a}, b={b}, c={c}")

my_dict = {'a': 1, 'b': 2, 'c': 3}

my_func(**my_dict)

Python has several easter eggs scattered throughout. Try import antigravity 😁


PYTHON | PROGRAMMING | FEATURES

A look at some powerful features to unlock Python’s full potential

Photo by Joshua Reddekopp on Unsplash

Python is a strong and robust language — and one of the ways that renders Python to be on a class of its own is its versatility and dynamics. Python is famous for its cool ‘one-liners’. Undoubtedly, we all feel a rush of excitement or intrigue whenever we see a beautifully executed Pythonic line of code.

In our previous post, we discussed 5 awesome hidden features in the Python programming language. You can find that article here:

I received a lot of positive feedback on this article, and some of you even reached out to request some more of these powerful Python tricks.

So here you go, another 5 cool hidden features that you can use to become even more dangerous when programming in Python!

List stepping is used to select different parts of a list, or even select items at different intervals. The syntax is as follows:

list[start:end:step]
  • start: the index of the first element to include
  • end: the index of the first element to exclude
  • step: the number of elements that we skip between each iteration

Suppose we have a list with the numbers 0–9 and we want to return only the even numbers. We can do something as follows:

my_list: list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
even_numbers: list = my_list[::2] # [0, 2, 4, 6, 8]

Here, we do not specify the start and end index; therefore, Python takes the start to be the first element and the end to be the last element (i.e., the entire list). We then specify that we want a step of 2. So, python will start with the first element and return it (i.e., 0). Python will then move 2 steps (moves to 1, and then moves to 2) and return the result (i.e., 2). This process is repeated until we go over the entire list.

Another powerful trick with list stepping is the ability to reverse a list using negative indexing.

my_list: list = [1, 2, 3, 4, 5]
reversed_list: list = my_list[::-1] # [5, 4, 3, 2, 1]

It is often the case in programming that we need to evaluate multiple comparisons as part of our logic flow.

Suppose we have a variable x and we want to ensure that x is greater than 1 but smaller than 10. We typically do something like:

x: int = 5

condition1: bool = x > 1 # check if x is greater than 1
condition2: bool = x < 10 # check if x is smaller than 10

print(condition1 and condition2) # True

In Python we can chain these comparisons as follows:

x: int = 5

print(1 < x < 10) # True
print(10 < x < 20) # False

We can also do something like:

x: int = 5

print(5 == x > 4) # True
print(x < 10 < x*10 < 100) # True

If you studied mathematics, then you are familiar with the concept of complex numbers. For those of you who didn’t and are interested, you can read the following article as a good introduction to this topic.

An interesting feature in Python that most people don’t know about is that it has full support for complex numbers. In mathematics we typically use the symbol i to represent a complex number. In Python, we use either jor call the complex() function.

# Creating complex numbers
z1 = 2 + 3j
z2 = complex(4, -2) # (4 -2j)

# Accessing real and imaginary parts
print(z1.real) # 2.0
print(z1.imag) # 3.0

# Arithmetic with complex numbers
z3 = z1 + z2 # (6+1j)
z4 = z1 * z2 # (14+8j)
z5 = z1 / z2 # (0.1+0.8j)

# Conjugate of a complex number
z6 = z1.conjugate() # (2-3j)

You might have seen that most programmers reserve the _ as a placeholder for some variable which is unused or not needed during execution.

However, what most people don’t know is that by default, Python assigns the result of your last execution to the variable _.

x: int = 5
y: int = 99

x + y # 104
print(_) # 104

Suppose we have some arbitrary function:

def my_sum(a, b, c):
return a + b + c

We have a list of 3 numbers that we want to pass to our function. We typically write:

my_list = [1, 2, 3]

result = my_sum(my_list[0], my_list[1], my_list[2])
print(result) # 6

In Python we can do the following instead:

result = my_sum(*my_list)
print(result) # 6

The * will unpack the entire list and passes every item as a parameter to the function. Subsequently, we can also unpack a dictionary using the **.

# Example of dictionary argument unpacking
def my_func(a, b, c):
print(f"a={a}, b={b}, c={c}")

my_dict = {'a': 1, 'b': 2, 'c': 3}

my_func(**my_dict)

Python has several easter eggs scattered throughout. Try import antigravity 😁

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
artificial intelligenceAwesomeDavidFarrugiaFeaturesHiddenlatest newsmachine learningMARPartpython
Comments (0)
Add Comment