Top 100 Blogs
Guest bloggers
Hostinger

Related Posts

6 Practical Tricks & Tips Every Python Developer Should Have

HomeTechnology6 Practical Tricks & Tips Every Python Developer Should Have

As it is simple, scalable, easy to learn, and has plenty of useful libraries for various tasks, Python has become a popular programming language. Python programmers are high in demand, from web creation to data science and cybersecurity.

Fortunately, Python has a wide range of built-in characteristics that can help you write elegant, concise, and expandable code, the kind of features you expect from a professional programmer.

Here are some of the tricks that every developer of Python should know.

1. Comprehension list

Understanding the list is one of Python’s core features that help you write a more concise and elegant code. Let’s assume that you want to create an array that includes numbers between 1 and 100. The convenient way to do this would be to use the feature range):

Data = list(range(1, 101)))

But what if you were to do something more complex, like making a list of 1 to 100 squares? The classic approach, in that case, would be to use a loop for:

=] [Numbers

In the range(1, 101) for I

Numerals.append(i**2)

This is the standard approach supported by most programming languages. But luckily, in Python, comprehension of the list makes it a lot simpler. When written in list comprehension mode, here is what the same code will look like:

Numbers = [i**2 in the range(1, 101)] for I

You can pack multiple instructions and expressions in brackets that describe a list by using list comprehension. They’re a lot shorter and more elegant than the loop for that. And though still keeping the code safe, there is a lot more you can do. For example, say that you have an is prime) (a function that checks an input number and returns True if it’s a prime number. By adding is prime) (a condition to the understanding, the following code snippet produces a list of squares of primes from 1 to 100.

2. With zipping

The zip) (function is another Python feature that comes in handy every once in a while. With zip, two or more lists are merged into a single variable. State that a list of customer names, ages, and favorite ice cream flavor has been compiled.

Customers = [‘smith’, ‘saheli’, ‘deep’, ‘Sally’]

[26, 31, 39, 22] Era =

Flavors = [‘cherry’, ‘chocolate’, ‘lemon’, ‘strawberry’]

You can merge all three lists into a single list by using zip) (where each entry contains a tuple with one customer’s name, age, and preferred flavor.

Combined = zip(clients, ages, tastes)

Ice-cream clients = list(combined)

This is what the ice cream list of your customers looks like after zipping the data:

[(‘John’, 26, ‘cherry’), 26, ‘cherry’),

(‘Sahel’, 31, ‘chocolate’),

(“deep”, 39, “strawberry”),

(‘Sally’, 22, ‘lemon’)]]

And here’s how you can use the zip list in a loop:

Customers’ ice cream for customers:

Print} (“{is} {likes and years old }” {.format(*cust))

The performance looks this way:

He’s 26 years old and likes raspberry, Smith.

Saheli is 31 years old and loves chocolate.

It’s 39 years old and deep and likes strawberry.

Sally is 22 years old, and she loves lemon,

3. Objects Counting

You also want to know how many times a particular attribute in a chart happens. For example, say we’ve gathered a list from an online survey of 1-10 ratings. We’ll generate a list of 1,000 random numbers from 1 to 10 using the randint) (function to simulate this.

Randint from Random Import

Scores = [randint(1, 10) in range(1,1001)] for

Now, we want to know how many are included in the list for each ranking. One way to do that is to use the list’s built-in counting feature. The value count) (takes a value and returns the number of times the value in the list occurs.

However, this only works if you understand in advance what the range of values in your list is. You may use a collection that generates a list of unique things found in another list if you don’t know the possible values. For example, you can use the following code if you have a list of names and know how many times each name has happened.

Set(names): for name in set(names):

Occurrences of print}:}” (“{{.format(name, names.count(name)))).

Alternatively, you can use the Counter class that specializes in lists to count values.

Import Counter from Collections

Counting ratings = Counter (ratings)

Count for ranking in ratings:

Print}:} (“{{occurrences”.format(rating, counting[rating])))))

Counteroffers some added features, such as the most-common) (function, which provides you with the most common values in a chart. The following code will, for example, print out the three most common values:

In ratings count.mostcommon(3): for rating:

Occurrences of print}:}” (“{{.format(*rating))).

Want to go to Rijksoverheid to work? They’re searching for developers for Python.

4. Enumeration

Often, when you iterate through a list, you want to keep track of the item count. Suppose you have a list of customer names, and along with their index number, you want to list them.

5. Expansion of parameters

Say you have a feature that processes information from students:

Student-info(first-name, last-name, fav-topic, score) def process:

Print(first name, last name, score, fav-topic)

In certain instances, a list or dictionary object that has been populated from a database or text file contains the values you want to transfer to the feature. It would be a little clunky in such cases to call the function:

Fortunately, Python’s “parameter expansion” feature allows you to move an entire list to a function directly. Adding a * at the starting of the list’s name, you extend it to its values before sending it to the function.

The expansion of parameters with lists works as long as the number of parameters, and their order is identical to the target function parameters. It will produce an error in the event of a mismatch.

For dictionaries, you can also use expansion criteria, in which case the order of values does not matter. It would help if you had keys that correspond to the parameters of your function. The expansion parameter for dictionaries includes an operator * * before the object.

One of the advantages of using dictionary expansion is that omitting them in your dictionary won’t cause an error if your feature has default parameters.

6. Typing annotations

Python is a dynamically typed language, meaning if you try to combine variables with various types of data, it typically finds a way to fix the difference or, if it can’t, raises an exception. But this versatility may also result in unpredictable behaviour.

pearls of wisdom
Jignesh Vaghasiya
Jignesh Vaghasiya
Jignesh Vaghasiya is the CEO at Universal Stream Solution. Universal Stream Solution is a web development company in Atlanta (http://www.universalstreamsolution.com/). That helps start-ups to enterprise companies in mobile & web technology.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Posts

Sharing is Caring!

Help spread the word. You are awesome for doing it!