A Python dictionary is a very useful data structure that stores key value pairs. A python dictionary is declared like the following-
Why write more code when you can write less? Become a Python Ninja with the below one-liners.
if-elif statements are one of the used features of Python. However, they can become hard to read when the number of elif statements increase. Let’s consider the following code for a game: # Assume run(), stop(), shoot(), guard() methods are defined command = input() if command == "run" run() elif command == "stop" stop() elif command == "shoot" shoot() elif command == "guard" guard() ... ... ... There are multiple actions that our protagonist can do. Additionally, the game devs are planning to add more actions for the protagonist. Soon this action list will become big and hard to read.
Avoid these things in Python to write better code and make better software.
Python is known for being slow in terms of execution speed. However, you can try out the following tricks to make your code faster.
Python f-strings are super useful for formatting strings. Before python 3.6 str.format() were used. But f-strings makes string manipulation much more easier and effective.