5

Your Guide To Productive Python Programming

 3 years ago
source link: https://hackernoon.com/your-guide-to-productive-python-programming-fj7635yw
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Your Guide To Productive Python Programming

May 2nd 2021 new story
5
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png

@zenulabidinAli Sherief

Developer at ChainWorks Industries & online entrepreneur

Have you ever felt that you are not coding Python as productive as you want to be? Have you ever found yourself repeating the same statements consecutively but each with slight changes?

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Well then, is guide is for you. The beauty of Python is that it gives us several valuable constructs to avoid repetition and to do more work with less code.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

You may already know such gems as what I call the "two-variable swap trick" involving the use of tuples or the "multiple value input” trick that gets many variables from

input()
by running the resulting line through
split(" ")
, or even the one-liner fit reversing an array using
[::-1]
, but this article will give you even more shortcuts to use.
0 reactions
heart.png
light.png
money.png
thumbs-down.png

Variable numbers of arguments

You might know that there are some C functions such as

printf()
, which take a variable number of arguments depending on the value of the first one. This is easy to replicate in Python by using list expansion after the initial positional arguments.
0 reactions
heart.png
light.png
money.png
thumbs-down.png
def fun1(*a):
    res=1
    for ele in a:
        res*=ele
    return res
ans=fun1(5,6,9,6,9,6,6)
print(ans)   # prints `524880`

(from 10 Python Tricks That Will Wow You)

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Here's another more organic example, a calculator that makes use of positional arguments:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
OP_ADD = 1
OP_SUBTRACT = 2
OP_MULTIPLY = 3
#...
def calculator(op, *var):
    if op == OP_ADD:
        a, b = *var
        return a + b
    elif op == OP_NEG:
        a = *var
        return -a
    #...

Selectively printing text

Have you ever found yourself writing code like this?

0 reactions
heart.png
light.png
money.png
thumbs-down.png
if foo == True:
    print("All is well!")
else:
    print("We may have a problem")

You don't have to do that! This can be done in one line using the condensed form of the

if
statement.
0 reactions
heart.png
light.png
money.png
thumbs-down.png
print("All is well!" if foo == True else "We may have a problem")

Make a table of element occurrences

By making use of the

count()
method as a key, we can convert arrays to dictionaries to find how many times each element occurs.
0 reactions
heart.png
light.png
money.png
thumbs-down.png

Two-way comparisons

In Python, you can chain two less-than or greater-than operators to test whether a number is within a range. No more having to test them separately!

0 reactions
heart.png
light.png
money.png
thumbs-down.png
if 1 < x < 10:
   #...

Enhanced tracing using

icecream

It's happened to most of us. The debugger goes kaput, and it forces us to sprinkle

print()
statements everywhere to trace program execution while finding a bug. Now seeing is everything, and most people can't write good words that help someone identify where control flow is (I know I can’t).
0 reactions
heart.png
light.png
money.png
thumbs-down.png

That's where

icecream
comes in handy.
icecream
is a PyPI module that contains a function you can place anywhere in the program, and when you call it without arguments, it will tell you the file and line number, as well as function it's in as well as the current time.
0 reactions
heart.png
light.png
money.png
thumbs-down.png

When you do call it with an argument, however, it will print the name and value of the variable you passed. It's convenient to avoid mass repetition from writing a bunch of your own

print()
statements.
0 reactions
heart.png
light.png
money.png
thumbs-down.png
from icecream import ic 

def hello(user:bool):
    if user:
        ic()
    else:
        ic()

hello(user=True)

num1 = 30
num2 = 40 

ic(num1)
ic(num2)

(from Stop Using Print to Debug in Python. Use Icecream Instead)

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Antipatterns

Having said all that, make sure you avoid the following pitfalls while coding:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
  • Using list comprehensions on a vast array. This will use unnecessarily large amounts of memory.
  • Using
    range(len(<list>))
    for the iterator in
    for
    loops. This forces you to get the list length again using
    len()
    . It is better to use
    enumerate()
    in such scenarios to get both the value and index. Additionally, avoid using
    for i in ...
    syntax if you need indices in the loop.
  • Using square brackets
    []
    to access dictionary keys if they may not exist. Instead of handling
    KeyError
    exceptions (and it's tempting to omit
    try
    groups when accessing random dictionary keys), use the
    get()
    method of dictionaries, which will return
    None
    if the key does not exist. The resulting
    if
    conditional is much easier to handle in code.

So as you just saw, Python is a very rich language that gives you more straightforward ways of programming the same thing. And where the functionality is not built-in, there is usually a PyPI module that does it.

0 reactions
heart.png
light.png
money.png
thumbs-down.png
5
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
by Ali Sherief @zenulabidin. Developer at ChainWorks Industries & online entrepreneurRead my stories
Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK