PHOTO-2022-08-06-13-39-03

3 Tricks a Python Developer Should Know | by Varun Singh | Jan, 2022 – Medium

Sign in
Hemangkumar Parekh
Jan 6·5 min read
Python developers are often writing code with the existing knowledge they have (including me). We hardly turn up to the Python docs and read something new and try to apply it in our existing code scenario.
Well, I recently came across amazing tricks that will definitely apply and will be helpful to at least know these tricks. Let us start discussing these 3 Python tricks below —

This can also be written by using condition as below —
Above example, we return the values and not True/False. For example, look at the program below and think about what it outputs —
If you thought it should print “Yeah.”, you are right! Notice how 7 **or** 9 was the condition of the if statement and it evaluated to True, which is why the statement under if got executed.
Now, look at the program below and think about what it outputs:
What do you think it outputs? If you think the output should be True, you are wrong! The program above outputs 7:
Let’s go back to something I said:

The wording of this statement is wrong, but the error in it is fairly subtle. If you spotted it before I pointed it out, give yourself a pat in the back, you deserve it. So, what did I say wrong?
in Python means — and and or only evaluate the right operand if the left operand is not enough to determine the result of the operation.
Considering the fact that the boolean operators and and or return the values of the operands and not necessarily a Boolean, means we can do some really cool things with them. Let’s take some examples below —
Above operations will also evaluate for y, because the first operand is False. In case of OR operator, if the first operand evaluates to False, then it will always evaluate the next operand which in our case is y.
Now try to guess the answer for below example —
You guessed it correctly — It evaluate for True since the first operand is True and the expression will stop evaluating further.
That’s what “REPL” stands for, and it is often referred to as “read-eval-print-loop”. The REPL is the program that takes your input code, evaluates it, prints the result, and then repeats.
Usually you will find it as — The Python Interpreter when you go through the Python docs
The REPL is what you get when you open your computer’s command line and type python or python3 depending on the Python version you have installed in your PC. It should look something like below —
You may see few differences, but it is the same REPL.
REPL can help you debug your python code. It can help you get started with your core programming logic and test it on the go before you even add it to you python script. Still not sure about REPL? I will list out few ways for you to realize the power of REPL —
The REPL generally contains a >>> in the beginning of the line, to the left of your cursor. You can type code in front of that prompt and press Enter. When you press Enter, the code is evaluated and you are presented with the result:
The REPL also accepts code that spans multiple lines, like if statements, for loops, function definitions with def, etc.
In order to do those, just start typing your Python code regularly:
When you press Enter after the colon, Python realises the body of the if statement is missing, and thus starts a new line containing a ... on the left. The ... tells you that this is the of what you started above.
In order to tell Python you are done with the multiline code blocks is by pressing Enter on an empty line with the continuation prompt ...:
Another great feature that is often underappreciated is the built-in help system. If you need to take a look at a quick reference for a built-in function, for example, because you forgot what the arguments are, just use help!
It is fairly common to have a list or another iterable that you want to split in the first element and then the . You can do this by using slicing in Python, but the most explicit way is with .
Starred assignment was introduced in Extended Iterable Unpacking and its usage is shown below:
As shown above, starred assignments are done by placing a * to the left of a variable name in a multiple assignment, and by having any iterable on the right of the assignment. All variable names get a single element and the variable name with the "star" (the asterisk *) gets all other elements as a list:
You can have more than two variables on the left, but only one asterisk for starred assignment to evaluate to the right results as shown below —
I am a full-time software engineer with 4+ years of experience in Python, AWS, and many more technologies. I have started writing recently as it helps me to read more. I am looking forward to sharing technical knowledge and my life experiences with people out there.
Register Here for my Programmer Weekly newsletter where I share interesting tutorials, python libraries, was, and tools, etc.
Github | LinkedIn | Twitter | Facebook | Quora | Programmer Weekly
Data Analyst turned Software Developer and Blogger. Enjoys a hot cup of tea and nature’s spell.



Data Analyst turned Software Developer and Blogger. Enjoys a hot cup of tea and nature’s spell.

source

Leave a Reply

Your email address will not be published. Required fields are marked *