<jupyter_start><jupyter_text>Best Practise 1 → Using enumerate() - Fetch elements from list<jupyter_code># List Variable
example = ['use','enumerate','instead','of','iteration']

# Ideal Way
for i in range(len(example)):
    print(f"# {i + 1}: {example[i]}")
          
# Pythonic way - enumerate
for i, value in enumerate(example, 1):
    print(f"# {i}: {value}")<jupyter_output># 1: use
# 2: enumerate
# 3: instead
# 4: of
# 5: iteration
# 1: use
# 2: enumerate
# 3: instead
# 4: of
# 5: iteration<jupyter_text>Best Practise 2 → Using zip() - Fetch elements from multiple lists<jupyter_code># Lists 
Employees = ['Employee1','Employee2','Employee3','Employee4']
Age = [30,25,35,40]

# Ideal Way
for i in range(len(Employees)):
    employee = Employees[i]
    age = Age[i]
    print(f"Employee name is {employee} and age is {age}")
    
# Pythonic way - zip
for employee, age in zip(Employees, Age):
    print(f"Employee name is {employee} and age is {age}")<jupyter_output>Employee name is Employee1 and age is 30
Employee name is Employee2 and age is 25
Employee name is Employee3 and age is 35
Employee name is Employee4 and age is 40
Employee name is Employee1 and age is 30
Employee name is Employee2 and age is 25
Employee name is Employee3 and age is 35
Employee name is Employee4 and age is 40<jupyter_text>Best Practise 3 → Using reversed() - Fetch elements reversly<jupyter_code># Lists 
Employees = ['Employee1','Employee2','Employee3','Employee4']

# Ideal way
for i in range(1,len(Employees) + 1):
    print(f"Approach 1 - Employee came to office after covid 19 is {Employees[-i]}")
for employee in Employees[::-1]:
    print(f"Approach 2 - Employee came to office after covid 19 is {employee}")
    
# Pythonic way - reversed()
for employee in reversed(Employees):
    print(f"Using revered -  Employee came to office after covid 19 is {employee}")<jupyter_output>Approach 1 - Employee came to office after covid 19 is Employee4
Approach 1 - Employee came to office after covid 19 is Employee3
Approach 1 - Employee came to office after covid 19 is Employee2
Approach 1 - Employee came to office after covid 19 is Employee1
Approach 2 - Employee came to office after covid 19 is Employee4
Approach 2 - Employee came to office after covid 19 is Employee3
Approach 2 - Employee came to office after covid 19 is Employee2
Approach 2 - Employee came to office after covid 19 is Employee1
Using revered -  Employee came to office after covid 19 is Employee4
Using revered -  Employee came to office after covid 19 is Employee3
Using revered -  Employee came to office after covid 19 is Employee2
Using revered -  Employee came to office after covid 19 is Employee1<jupyter_text>Best Practise 4 → Using filter() - Data Filtering<jupyter_code># List
numbers = [1,2,3,4,5,6,7,8,9,10]

#Ideal way
for number in numbers:
    if number % 2:
        print(f"Odd Number : {number}")

# Pythonic way - filter()
for number in filter(lambda x: x %2, numbers):
    print(f"Odd Number : {number}")<jupyter_output>Odd Number : 1
Odd Number : 3
Odd Number : 5
Odd Number : 7
Odd Number : 9
Odd Number : 1
Odd Number : 3
Odd Number : 5
Odd Number : 7
Odd Number : 9<jupyter_text>Best Practise 5 → Using Chain() - Concatenate values from lists<jupyter_code>from itertools import chain

#Lists
oddValues = [1,3,5,7,9]
evenValues = [2,4,6,8,10]

# Ideal way
values = oddValues + evenValues
for value in values:
    print(f"value is : {value}")

# Pythonic way - chain()
for value in chain(oddValues, evenValues):
    print(f"value is : {value}")<jupyter_output>value is : 1
value is : 3
value is : 5
value is : 7
value is : 9
value is : 2
value is : 4
value is : 6
value is : 8
value is : 10
value is : 1
value is : 3
value is : 5
value is : 7
value is : 9
value is : 2
value is : 4
value is : 6
value is : 8
value is : 10<jupyter_text>Best Practise 6 → Using Dictionaries() - Retrieve keys & values from dictionary<jupyter_code># Dict
Employees = {"Employee1": 30, "Employee2": 35, "Employee3": 40, "Employee4": 45}

#Ideal way
for key in Employees:
    print(f"Employee Name is : {key}")
for key in Employees.keys():
    print(f"Employee Name is : {key}")
for value in Employees.values():
    print(f"Age is : {value}")
for value in Employees:
    print(f"Age is : {Employees[value]}")
    
#Pythonic way
for key, value in Employees.items():
    print(f"Employee came to office after covid 19 is {key} and age is {value}")<jupyter_output>Employee Name is : Employee1
Employee Name is : Employee2
Employee Name is : Employee3
Employee Name is : Employee4
Employee Name is : Employee1
Employee Name is : Employee2
Employee Name is : Employee3
Employee Name is : Employee4
Age is : 30
Age is : 35
Age is : 40
Age is : 45
Age is : 30
Age is : 35
Age is : 40
Age is : 45
Employee came to office after covid 19 is Employee1 and age is 30
Employee came to office after covid 19 is Employee2 and age is 35
Employee came to office after covid 19 is Employee3 and age is 40
Employee came to office after covid 19 is Employee4 and age is 45<jupyter_text>Best Practise 7 → Using Comprehension() - Comprehensions for lists, dictionaries & set<jupyter_code>### list
numbers = [1,2,3,4,5,6,7,8,9,10]

#Ideal way
squaredNumbers = list()
for square in numbers:
    squaredNumbers.append(square * square)
print(squaredNumbers)

#Using list comprehension
squaredNumbers = [x * x for x in numbers]
print(squaredNumbers)

#Ideal way
squaredNumbers = dict()
for square in numbers:
    squaredNumbers[square] = square * square
    
#Using list comprehension
squaredNumbers = {x: x*x for x in numbers}
print(squaredNumbers)

#Ideal way
squaredNumbers = set()
for square in numbers:
    squaredNumbers.add(square)
print(squaredNumbers)

#Using list comprehension
squaredNumbers = [x*x for x in numbers]
print(squaredNumbers)<jupyter_output><empty_output><jupyter_text>Best Practise 8 → Using else clause - For and While Loops<jupyter_code># For Loop
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print( n, 'equals', x, '*', n/x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')

# While Loop
count = 2
while (count < 1):     
    count = count+1
    print(count) 
    break
else: 
    print("No Break")<jupyter_output>2 is a prime number
3 is a prime number
4 equals 2 * 2.0
5 is a prime number
6 equals 2 * 3.0
7 is a prime number
8 equals 2 * 4.0
9 equals 3 * 3.0
No Break<jupyter_text>Best Practise 9 → **Using Ternary Opertor** - Ternary Opertor<jupyter_code>#Traditional
    value = True
    if value:
        v = 1
    else:
        v = 0
    print(v)

    #Using ternary
    value = True
    v = 1 if value else 0
    print(v)<jupyter_output>1
1
{'fraction_non_alphanumeric': 0.09453032104637336, 'fraction_numerical': 0.04087395957193817, 'mean_word_length': 3.918859649122807, 'pattern_counts': {'":': 4, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '11381', 'license': 'Apache-2.0', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2184, 'n_words': 908, 'path': '_notebooks/2020-02-20-PythonBestPractises.ipynb', 'repo_name': 'SurendraRedd/Learnings2020', 'size': '6728'}