<jupyter_start><jupyter_text>Basic Maths Operations<jupyter_code>21/21 #always returns float

2**3 #returns 2^3 (int or float it depends on the inputs)
21//4 #returns the integer part (int or float it depends on the inputs)
21%4 #returns the remainder (int or float it depends on the inputs)<jupyter_output><empty_output><jupyter_text>String operations<jupyter_code>x = 'this is a string' #strings are immutable

x[3] #indexing - returns (i+1)th character in x
x[-1] #Returns the (n-i+1)th character in x
len(x) #Returns the length of the string (an integer)
x[:3] #slicing - Returns from 0 to 4th character in x
x[8:] #returns from 8th to the last character in x
x[0::2] #returns a substring starting from 0, going to the end (omitted), 2 characters at a time
x[::-1] #Start from whatever makes sense as the start, go to whatever makes sense as the end, go backward
# one character at a time
#Here it makes sense to start at the end and go all the way to the beginning (because of the -1)
#Returns a reversed string
x.find('is') #Searching - Returns the location of the first 'is' found
y = 'now we have another string' #Concatenation

z = x + ',' + ' ' +y # Returns the concatenation of x plus y with space and comma

print(z)<jupyter_output>this is a string, now we have another string<jupyter_text>Boolean<jupyter_code>x=8
print(x,bool(x)) #Non-zero numbers, strings with values are always True
x='' #Empty string
print(x,bool(x)) #Empty strings, 0 numbers are always False<jupyter_output>False<jupyter_text>Logical Operators<jupyter_code>x=4
y=5
print(x>2 and y>2) #True because both x and y are greater than 2
print(x>2 or y>2) #True because at least one is True
print(not(x>2 or y>2)) #False because x>2 or y>2 is True
print(x or y) #4 because x is True (non-zero) so no need to evaluate y. Value of x is returned
print(x and 0+3) #3 because x is non-zero but need to check the second operand as well. That evaluates to 3<jupyter_output>3<jupyter_text>Assignment<jupyter_code>x,y = 3,4
print(x,y) #x is 3 and y is 4
x = 7
x += 4 
print(x) #x is 11 because x += 4 is equivalent to x = x+4
len("Hi there!")*2**3/4<jupyter_output><empty_output><jupyter_text>Variables and Values<jupyter_code>x = 38
id(x)
y = 15
id(y)
x = y
id(x)
id(15) #location is de value not de variable<jupyter_output><empty_output><jupyter_text>If statement<jupyter_code>purchase_price = float(input("Enter the purchase price of the stock: ")) #input is always a string, thus it must be changed if it's a number 
price_now = float(input("Enter the current price of the stock: "))
if price_now < purchase_price * 0.9:
    print("STOP LOSS: Sell the stock! ")
elif price_now > purchase_price * 1.2: #elif
    print("PROFIT TAKING: Sell the stock!")
else:
    print("HOLD: Don't do anything!")<jupyter_output>Enter the purchase price of the stock: 10
Enter the current price of the stock: 9
HOLD: Don't do anything!<jupyter_text>Installing libraries and importing functions<jupyter_code>!pip install easygui 
#pip: python installer program
# ! run the program from the shell (not from python)
# easygui: a python library for GUI widgets
import easygui #Imports easygui into the current namespace. We now have access to functiona and objects in this library
easygui.msgbox("To be or not to be","What Hamlet elocuted") #msgbox is a function in easygui.<jupyter_output><empty_output><jupyter_text>Importing functions<jupyter_code>import math as m #imports the math namespace into our program namespace but gives it the name 'm'
m.sqrt(34.23) #Functions in the math namespace have to be disambiguated using the name 'm' rather than 'math'
from math import sqrt #imports the sqrt function into our program namespace. No other math functions are accessible
sqrt(34.23) #No disambiguation necessary<jupyter_output><empty_output><jupyter_text>Returning values from a functionThe return statement tells a function what to return to the calling program<jupyter_code>def spam(x,y,k): #define a function
    if x>y:
        z=x
    else:
        z=y
    p = z/k
    return p #Only the value of p is returned by the function
#it could return more than one values like "return p, x, y", for instance
def foo(a, b, function):
    z = function(a,b)
    return z, a, b
foo(2, 4, max)
a, b, c = foo(3, 4, min)
print(a, b, c)
spam(4, 3, 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09412580943570768, 'fraction_numerical': 0.020582793709528216, 'mean_word_length': 4.93278463648834, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 47, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '554190', 'license': 'MIT', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1428, 'n_words': 607, 'path': 'tiny_intro_to_python/Week 1 - Python Data type - IF - Functions.ipynb', 'repo_name': 'caiomts/scripts-tsm-fit', 'size': '4324'}