<jupyter_start><jupyter_text>Introduction to python  Basic Operations<jupyter_code># There are many basic mathematics operations, most common are

# Addition 

1+1
# Subtraction 

1-1-1-1-1
# Multiplication and division

10*10/5
# Powers

10**2
# Note that order of operations, like in Maths, is very important. We can use () to be more specific if we want

5*5+5/2
(5*5+5)/2<jupyter_output><empty_output><jupyter_text>Variables<jupyter_code>i = 10

i
i+i
i = i + i
i
# Using variables allows us to make it more clear what we are doing and limit mistakes 

x = 5
y = 10
z = 20 

volume = x*y*z
volume
# Variables don't have to be numbers, or even single things, as we'll find out shortly. 

hello = "Good morning"
name = 'Bob'

hibob = hello + name
hibob
# let's see if we can fix to make it pretty

hibob = hello + ' ' + name
hibob
# But be careful and know what variable types you're using... 
# Is this the output you really want?
name + 5
# Or how about this

hibob * 5<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def myFirstFunction(a):
  return a*2

myFirstFunction(5)
# Combine with the previous mention of variables and we can do some fun things

res = myFirstFunction(10)
res = myFirstFunction(res)
res

# Or some crazy things, that may be quick to code but hard to read and for others to understand 

res = myFirstFunction(myFirstFunction(10))
res
# Using the print function for more output than just the last command entered: 

def volumeOfCuboid(x,y,z):
    print("X is equal to", x)
    print("Y is equal to", y)
    print("Z is equal to", z)
    print("----")
    volume = x*y*z
    print("Volume is:")
    print(volume)
    return volume

v = volumeOfCuboid(10,20,30)

a = 5
b = 10
c = 2

v = volumeOfCuboid(a,b,c)<jupyter_output>X is equal to 10
Y is equal to 20
Z is equal to 30
----
Volume is:
6000
X is equal to 5
Y is equal to 10
Z is equal to 2
----
Volume is:
100<jupyter_text>Lists, dictionaries and loops<jupyter_code>myShoppingList = ['milk', 'bread', 'tofu']
print(myShoppingList)
print('Opps I forgot to add apples')
myShoppingList.append('apples')
print(myShoppingList)
myShoppingList = ['milk', 'bread', 'tofu']

print("the first thing in my list is")
print(myShoppingList[0])
myShoppingList = ['milk', 'bread', 'tofu']

for i in myShoppingList:
    print(i)
Ages = {"Ben" : 10, "Spider-man": 34, "Claire": 24}
print(Ages["Ben"])
# notice what happens here, is it what you expect?
for i in Ages:
  print(i)
Ages = {"Ben" : 10, "Spider-man": 34, "Claire": 24}

print("ahh, we forgot to get Tom's age")
Ages['Tom'] = 17

print(Ages)<jupyter_output>ahh, we forgot to get Tom's age
{'Ben': 10, 'Spider-man': 34, 'Claire': 24, 'Tom': 17}<jupyter_text>Conditional statements<jupyter_code># Sometimes we want to only process data if certain conditions are met... 
# Or we might want to check if something is True, this can be done really easily... 


5 == 10
10==10
'carrots' == 'carrotz'
'carrots' == 'carrotz'
# Read as "10 is NOT equal to 10" which is a False statement. 
10 != 10
# We can use an 'in' statement in a similar way

'spider' in 'spider-man'
# let's build a piece of code which only runs if a condition is met 

age = 16

if age < 17:
  print('Youre too young to drive')
if age >= 17:
  print('You can get a license')
# we can shorten this

if age < 17: 
  print('Youre too young to drive')
else:
  print('you can get a license')
# Let's just combine some of what we've done together

Ages = {"Ben" : 10, "Spider-man": 34, "Claire": 24}

for person in Ages:
  if person == "Spider-man":
    print('Hey spidey')
  else:
    if Ages[person] < 18:
      print('Hey, who let a kid in here')
      print('Can ' + person + 's parents come and collect them')
    else:
      print("Hello" + " " + person)<jupyter_output>Hey, who let a kid in here
Can Bens parents come and collect them
Hey spidey
Hello Claire<jupyter_text>Importing libraries<jupyter_code>import numpy 
i = 100 
numpy.sqrt(i)
import numpy as np 
i = 100 
np.sqrt(i)
myArray = np.arange(1,10)
print(myArray)
start = 0
end = 50
num = 100

nums = np.linspace(start, end, num)
print(nums)
# we can also do some nice rounding with np 
decimals=1
np.around(nums, decimals)
# Numpy arrays can be accessed similar to lists

nums[10]
# and you can do some fun matrix-style maths on them 
# notice how each element has been multiplied 
nums * 5 
# Some other common functions with arrays
sums = np.sum(nums)
print("The sum of values is ")
print(sums)

means = np.mean(nums)
print("The mean of values is ")
print(means)

# You can try out others, such as np.min, np.max, np.median, np.std 

# Let's do some plotting and really get into the guts of data science

import matplotlib.pyplot as plt
import numpy as np

Xs = np.arange(0,10)
sin_values = [ ]

for v in Xs: 
  sin = np.sin(v)
  sin_values.append(sin)

plt.plot(sin_values)
# We can reuse these same values to do other kinds of plotting... like a histogram of the values 
# Not exciting outputs yet, but good for us to see that we're quickly building some tools that can help us explore data
plt.hist(sin_values)
import matplotlib.pyplot as plt 
import numpy as np 

sin_values = []
Xs = np.linspace(0,10, num=1000)
for x in Xs:
    sin_values.append(np.sin(x))
plt.plot(Xs, sin_values)
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid()<jupyter_output><empty_output><jupyter_text>Before moving on! Take some time to look over what we've just done. Think if you can modify and re-run with other functions. Maybe you can get 'cos' to work rather than 'sin' with a little bit of guess work. You will probably see some errors if something goes wrong, but that's good! It gives us a chance to learn!   Some errors and what they mean<jupyter_code>X = 
print(X)
X= 10
print(X
def f(x):
  print(notX)
f(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.108234075608493, 'fraction_numerical': 0.026238563783877095, 'mean_word_length': 4.047038327526132, 'pattern_counts': {'":': 7, '<': 29, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '518995', 'license': 'MIT', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2017, 'n_words': 804, 'path': 'SummerSchool2021_part1.ipynb', 'repo_name': 'SirSharpest/Python-Crash-Course', 'size': '5793'}