25 Interesting Python Codes to Solve Complex Tasks

Part 1: A list of Python codes for solving complex tasks.

image

In the process of learning Python, you will always find that Python can easily solve many problems. Some complex tasks can even be done with a single line of Python code.

Here are 25 interesting Python codes, all of which are practical. I hope you can find they are useful.

1. Letter Anagram

If two words contain the same letters and are in a different order, they are called anagrams. For example, “silent” and “listen” are letter anagrams, while “apple” and “aplee” are not.

from collections import Counter

s1 = 'below'
s2 = 'elbow'

print('anagram') if Counter(s1) == Counter(s2) else print('not an anagram')

2. Binary to Decimal

decimal = int('1010', 2)
print(decimal) #10

3. Convert String to Lowercase

print("Hi my name is Tony Xu".lower())
# 'hi my name is tony xu'

print("Hi my name is Tony Xu".casefold())
# 'hi my name is tony xu'

4. Convert String to Uppercase

print("hi my name is Tony Xu".upper())
# 'HI MY NAME IS Tony Xu'

5. Convert String to Bytes

print("convert string to bytes using encode method".encode())
# b'convert string to bytes using encode method'

6. Copy File

import shutil

shutil.copyfile('source.txt', 'dest.txt')

7. Quick Sort

qsort = lambda l: l if len(l) <= 1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])

print(qsort([17, 29, 11, 97, 103, 5]))
# [5, 11, 17, 29, 97, 103]

8. Sum of N Numbers

n = 10

print((lambda x: (x*(x+1))/2)(n)
# 55

9. Switch the Value of Two Variables

a, b = b, a

10. Fibonacci

fib = lambda x: x if x<=1 else fib(x-1) + fib(x-2)

print(fib(20))
# 6765

11. Combine Nested Lists into One List

main_list = [[0, 1, 2], [11, 12, 13], [52, 53, 54]]

result = [item for sublist in main_list for item in sublist]
print(result)

>
[0, 1, 2, 11, 12, 13, 52, 53, 54]

12. Run an HTTP Server

python3 -m http.server 8000
python2 -m SimpleHTTPServer

13. Reverse a List

numbers = [0, 1, 2, 11, 12, 13, 52, 53, 54]

print(numbers[::-1])
# [54, 53, 52, 13, 12, 11, 2, 1, 0]

14. Factorial

import math

fact_5 = math.factorial(5)
print(fact_5)
# 120

15. Use for and if in List Comprehensions

even_list = [number for number in [1, 2, 3, 4] if number % 2 == 0]

print(even_list)
# [2, 4]

16. The Longest String in List

words = ['This', 'is', 'a', 'list', 'of', 'words']

result = max(words, key=len)
print(result)
# 'words'

17. List Comprehension

li = [num for num in range(0, 10)]

print(li)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

18. Set Comprehension

num_set = {num for num in range(0, 10)}

print(num_set)
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

19. Dict Comprehension

dict_numbers = {x: x*x for x in range(1, 5)}

print(dict_numbers)
# {1: 1, 2: 4, 3: 9, 4: 16}

21. Print with if and else

print("even") if 4 % 2==0 else print("odd")

22. Infinite Loop

while 1:0

23. Check Data Type

print(isinstance(2, int))
# True

print(isinstance("allwin", str))
# True

print(isinstance([3, 4, 1997], list))
# True

24. Print to File

print("Hello, World!", file=open('file.txt', 'w'))

25. Frequency of a Character in a String

print("umbrella".count('l'))
# 2

And there you have it. Thank you for reading.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics