Python >> Tips and Tricks 4
Table of Contents
This tutorial will explain how to use enumerate, Enum, switch case/match case, base64, bytes, bytearray.
Python how to use enumerate to loop all items and index
Loop all items and index in a list
# Loop all items and index
mylist = ["a", "b", "c"]
for i, item in enumerate(mylist):
print(i, item)
0 a
1 b
2 c
Loop all items and index in a tuple
# Loop all items and index
mytuple = ("a", "b", "c")
for i, item in enumerate(mytuple):
print(i, item)
0 a
1 b
2 c
Loop all items and index in a dictionary
# Loop all items and index
mydict = {"a": 1, "b": 2, "c": 3}
for i, item in enumerate(mydict):
print(i, item)
This will get the index and keys of dictionary.
0 a
1 b
2 c
Loop all items and index in a set
# Loop all items and index
myset = {"a", "b", "c"}
for i, item in enumerate(myset):
print(i, item)
0 c
1 a
2 b
Python how to use enum
Define enum
# Define enum
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Use enum
# Use enum
print(Color.RED.name)
print(Color.RED.value)
RED
1
Compare enum
# Compare enum
my_color = Color.RED
if my_color == Color.RED:
print("My color is RED")
else:
print("My color is not RED")
if my_color == Color.BLUE:
print("My color is BLUE")
else:
print("My color is not BLUE")
My color is RED
My color is not BLUE
Iterate all enums
# Loop all enums
for color in Color:
print(color)
Color.RED
Color.GREEN
Color.BLUE
Python match case / switch case
From Python 3.10 onwards, you can use match
and case
statement instead of if
statement.
# Switch case
my_lang = 'python'
match my_lang:
case "python":
print("Python")
case "java":
print("Java")
case "c++":
print("C++")
case "c#":
print("C#")
case "javascript":
print("Javascript")
case "go":
print("Go")
case _:
print("Unknown")
Python
Python base64
Encode base64
# Encode base64
import base64
my_string = "Hello World"
my_base64 = base64.b64encode(my_string.encode())
print(my_base64)
b'SGVsbG8gV29ybGQ='
Decode base64
# Decode base64
import base64
my_base64 = "SGVsbG8gV29ybGQ="
my_string = base64.b64decode(my_base64)
print(my_string.decode("utf-8"))
Hello World
Python bytes
Convert string to bytes
# Convert string to bytes
my_string = "Hello World"
my_bytes = bytes(my_string, "utf-8")
print(my_bytes)
b'Hello World'
Convert bytes to string
# Convert bytes to string
my_bytes = b"Hello World"
my_string = my_bytes.decode("utf-8")
print(my_string)
Hello World
Convert bytes to JSON
# Convert bytes to JSON
import json
my_bytes = b'{"name": "John", "age": 30}'
print(json.loads(my_bytes))
{'name': 'John', 'age': 30}
Convert bytes to dict
# Convert bytes to dict
import ast
my_bytes = b'{"name": "John", "age": 30}'
dict_str = my_bytes.decode("utf-8")
print(ast.literal_eval(dict_str))
{'name': 'John', 'age': 30}
Python bytearray
Create bytearray
# Create bytearray
my_bytearray = bytearray(b"Hello World")
# OR
my_bytearray = bytearray("Hello World", "utf-8")
print(my_bytearray)
bytearray(b'Hello World')
bytearray to string
# bytearray to string
my_bytearray = bytearray(b"Hello World")
print(my_bytearray.decode("utf-8"))
Hello World
bytearray to JSON
# bytearray to JSON
import json
my_bytearray = bytearray(b'{"name": "John", "age": 30}')
print(json.loads(my_bytearray))
{'name': 'John', 'age': 30}
Difference between bytes and bytearray
bytes
is an immutable version of bytearray
.
# Difference between bytes and bytearray
my_bytes = b"Hello World"
my_bytearray = bytearray(b"Hello World")
print(my_bytes == my_bytearray)
True