<jupyter_start><jupyter_text>What is the purpose of programming?If you do much work on computers, eventually you find that there’s some task you’d like to automate.The goal of this course is to help you learn the basics of how to translate algorithmic or mathematical ideas you may have into instructions that your computer can understand and implement. Programming languages provide the interface that allows you to do so and each language does so in unique ways. While each language is different, the basic principles of programming translate across language and this is what we want you to understand. Python is one of the most popular languages in use today and is used by people from all fields.   A Little Bit of Jupyter... As you can see in this notebook there are these boxes that you can type in- each box can store either code that python will try to "interpret" or text which will just display. BUT you need to tell the jupyter notebook what you intend to do by changing the type of the cell using the dropdown menu: Cell --> CellType --> Code/Markdown. To evaluate a cell use Shift-Enter<jupyter_code>This is not a text cell but I am typing text<jupyter_output><empty_output><jupyter_text>This is a text cell and now the text is not interpreted.<jupyter_code># If you want to put text in a code cell- use the # symbol before the text<jupyter_output><empty_output><jupyter_text>Note you can insert or delete cells using these menus: Insert --> Cell Above/Below or Edit --> Delete Cells  Functions and Arguments (a very brief intro.. )![image.png](attachment:image.png)  Data TypesWe will talk more about this later but when you write things in Code, every item has a type... you can find out the type of something using the type() function<jupyter_code>type(3) # this is an integer
type(3.0) # this is a float
type("3.0, abc") # this is a string<jupyter_output><empty_output><jupyter_text>Why did the above just print str when it should have printed float and then str? This is because jupyter notebook will evaluate the code in the most recent line and put that in the output. If you want to print something along the way there is a python command for that!<jupyter_code>print(type(3.0))
print(type("3.0, abc")) 
print("Hello World...")
#Printing two things on the same line
print("3", 4)
help(print)<jupyter_output>Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.<jupyter_text>Casting<jupyter_code>#What if I want to convert that float to an integer or a string?
int(3.0)
str(3.0)
print(type(str(3.0)))<jupyter_output><class 'str'><jupyter_text>Python as a calculator!<jupyter_code># Arithmetic operations
5 + 2
5 - 2
5 * 2
# We can check the type of the output as before
type(5*2)
# floating point by default
print(5 / 2)
print(type(5/2))
5 // 2
5 ** 2
3 * * 4 #spaces matter if it changes the operation
3*4 # here the lack of spaces doesnt change each item
5 ** 0.5
# order of operations
(5 + 4/2) ** 2<jupyter_output><empty_output><jupyter_text>Python is a smart calculator ...<jupyter_code>"3.0" + "4.0" # the addition operation can be used to combine strings
"3.0" - "4.0"
"3" + 4 # we can't add a string and int<jupyter_output><empty_output><jupyter_text>Comparisons<jupyter_code>print(3 == 3)
print(1 == 3)
print("abc" == "abc")
print("abc" == "ABC")
print(abc == "abc")<jupyter_output><empty_output><jupyter_text>Basics of ConditionalsWe just saw comparisons- comparisons return True or False and this can be used to tell the computer what to do.For example if 'str1' == 'str2': print("Yes they are equal!"). Let's try this... remember after conditions like if you need a colon and indentation of what you want to do.<jupyter_code>if "abc" == "abc":
    print("Yes they are equal!")<jupyter_output>Yes they are equal!<jupyter_text>What if that is not true and we want to do something? We can use the "not" operator for this<jupyter_code>print("Comparing first...")
if not "abc" == "abc":
    print("No they are not equal!")

print("Comparing second...")
if not "ABC" == "abc":
    print("No they are not equal!")<jupyter_output>Comparing first...
Comparing second...
No they are not equal!<jupyter_text>We can combine these into one statement using the "else" idea.. just as in english the commands are staged if .. do this, else.. do something else.<jupyter_code>if "abc" == "abc":
    print("Yes they are equal!")
else:
    print("No they are not equal!")<jupyter_output>Yes they are equal!<jupyter_text>Keeping things around to use later... Variables!All those expressions output something but how do we store them to use them later? Python uses the '=' to assign things to certain names. The name then becomes a container for whatever is on the right of the equals<jupyter_code>a = 10
b = 20


a + b<jupyter_output><empty_output><jupyter_text>What is the type of a?<jupyter_code>type(a)<jupyter_output><empty_output><jupyter_text>But we can't use anything as names..<jupyter_code>10=20
print = 10 #bad! 
print(6)<jupyter_output><empty_output><jupyter_text>How do I delete a variable? (Little discussion about state )<jupyter_code>del print
print(6)
quarter = 1/4
half = 2 * quarter
print(half)
print(quarter)
print(half == quarter) # we can compare variables to see if they are the same<jupyter_output>0.5
0.25
False<jupyter_text>If there are a lot of variables printing is tedious... let's use the command whos. If we type 'whos' it will show all variables.<jupyter_code>whos<jupyter_output>Variable   Type     Data/Info
-----------------------------
a          int      10
b          int      20
half       float    0.5
quarter    float    0.25<jupyter_text>The golden ratio is a special number for many reasons it can be defined as  (1 + sqrt(5))/2. Store the golden ratio in a variable. Next convert this to a string and print one string that says: "This is the golden ratio (golden ratio)" where the parentheses refers to the string version of the golden ratio variable. Store another variable b that has value 1. Calculate 1 + 1/goldenratio ... what does this look like? Verify this in python using a comparison.<jupyter_code>goldenratio = (1 + 5 ** 0.5)/2
print("This is the golden ratio " + str(goldenratio))
print(1+ (1/goldenratio))

print(goldenratio == 1+1/goldenratio)<jupyter_output>This is the golden ratio 1.618033988749895
1.618033988749895
True<jupyter_text>SequencesIf we want to put a sequence of things into one item (a la an excel row) how do we do that? One way is through the data type list.<jupyter_code>x = [1,2,3,4,5]
print(x)<jupyter_output>[1, 2, 3, 4, 5]<jupyter_text>How do I grab an element? In python indexing is done through square brackets[] we will see more of this later, but for a list the brackets will require an integer to select an index. Python indexes starting at 0!<jupyter_code>x[0]
x[1.0]
x[-1] #you can index the end through negatives
x[-2]
#The : allows you to get all elements after or before the index
print(x[1:])
print(x[:4])
#lists can store other types too
y = ['1', 2, '3', 4.0]<jupyter_output><empty_output><jupyter_text>Basic loopsHow do I go through each element of y and print it's type?<jupyter_code>for element in y: #note  the colon
    print(type(element))<jupyter_output><class 'str'>
<class 'int'>
<class 'str'>
<class 'float'><jupyter_text>What if I wanted to loop through y and just print the 3rd element?<jupyter_code>#python uses the range() function to create a list of numbers
size_y = len(y)
print(range(size_y))
for i in range(size_y):
    #index 2 is the 3rd element uis a conditional! 
    if i == 2:
        print(y[i])
        
#print index of elements that equal '3'
for i in range(size_y):
    if y[i] == '3':
        print("Index of element that equals '3':", i)<jupyter_output>3
Index of element that equals '3': 2<jupyter_text>HARDER Example: What if we for some reason couldn't calculate the size of y and still want to print the 3rd element? We can use what is known as a while loop.<jupyter_code>i = 0
while True:
    print("Index: " , i)
    if i == 2:
        print(y[i])
        break
    i += 1<jupyter_output>Index:  0
Index:  1
Index:  2
3
{'fraction_non_alphanumeric': 0.0955743879472693, 'fraction_numerical': 0.020833333333333332, 'mean_word_length': 4.300686213349969, 'pattern_counts': {'":': 4, '<': 82, '<?xml version=': 0, '>': 86, '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': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '6888', 'license': 'MIT', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2610, 'n_words': 1169, 'path': 'Day1/morning_python_intro.ipynb', 'repo_name': 'nikparth/mathtools-bootcamp2019', 'size': '8496'}