# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # TABLE OF CONTENTS:<a id='toc'></a>
#
# 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.
#
# <b>Topics:</b>
#  - <b>[Additional Operators](#op)</b>
#  - <b>[Global](#global)</b>
#  - <b>[Comparisons](#compare)</b>
#  - <b>[Enumerate](#enum)</b>
#  - <b>[Comprehension](#comp)</b>
#      - [List](#list)
#      - [Set](#set)
#      - [Dict](#dict)

# +
# # 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)
# -

# # Additional Operators<a id="op"></a>
# [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`

# <b>Or assignment</b>
#
# Assigning a variable based on another variable's assignment.

var = None
b = None or var
print(b)

var = 5
b = None or var
print(b)

# # Global<a id="global"></a>
# [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.

# +
# 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()
# -

# # Comparisons<a id="compare"></a>
# [Return to table of contents](#toc)

# <b>Max<b>

# +
# Find the max of an array/variables/values etc.

print(max(5, 2))

print(max([10,11,12,13]))
# -

# <b>Min<b>

# +
# Find the min of an array/variables/values etc.

print(min(5, 2))

print(min([10,11,12,13]))
# -

# <b>float("inf")/float("-inf")<b>

# +
# Setting a value to infinity or -infinity lets you have an easy comparion

print(float("inf") > 309840)
# -

print(float("-inf") < -930984)

# # Enumerate<a id="enum"></a>
# [Return to table of contents](#toc)
#
# This lets you take the element's index and use it as a variable.

# +
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)
# -

# # Comprehensions<a id="comp"></a>
# [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.

# <b>List comprehension</b><a id='list'></a>
#
# [Return to table of contents](#toc)

# +
# 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)]
# -

# <b>Set comprehension</b><a id='set'></a>
#
# [Return to table of contents](#toc)

# +
# 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)

# <b>Dict comprehension</b><a id='dict'></a>
#
# [Return to table of contents](#toc)
#
# Examples from: http://cmdlinetips.com/2018/01/5-examples-using-dict-comprehension/ (More samples there as well)

# 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}

StarcoderdataJupyterScriptsDedupFiltered