These days I have a newfound pastime — reading Python documentation just for fun! When you read something at leisure, you tend to notice interesting tidbits that you would have missed otherwise. So, here is the list of tidbits that made me go “Oh! You can do that in Python?”
1. Function Attributes
Similar to how you can set attributes of classes and objects, you can also set attributes to functions.
We have set the attributes ‘optional_return’ in line 10 and ‘is_awesome’ in line 11. We have accessed those attributes outside of the function later in lines 19 & 20. The output of the code will be:
Final answer is 2197
Show calculations --> 13
Is my function awesome? --> Yes, my function is awesome.
This comes in handy when you want the option to retrieve some intermediate variable, but not explicitly return it with the return statement every time the function is called. Also note that attributes can be set from inside the function definition or from outside the function definition.
2. For-else loop
In Python, you can add an else clause to a for loop. The else clause will be triggered only if no break statement was encountered within the body of the loop during execution.
All elements at least 3 letters long
Notice that the else is indented at the level of for and not at the level of if. Here, no element has length shorter than 3. So, the break statement will never be encountered. Hence, the else clause will get triggered (after the for loop is executed) and print the output shown above.
One could argue that this could be achieved using a separate variable that keeps track of whether the break statement was encountered. And perhaps it would also be less confusing for the next person reading the code. Still, it’s good to know, I guess.
3. Separators for ‘int’
It’s hard to distinguish visually between integers like 10000000 and 100000000 (are they even different numbers?). We can’t use commas here in Python like we use them in English because Python will interpret it as a tuple of multiple integers.
Python has a very convenient way of dealing with this: we can use underscore as a separator to improve readability. Thus, 1_000_000 will be interpreted as a single int.
True
4. eval() and exec()
Python has the ability to dynamically read a string and treat it like a piece of Python code. This is achieved using eval() and exec() functions (‘eval’ is for evaluating expressions and ‘exec’ is for executing statements).
b = 5
c is 9
In line 3, eval() function will read the input string as a Python expression, evaluate it, and assign the result to the variable ‘b’. In line 6, exec() function will read the input string as a Python statement and execute it.
You can even pass dynamically created strings to these functions. You could, for example, create 1000 variables with names x_0, x_1, …, x_999 without having to write each of those variable declarations manually in your code. This might look like it’s a completely pointless feature, but it’s not.
In the broader context of programming in general, not just Python, the use of eval/exec is incredibly powerful because it allows you to write dynamic code that uses information available at runtime to solve problems which cannot even be expressed at compile time. […] exec is literally a Python interpreter embedded inside Python, so if you have a particularly hard problem to solve, one of the ways you can solve it is to write a program to *write a program to solve it*, then use exec to run that second program.
You can read more of this beautiful explanation by Steven D’Aprano.
5. Ellipsis
Ellipsis or ‘…’ is a built-in constant of Python similar to built-in constants like None, True, False, etc. It can be used in different ways including, but not limited to:
5.1 Placeholder for unwritten code
Similar to pass, Ellipsis can be used as a placeholder when the code is not fully written, but requires some placeholder for syntactic accuracy
5.2 Alternative to ‘None’
None is the usual choice when one wants to denote an empty input or return. But sometimes None can be one of the expected inputs or returns of a function. In that case Ellipsis can serve as the placeholder.
The function nth_odd() calculates the nth odd number, given n. The function original_num() calculates the original number n, given the nth odd number. Here, None is one of the expected inputs to the function original_num() so we can’t use it as the default placeholder for the argument m. The output of the code will be:
This function needs some input
Non integer input provided to nth_odd() function
9 is 5th odd number
16 is not an odd number
5.3 Array slicing in NumPy
NumPy uses Ellipsis to slice an array. The following code shows two equivalent ways of slicing a NumPy array:
[ 0 2 4 6 8 10 12 14]
[ 0 2 4 6 8 10 12 14]
Thus, ‘…’ signifies that there are as many ‘:’s as needed.
Boolean value of Ellipsis
In contrast to None (whose Boolean values is False), the Boolean value of Ellipsis is taken to be True.
TL;DR
Is my function awesome? --> Yes, my function is awesome.
This comes in handy when you want the option to retrieve some intermediate variable, but not explicitly return it with the return statement every time the function is called. Also note that attributes can be set from inside the function definition or from outside the function definition.
2. For-else loop
In Python, you can add an else clause to a for loop. The else clause will be triggered only if no break statement was encountered within the body of the loop during execution.
All elements at least 3 letters long
Notice that the else is indented at the level of for and not at the level of if. Here, no element has length shorter than 3. So, the break statement will never be encountered. Hence, the else clause will get triggered (after the for loop is executed) and print the output shown above.
One could argue that this could be achieved using a separate variable that keeps track of whether the break statement was encountered. And perhaps it would also be less confusing for the next person reading the code. Still, it’s good to know, I guess.
3. Separators for ‘int’
It’s hard to distinguish visually between integers like 10000000 and 100000000 (are they even different numbers?). We can’t use commas here in Python like we use them in English because Python will interpret it as a tuple of multiple integers.
Python has a very convenient way of dealing with this: we can use underscore as a separator to improve readability. Thus, 1_000_000 will be interpreted as a single int.
True
4. eval() and exec()
Python has the ability to dynamically read a string and treat it like a piece of Python code. This is achieved using eval() and exec() functions (‘eval’ is for evaluating expressions and ‘exec’ is for executing statements).
b = 5
c is 9
In line 3, eval() function will read the input string as a Python expression, evaluate it, and assign the result to the variable ‘b’. In line 6, exec() function will read the input string as a Python statement and execute it.
You can even pass dynamically created strings to these functions. You could, for example, create 1000 variables with names x_0, x_1, …, x_999 without having to write each of those variable declarations manually in your code. This might look like it’s a completely pointless feature, but it’s not.
In the broader context of programming in general, not just Python, the use of eval/exec is incredibly powerful because it allows you to write dynamic code that uses information available at runtime to solve problems which cannot even be expressed at compile time. […] exec is literally a Python interpreter embedded inside Python, so if you have a particularly hard problem to solve, one of the ways you can solve it is to write a program to *write a program to solve it*, then use exec to run that second program.
You can read more of this beautiful explanation by Steven D’Aprano.
5. Ellipsis
Ellipsis or ‘…’ is a built-in constant of Python similar to built-in constants like None, True, False, etc. It can be used in different ways including, but not limited to:
5.1 Placeholder for unwritten code
Similar to pass, Ellipsis can be used as a placeholder when the code is not fully written, but requires some placeholder for syntactic accuracy
5.2 Alternative to ‘None’
None is the usual choice when one wants to denote an empty input or return. But sometimes None can be one of the expected inputs or returns of a function. In that case Ellipsis can serve as the placeholder.
In summary, the interesting features I found thus far are:
Function Attributes: Assign attributes to functions just like objects
For-else loop: To keep track of whether the loop was executed without break statement
Separators for int: 32_534_478 is an int
eval() and exec(): read strings as Python code and run it
Ellipsis: A versatile built-in constant.
Parting Words
Python is not just a useful language, but also a really interesting one. We are all busy in our lives, but it doesn’t hurt to get to know the language better for its own sake. I would love to know about some Easter-eggs that you might have found.
https://classics.honestjohn.co.uk/user-article/148586/full-series-grey-s
https://classics.honestjohn.co.uk/user-article/148671/free-series-grey-s
https://classics.honestjohn.co.uk/user-article/148692/free-watch-grey-s
https://classics.honestjohn.co.uk/user-article/148718/hd-watch-grey-s
https://classics.honestjohn.co.uk/user-article/148730/full-series-legacies
https://classics.honestjohn.co.uk/user-article/148752/free-watch-legacies
https://classics.honestjohn.co.uk/user-article/148753/hd-watch-legacies--
https://classics.honestjohn.co.uk/user-article/148754/free-series-legacies
https://classics.honestjohn.co.uk/user-article/148772/free-watch-station
https://classics.honestjohn.co.uk/user-article/148773/hd-watch-station-19
https://classics.honestjohn.co.uk/user-article/148774/free-series-station
https://classics.honestjohn.co.uk/user-article/148775/full-series-station
https://classics.honestjohn.co.uk/user-article/148780/full-series-law-
https://classics.honestjohn.co.uk/user-article/148781/free-series-law-
https://classics.honestjohn.co.uk/user-article/148782/hd-watch-law-
https://classics.honestjohn.co.uk/user-article/148783/free-watch-law-
https://classics.honestjohn.co.uk/user-article/148809/free-watch-young
https://classics.honestjohn.co.uk/user-article/148810/hd-watch-young
https://classics.honestjohn.co.uk/user-article/148811/free-series-young
https://classics.honestjohn.co.uk/user-article/148812/full-series-young
https://classics.honestjohn.co.uk/user-article/148817/full-series-manifest
https://classics.honestjohn.co.uk/user-article/148818/free-series-manifest
https://classics.honestjohn.co.uk/user-article/148819/hd-watch-manifest--
https://classics.honestjohn.co.uk/user-article/148820/free-watch-manifest
https://classics.honestjohn.co.uk/user-article/148829/free-watch-walker--
https://classics.honestjohn.co.uk/user-article/148830/hd-watch-walker--
https://classics.honestjohn.co.uk/user-article/148831/free-series-walker--
https://classics.honestjohn.co.uk/user-article/148832/full-series-walker--
https://steemkr.com/people/@jurigsawah/the-white-people
https://cox.tribe.so/post/jurigsawah-the-white-people-606eb325c2acff3381ee20f8
https://myanimelist.net/blog.php?eid=848847
https://paiza.io/projects/5Vq6kpar_foEHJLiMx9kZA
https://onlinegdb.com/B19Ym4hH_
https://jsfiddle.net/andoloki/fptke0u6/
http://divasunlimited.ning.com/profiles/blogs/the-white-people
https://www.peeranswer.com/question/606eb1506b091ada0fb6b507
https://ideone.com/zl2UJ8
https://dumpz.org/bhZTnNz3bStt
http://paste.jp/3df0f873/
https://rentry.co/6fim4
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day1-08-04-2021
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day2-08-04-2021
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day3-08-04-2021
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day4-08-04-2021
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day5-08-04-2021
https://www.guest-articles.com/art-culture/yes-you-can-do-that-during-the-work-day6-08-04-2021
https://dreampirates.us/business/yes-you-can-do-that-during-the-work-day7-08-04-2021
- The Worldwide Baccalaureate Major A long time Programme (PYP) for prekindergarten like a result of fifth quality, Middle
- Italy has made great strides in recent years having joined the six nations. And this is reflected by their continued showingsb7
- An extra bit of fiction we weave into our childrens lives contacting it school is unquestionably the bodily environment by means of which they go the a
- Some babies use a very challenge in processing languages like studying, composing or spoken issues or sometimes it really is in fact problem in being