# now, let's see some Pythonic way to Coding in Python 😁
# look, if we have to generate a List from 1 to 1000 numbers which are only divisible by 3, we will do
# List
lst1 = []
for i in range(1, 1000): # General Way
if i % 3 == 0:
lst1.append(i)
print(lst1)
# yeah right, it's a legit way and applicable for almost every Programming Language, but we can do things in just 1 Line in Python 😏, let's see.
lst2 = [i for i in range(1, 50)] # Pythonic Way
print(lst2)
# adding conditions
lst3 = [i for i in range(1, 1000) if i % 100 == 0]
print(lst3)
# it's called the real Pythonic Way 😏
# Dict
dict1 = {}
# General Way
for i in range(50):
dict1[f"Item{i}"] = i
print(dict1)
# Pythonic Way
dict2 = {i : f"Item{i}" for i in range(10)}
print(dict2)
0 Comments