Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

10 lines of code to give you a taste of Python's charm


Jun 01, 2021 Article blog



After learning about Python and writing code with it once, I was fascinated by its simplicity, excellent readability, and useful one-line code. Next, I'll introduce you to a line of code that might be helpful for your Python project.

1. Swap two variables

# a = 1; b = 2
a, b = b, a
# print(a,b) >> 2 1

Let's start with a classic: by simply exchanging assignment positions to exchange the values of variables -- in my opinion, this is the most intuitive approach. T emporary variables are not required. It even applies to more than two variables.

2. Multiple variable assignments

a, b, *c = [1,2,3,4,5]
# print(a,b,c) >> 1 2 [3, 4, 5]

Swap variables are actually a special case where python can allocate multiple variables at once. H ere, you can use it to assign list elements to a given variable, also known as a de-table. * remaining values will be packaged again, which will result in a sublist of c It can even be used in other places of * (such as the beginning or middle part of the list).

3. Sum every two elements of the list

# a = [1,2,3,4,5,6]
s = sum(a[1::2])
# print(s) >> 12

There is no need for a special reduce function here, sum simply adds items for each given iteration. T he second element is returned here using the [::] slice syntax . You can read it as [start: stop: step] so [1::2] translation starts with the element of index 1 (the second element) until the end of the list (the second argument does not give parameters), and always takes two steps.

(Recommended tutorial: python tutorial)

4. Delete multiple elements of the list

# a = [1,2,3,4,5]
del a[::2]
# print(a) >> [2, 4]

Extended tile syntax can also be used to remove multiple list elements at once.

5. Read the file into an array of rows

c = [line.strip() for line in open('file.txt')]
# print(c) >> ['test1', 'test2', 'test3', 'test4']

With python inline for loops, you can easily read files into an array of rows. Y ou need to use strip() to remove the broken line that follows. If you want to keep them or they don't matter to you, you can use shorter lines:

c = list(open('file.txt'))
# print(c) >> ['test1\n', 'test2\n', 'test3\n', 'test4\n']

Reading files in Python is really easy. Note: You can also use the readlines() method if you prefer.

6. Write the string to the file

with open('file.txt', 'a') as f: f.write('hello world')
# print(list(open('file.txt'))) >> ['test1\n', 'test2\n', 'test3\n', 'test4\n', 'hello world']

With the help of the With statement, you can write content directly to a file. Make sure to open the file in the correct mode (where "a" means additional content).

7. Create a list

l = [('Hi ' + x) for x in ['Alice', 'Bob', 'Pete']]
# print(l) >> ['Hi Alice', 'Hi Bob', 'Hi Pete']

You can use inline for loops for dynamically create lists from other lists. You can modify the values directly, just like string connections in this example.

8. List mapping

l = list(map(int, ['1', '2', '3']))
# print(l) >> [1, 2, 3]

You can also use Pythons map() function to cast each list element to another type.

(Recommended micro-course: python3 basic micro-course)

9. Collection creation

squares = { x**2 for x in range(6) if x < 4 }
# print(squares) >> {0, 1, 4, 9}

The same is true for collections. In addition to inline for loops, you can even add conditions directly!

10. Reply check

# phrase = 'deleveled'
isPalindrome = phrase == phrase[::-1]
# print(isPalindrome) >> true

A reply is a series of characters that read the same characters forward and backward. I f a given string is an echo, some loops and conditions are usually required to check. I n Python you only need to compare the string with its reverse string. In addition to using the slice operator [::-1] you can also use reverse() function to reverse the string.

That's some of the knowledge about Python one-line code, and I hope it's helpful.

Original English: dev.to/devmount/10-awesome-pythonic-one-liners-explained-3doc