1
ProgrammingSoftware

Simple Python Tricks That You Might Find Useful

2 Mins read

Python is one of the most popular programming languages out there. What are the simple python tricks that you might find useful. Python can be sometimes hard to get your head around. You might find that if you’re an experienced programmer and you just want to brush up on Python basics, these are the techniques you’ll want to keep in your back pocket.

Every single little thing that comes in handy for a developer is going to come in handy for something else as well. They’re all simple tricks that will help you develop faster and with less hassle.

Using Collectors

A Collector is a method that comes along with all the data types. These collect all their input arguments into an array, and then call a given function and pass it the array. This can be useful in programming to reduce repetition in your code, or to keep track of state. But they’re especially handy if you’re trying to write something simple, but which needs to output a bunch of stuff.

A Counter is particularly important when counting elements in Collections such as Arrays, Lists and Dictionaries.

Example:

In this example, we count the number of syllables in words separated by a “-“, for example “ha-llo” – should return 2

def count(str):
    items = Counter(str)
    return items["-"]+1

Looping through Hash values

To quickly loop through a list of values, a handy simple solution is to use the ‘for-in’ statement. With this, you can essentially go through your entire list and grab the value you want, and it will keep going until the end of your list.

Here’s an example:
Let’s say you have a list of online status’. How would you print the number of online status from a hash as shown in the given example?

statuses = {
"Alice": "online",
"Bob": "offline",
"Eve": "online",
}

Possible solution:

statuses = {
"Alice": "online",
"Bob": "offline",
"Eve": "online",
}

def get_online(statuses):
    count = 0
    for k,v in statuses.items():
        print(v)
        if v == 'online':
            count +=1
    return count

print(get_online(statuses))

Using ternary operator in python

A ternary operator essentially is a shorthand way of doing if-then-else statements. So if you have some code that you want to execute, but only under certain conditions, this allows you to do that without having to actually write the whole statement.

Example:

Follows this pattern: [on_true] if [expression] else [on_false]

The below example will return True if the str equals `abc` but otherwise return False

value = True if (str == 'abc') else False

String Interpolation

In python, it can sometimes be hard to output a bunch of different things in the same string. You need to use the ‘str. join’ method, and then output each item one at a time. This can get tedious very quickly and is easily one of the most annoying parts of Python for new developers. However, there exist two methods that provide simple shortcuts for this.

Example:

name = "John Mayer"
print(%f"my name is {name}")

Using Maps

One of the best things about Python is that it comes with a dictionary, and you can use a dictionary to output each item in a list like so:

The best part of using maps is that it’s really simple. You just go on and create your map, and then access it like you would any other variable.

Example:

An example would be passing a list with int elements and giving an output of string elements by using a map

def convert(a_str):
   return list(map(str, a_str))

Conclusion

Python is a general-purpose, high-level programming language that is used in a variety of fields. It has many features that make it easy to use while also maintaining flexibility and power. Python has taken the programming world by storm, and its popularity will only continue to grow in the future. By learning python now, you’ll be able to help shape tomorrow’s development!

You might be interested in 16 great advantages of python programming.

Don’t miss amazing tips!

1
Related posts
How ToProgrammingSoftwareTechnology

How to configure Apache Airflow on MacOS locally

4 Mins read
Table of contents0.1 Creating the project folder and the virtual environment0.2 Installing0.3 Running airflow0.4 Starting the webflow server0.5 Starting the scheduler0.6 Running…
Code ChallengesHow ToProgrammingSoftware

How To Implement Merge Sort and Quick Sort Algorithms In Python 3

3 Mins read
Table of contents0.1 Merge Sort0.2 Quick Sort0.3 Conclusion1 Don’t miss amazing tips! Let’s have a look at how to how to implement…
InterestingSoftwareTechnology

Getting started with TensorFlow, amazing basics

4 Mins read
Table of contents0.1 What is a Tensor?0.2 What is TensorFlow?0.3 How many models can I train with TensorFlow?0.4 How do I use…

Leave a Reply

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

16 − 7 =

×
General testingQa

Why Do We Need QA Testing