Techno Blender
Digitally Yours.
0 27

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

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 = 5condition1: bool = x > 1  # check if x is greater than 1condition2: 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 = 5print(1 < x < 10)   # Trueprint(10 < x < 20)  # False`

We can also do something like:

`x: int = 5print(5 == x > 4)  # Trueprint(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 `j`or call the `complex()` function.

`# Creating complex numbersz1 = 2 + 3jz2 = complex(4, -2)  # (4 -2j)# Accessing real and imaginary partsprint(z1.real)  # 2.0print(z1.imag)  # 3.0# Arithmetic with complex numbersz3 = z1 + z2  # (6+1j)z4 = z1 * z2  # (14+8j)z5 = z1 / z2  # (0.1+0.8j)# Conjugate of a complex numberz6 = 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 = 5y: int = 99x + y  # 104print(_)  # 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, my_list, my_list)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 unpackingdef 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` 😁

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

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 = 5condition1: bool = x > 1  # check if x is greater than 1condition2: 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 = 5print(1 < x < 10)   # Trueprint(10 < x < 20)  # False`

We can also do something like:

`x: int = 5print(5 == x > 4)  # Trueprint(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 `j`or call the `complex()` function.

`# Creating complex numbersz1 = 2 + 3jz2 = complex(4, -2)  # (4 -2j)# Accessing real and imaginary partsprint(z1.real)  # 2.0print(z1.imag)  # 3.0# Arithmetic with complex numbersz3 = z1 + z2  # (6+1j)z4 = z1 * z2  # (14+8j)z5 = z1 / z2  # (0.1+0.8j)# Conjugate of a complex numberz6 = 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 = 5y: int = 99x + y  # 104print(_)  # 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, my_list, my_list)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 unpackingdef 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` 😁
{"ft_lang_label":"__label__en","ft_lang_prob":0.87785214,"math_prob":0.9368346,"size":8759,"snap":"2023-40-2023-50","text_gpt3_token_len":2451,"char_repetition_ratio":0.10599657,"word_repetition_ratio":0.99757576,"special_character_ratio":0.29466835,"punctuation_ratio":0.13641246,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98603725,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-29T16:05:59Z\",\"WARC-Record-ID\":\"<urn:uuid:758d14fa-d195-49f0-987d-107fb1b0790a>\",\"Content-Length\":\"127002\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b27964d3-53e4-4ca1-af17-b30b86cc7cd2>\",\"WARC-Concurrent-To\":\"<urn:uuid:3f865c2d-7be6-4199-ae74-269a2a433ca9>\",\"WARC-IP-Address\":\"104.21.57.133\",\"WARC-Target-URI\":\"https://technoblender.com/5-more-awesome-python-hidden-features-part-2-by-david-farrugia-mar-2023/\",\"WARC-Payload-Digest\":\"sha1:X4E6H6K4OX3NWVETRPKSCCMGT637RG5F\",\"WARC-Block-Digest\":\"sha1:T2ZQ574SXMD2L6PHZ2Z44BVIHUT4YEYM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100112.41_warc_CC-MAIN-20231129141108-20231129171108-00512.warc.gz\"}"}