Hidden Things in Python3

Hidden Things in Python3

In this blog, I have covered the unnoticed things in python3, which is not covered in any of your courses!

ยท

3 min read

Hey! Good looking eyes, thanks for start reading my blog ๐Ÿ’•

Annotations in Python

Have you ever seen this -> symbol anywhere in the C programming language? Yes, right? Mostly, we have used it while using structures in C language but have you ever seen an arrow operator in python3?

Sounds weird, right??
Okay! Life is boring without an example. So, let's explore annotations with a small snippet of code.

def function()->"hello world":
  pass
print(function.__annotations__)
Output
{'return': 'hello world'}

In the above code, we defined a function with an arrow operator and a string. Let us see what kind of magic is happening over there.

The .__annotations__ returns the data in a dictionary format, which you define using the function.

Let's see another example to understand the above concept in a better way.

def function(n : "int")->"hello world":
  pass
print(function.__annotations__)
Output
{'n': 'int', 'return': 'hello world'}

I hope you got the hang of the concept now.

Why we need annotations in our function?

It's easy for us to understand our own code but if someone else wants to understand the code it will take some time to understand the whole program. So, you can let other programmers know what your function does:

  • What are the arguments in the function

  • What does the function return.

Passing Arguments

You might have passed arguments to a function before; but have you ever passed arguments like this? ๐Ÿง

def function(a, b, c):
  print(a, b, c)
_list = [1, 2, 3]
function(*_list)
Output
1 2 3

Are you wondering about how it works?

  • This can be done by having a * before the list variable name while passing arguments.

  • You can also print a dictionary in a similar way by using the double asterisks (**)

def function(a, b, c):
  print(a, b, c)
_dict = {'a':1, 'b':2, 'c':3}
function(**_dict)
Output
1 2 3

Semicolons in Python3

Did you know that python3 supports semicolons in the program?

Image not found

Yeah, it supports semicolons. Let's explore that using the following program.
I know that many programmers love to use CTRL + C and CTRL + V. So copy the below code and try it.

def function(n1, n2):
  print(n1, n2);
n1 = 1; n2 = 2;
function(n1, n2);
Output
1 2

I hope you got the output. ๐Ÿ˜‰
That said, using a semicolon is not a good practice in python because it is not why it is built for.

For...Else

Most of the Pythoneers are aware of this. So, if you don't know about this go through the following description.

We have used for loop and if...else but WTH is for...else?
We know that the for loop executes only when the entry condition is true but what about when the condition is false? That's when we will use for...else.

Let's try to understand for...else using the following program.

for i in range(10):
  print(i, end=" ")
else:
  print("\nloop is terminated")
Output
0 1 2 3 4 5 6 7 8 9 
loop is terminated

Iterators

You may have used for loop to traverse the list, but there is another way to get the next element in the list. We do that by using __iter__() and __next__() functions.

list = [1, 2, 3, 4]
list_iter = list.__iter__()
print(list_iter.__next__())
print(list_iter.__next__())
print(list_iter.__next__())
print(list_iter.__next__())

If you try to execute .__next__() one more time, it will throw the StopIteration exception. To avoid that we can implement the program using the try and except block.

list = [1, 2, 3, 4]
list_iter = list.__iter__()

while True:
  try:
    list_iter.__next__()
  except StopIteration:
    break

Now the program will iterate through each element in the list.

Thanks for reading ๐Ÿ™‚

If you found this article helpful and learned something new, let me know in the comments

ย