By Peter Nistrup

Zhou Radish

Radish stew

Today, Python is more popular than ever, and people are experimenting every day with just how powerful and easy Python is.

I’ve been programming Python for a few years now, but I’ve been full-time for the last six months. Here are some things I wish I knew when I first started using Python:

  • String manipulation
  • The list of deduction
  • Lambda and Map functions
  • Use if elif and else conditions on one line
  • Zip () function

String manipulation

Python is very good at manipulating strings with mathematical operators like + and *

>>> my_string = "Hi Medium.. !"
>>> print(my_string * 2) Hi Medium.. ! Hi Medium.. !>>> print(my_string + " I love Python" * 2) Hi Medium.. ! I love Python I love PythonCopy the code

We can also invert strings very easily by using [::-1], and not just strings.

>>> print(my_string[::- 1])
!..muideM iH
>>> my_list = [1.2.3.4.5]
>>> print(my_list[::- 1[])5.4.3.2.1]
Copy the code

For lists with multiple strings, we could even make a Yoda-translator!

>>> word_list = ["awesome"."is"."this"]
>>> print(' '.join(word_list[::- 1]) + '! ')
this is awesome!
Copy the code

In the above code, we use the.join() method to concatenate the inverted list with Spaces and add an exclamation point.

The list of deduction

Oh, my God! Once I knew this, my whole world changed (it may not have happened yet, but it was close). This is a powerful, intuitive and readable way to quickly manipulate lists in China.

Let’s say we have a function that looks like this, and we take the square of something and we add 5

>>> def stupid_func(x):
>>>     return x**2 + 5
Copy the code

Now if we were to apply this function to all the odd numbers in a list, if you didn’t know the list derivation, you might write it like this

>>> my_list = [1.2.3.4.5]
>>> new_list = []
>>> for x in my_list:
>>>     if x % 2! =0:
>>>         new_list.append(stupid_func(x))
>>> print(new_list)
[6.14.30]
Copy the code

But there’s an easier way!

>>> my_list = [1.2.3.4.5]
>>> print([stupid_func(x) for x in my_list if x % 2! =0[])6.14.30]
Copy the code

List derivation applies to the [expression for item in list] condition, and if you want to apply some Boolean condition, such as the condition above to get an odd number: [Expression for item in list if conditional], then it is the same as the following

>>> for item in list:
>>>     if conditional:
>>>         expression
Copy the code

Cool, but we could do better, because we don’t need stupid_func at all.

>>> print([x ** 2 + 5 for x in my_list if x % 2! =0[])6.14.30]
Copy the code

Lambda and Map

Lambda

Lambda is a little strange, but like everything ELSE I’ve covered, it’s powerful and intuitive if you just use it.

Lambda is just a little anonymous function. Why anonymity? That’s because Lambda is often used to perform small, simple operations that don’t require def my_function() to define formal functions

Let’s use the example above, square a number and add 5. In the code above we defined a function def stupid_func(x), now let’s use Lambda to recreate it

>>> stupid_func = (lambda x : x ** 2 + 5)
>>> print([stupid_func(1), stupid_func(3), stupid_func(5[])6.14.30]
Copy the code

So why use this weird syntax? Now, this is useful because we don’t have to define the actual functionality, but we can do some simple things. Continuing with the numerical list example, if we want to sort the following list, one way is to use sorted()

>>> my_list = [2.1.0.- 1.2 -]
>>> print(sorted(my_list))
[2 -.- 1.0.1.2]
Copy the code

That’s fine, but Lambda is handy if we want to sort by the size of the square of an element. You can use Lambda to define the sorted() function’s key for sorting

>>> print(sorted(my_list, key = lambda x : x ** 2))0.- 1.1.2 -.2]
Copy the code

Map

A Map is a function that applies to each element of a sequence, such as a list. Suppose we had to list the product of the positional elements of two lists. What would we do? Lambda and Map

>>> print(list(map(lambda x, y : x * y, [1.2.3], [4.5.6[])))4.10.18]
Copy the code

The combination of Lambda and Map is quite elegant compared to the code below

>>> x, y = [1.2.3], [4.5.6]
>>> z = []
>>> for i in range(len(x)):
>>>     z.append(x[i] * y[i])
>>> print(z)
[4.10.18]
Copy the code

Use if elif and else conditions on one line

Sometimes, you might write code like this

>>> x = int(input())
>>> if x >= 10:
>>>     print("Horse")
>>> elif 1 < x < 10:
>>>     print("Duck")
>>> else:
>>>     print("Baguette")
Copy the code

When running this command, you will be prompted to enter something from the input() function, assuming we type 5, we will get Duck. But we could also write it like this

print("Horse" if x >= 10 else "Duck" if 1 < x < 10 else "Baguette")
Copy the code

It’s so easy! Go through your old code and you’ll see a lot of places where you can replace this simple if else judgment with this one-line judgment.

zip()

Remember from the Map function where we worked on two lists in parallel, using zip() would have been easier

If we have two lists, one containing the first name and one containing the last name, how do we combine them well, using zip()!

>>> first_names = ["Peter"."Christian"."Klaus"]
>>> last_names = ["Jensen"."Smith"."Nistrup"]
>>> print([' '.join(x) for x in zip(first_names, last_names)])
['Peter Jensen'.'Christian Smith'.'Klaus Nistrup']
Copy the code

Wow, there is something wrong, my name is not Peter Jensen, so I can adjust it as follows

>>> print([' '.join(x) for x in zip(first_names, last_names[::- 1[]])'Peter Nistrup'.'Christian Smith'.'Klaus Jensen']

Copy the code

conclusion

I’ve just put together a simple list to give you an idea of the many things Python can do elegantly. If you have any different ideas, please leave a comment!

Source: towardsdatascience.com/python-tric…