<jupyter_start><jupyter_text>TABLE OF CONTENTS:These tips are focused on general Python tips I think that are good to know.Please go through official documentation if you want more thorough examples.Topics: - [Additional Operators](op) - [Global](global) - [Comparisons](compare) - [Enumerate](enum) - [Comprehension](comp)     - [List](list)     - [Set](set)     - [Dict](dict)<jupyter_code># # Uncomment if you want to use inline pythontutor

# from IPython.display import IFrame

# IFrame('http://www.pythontutor.com/visualize.html#mode=display', height=1500, width=750)<jupyter_output><empty_output><jupyter_text>Additional Operators[Return to table of contents](toc)Operators besides you typical `+`, `-`, `/`, etc. `~`     Inversion, is the bitwise complement operator in python which essentially calculates `-x - 1``=`	    Assign value of right side of expression to left side operand	`x = y + z``+=`	Add AND: Add right side operand with left side operand and then assign to left operand	`a+=b`     `a=a+b``-=`	Subtract AND: Subtract right operand from left operand and then assign to left operand	`a-=b`       `a=a-b``*=`	Multiply AND: Multiply right operand with left operand and then assign to left operand	`a*=b`       `a=a*b``/=`	Divide AND: Divide left operand with right operand and then assign to left operand	`a/=b`         `a=a/b``%=`	Modulus AND: Takes modulus using left and right operands and assign result to left operand	`a%=b`   `a=a%b``//=`	Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand `a//=b`       `a=a//b``**=`	Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand	`a**=b`    `a=a**b``&=`	Performs Bitwise AND on operands and assign value to left operand	`a&=b`     `a=a&b``|=`	Performs Bitwise OR on operands and assign value to left operand	`a|=b`         `a=a|b``^=`	Performs Bitwise xOR on operands and assign value to left operand	`a^=b`       `a=a^b``>>=`	Performs Bitwise right shift on operands and assign value to left operand	`a>>=b`     `a=a>>b``<<=`	Performs Bitwise left shift on operands and assign value to left operand	`a <<= b`                    `a= a << b` Or assignmentAssigning a variable based on another variable's assignment.<jupyter_code>var = None
b = None or var
print(b)
var = 5
b = None or var
print(b)<jupyter_output>5<jupyter_text>Global[Return to table of contents](toc) Global lets you access global variables. In this example the global variable `c` is outside of the functions scope.<jupyter_code># c = 1

# def add():
#     c = c + 2
#     print(c)

# add()

# # UnboundLocalError: local variable 'c' referenced before assignment
c = 1

def add():
    global c
    c = c + 2
    print(c)

add()<jupyter_output>3<jupyter_text>Comparisons[Return to table of contents](toc) Max<jupyter_code># Find the max of an array/variables/values etc.

print(max(5, 2))

print(max([10,11,12,13]))<jupyter_output>5
13<jupyter_text>Min<jupyter_code># Find the min of an array/variables/values etc.

print(min(5, 2))

print(min([10,11,12,13]))<jupyter_output>2
10<jupyter_text>float("inf")/float("-inf")<jupyter_code># Setting a value to infinity or -infinity lets you have an easy comparion

print(float("inf") > 309840)
print(float("-inf") < -930984)<jupyter_output>True<jupyter_text>Enumerate[Return to table of contents](toc)This lets you take the element's index and use it as a variable.<jupyter_code>pies = ["apple", "blueberry", "lemon"]

for num, i in enumerate(pies):
    print(num, ":", i)
# For dictionaris this is what it would look like.

pies = {"pie1":"apple", "pie2":"blueberry", "pie3":"lemon"}

for key, value in pies.items():
    print(key, ":", value)<jupyter_output>pie1 : apple
pie2 : blueberry
pie3 : lemon<jupyter_text>Comprehensions[Return to table of contents](toc)Are a quicker way to create lists, dicts and sets, they act like for loops and can take conditions as well as if else statements. List comprehension[Return to table of contents](toc)<jupyter_code># Manual way to make a list

list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list_2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

print(list_1)
print(list_2)
# Using list()

list_1_with_list = list(range(1, 11))
list_2_with_list = list(range(11, 21))

print(list_1_with_list)
print(list_2_with_list)
# List comprehension autogenerates the list, they function closely to for loops.

list_1_with_comp = [x for x in range(1, 11)]
list_2_with_comp = [x for x in range(11, 21)]

print(list_1_with_comp)
print(list_2_with_comp)
# Works with functions.

def addition(x):
    return x + x

[addition(x) for x in range(0, 3)]
# Also works with conditions
# The % is modulus which gives you the remainder after division.

# If an number is even add 1 if it's odd add 3

[x + 1 if x%2 ==0 else x + 3 for x in range(1, 11)] 
# Without an else statement if goes at the end of the statement.

[x + 1 for x in range(1, 11) if x%2 ==0] 
# Nested loop example to show how they function like for loops.

for a in range(0, 3):
    for b in range(0, 5):
        print(a, b)
# List comp also works as a nested loop.

[[a, b] for a in range(0, 3) for b in range(0, 5)]<jupyter_output><empty_output><jupyter_text>Set comprehension[Return to table of contents](toc)<jupyter_code># Set comprehension is same format as list comprehension but uses curly brackets.

set_1_with_comp = {x for x in range(1, 11)}
set_2_with_comp = {x for x in range(11, 21)}
print(set_1_with_comp)
print(set_2_with_comp)<jupyter_output>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}<jupyter_text>Dict comprehension[Return to table of contents](toc)Examples from: http://cmdlinetips.com/2018/01/5-examples-using-dict-comprehension/ (More samples there as well)<jupyter_code># dict comprehension to create dict with numbers as values
{str(i):i for i in [1,2,3,4,5]}
# create list of fruits
fruits = ["apple", "mango", "banana", "cherry"]

# dict comprehension to create dict with fruit name as keys
{f:len(f) for f in fruits}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12469074715487383, 'fraction_numerical': 0.032162295893122216, 'mean_word_length': 4.64093023255814, 'pattern_counts': {'":': 5, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '36312', 'license': 'MIT', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2091, 'n_words': 762, 'path': 'general_tips.ipynb', 'repo_name': 'gpetepg/python_tips', 'size': '6063'}