<jupyter_start><jupyter_text>[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ietheredge/ASAB-2019-MLWS/blob/master/Programming101.ipynb) Our Very First Program: "Hello World"<jupyter_code>print("Hello World")<jupyter_output>Hello World<jupyter_text>What happens if you try Print("Hello World")? Or print Hello WorldHow doe sthe computer tell you what has gone wrong?<jupyter_code>Print("Hello World")
print "Hello World"<jupyter_output><empty_output><jupyter_text>*Get used to seeing error messages!!!* **Variables***Numbers*:1, 3.14, 1e-3*Strings*:"fish"*Booleans*:True, False (also 1, 0)We will focus on these three but python also has three other *datatypes* (6 total):Numbers, Strings, Bools, Lists, Tuples and Dictionaries<jupyter_code>message = "Hello World"  # this is a string variable  <- this is a comment 

print(message)<jupyter_output>Hello World<jupyter_text>In Python you assign variables with "="Here, we store the information "Hello World" in a variable named "message"what we call it is mostly useful for us (but this is not always the case)There are three things to consider for variable namesValidityReadabilityConflict<jupyter_code>message0 = "hello world"
_0message = "hello world"
0message = "hello world"
message = "hello world"

m = "hello world"

# which of this is more usful to a human reader?<jupyter_output><empty_output><jupyter_text>Most scripting languages have built in functionsMake sure you do not creat a conflict with these in your variable names<jupyter_code>PRINT = print
print = 1e-4
print(message)  # oops! Don't do this!
print = PRINT # lets fix that
del PRINT
print(message)  #be careful, most editors will indicate built in functions (yellow test here)<jupyter_output>hello world<jupyter_text>Related to this: Variables can only have one value at a time<jupyter_code>output = "Hello"
output = "Goodbye"
print(output)
a_string = "What is th ultimate answer to life, the universe, and everything?"
print(a_string)
another_string = "???"
a_number = 42
print(another_string)
print(a_number)<jupyter_output>What is th ultimate answer to life, the universe, and everything?
???
42<jupyter_text>There are four types of number variables:ints, longs, floats, complexFor now only worry about:\int means integer (whole numbers)\float means floating point (decimal numbers)<jupyter_code>an_int = 42
a_float = 3.14
type(an_int)  # "type" is a built in python function to find out what datatype a variable is
type(a_float)
type(message)<jupyter_output><empty_output><jupyter_text>**Working With Variables**<jupyter_code>num_a = 21
num_b = 42

num_c = num_a + num_b  # addition
print(num_c)
print(num_a * num_b)  # multiplication
num_d = 1.5
num_c = num_b / num_d
print(num_c)  # notice the datatype of the output
a = 10
b = 42
c = 3.14
result = a * c / b  # remember your order of operations
print(result)
prefix = 'super'
suffix = 'ish'
adjective = 'careful'  #python doesn't care if you use '' or "" quotes for strings

# you can only add strings, aka "concatenation"
new_word = prefix + adjective + suffix   
print(new_word)
result = (a - b)**2 * c / b + a - c #PEMDAS 
print(result)<jupyter_output>83.41619047619048<jupyter_text>addtion: +\subtraction: -\multiplication: \* \division: / \exponent: \** \modulo: %<jupyter_code>x = 2
y = 1.5
print(x % y)  # returns the remainder<jupyter_output>0.5<jupyter_text>*more math*<jupyter_code>import math  # this is how we import a library
number = 1000
log_number = math.log10(number)  # get the log base 10 of number using a function from the math library
print(log_number)
sqrt_number = math.sqrt(number)  # take the square root
print(sqrt_number)<jupyter_output>31.622776601683793<jupyter_text>**Booleans**<jupyter_code>first_boolean = True
print(first_boolean)
is_it_cold = True
print('Is it cold today? \n' + str(is_it_cold))<jupyter_output>Is it cold today? 
True<jupyter_text>< : less than\\> : greater than\<= : less than or equal to\>= : greater than or equal to\== : equal to\!= : not equal to<jupyter_code>print(100 > 50)
print(3.14 <= 3.14)
print(100 != 23)<jupyter_output>True<jupyter_text>For strings only:\in\not in<jupyter_code>letter = "x"
string = "pixel"

print(letter in string)<jupyter_output>True<jupyter_text>logical comparisons<jupyter_code>boolean_true = True
boolean_false = False

another_boolean = True
yet_another_boolean = False

# True and False equals False
print(boolean_true and yet_another_boolean)

# True and True equals True
print(boolean_true and another_boolean)

# False and False equals False
print(boolean_false and yet_another_boolean)

# True or False equals True
# True or True equals True
# False or False equals False
print(boolean_false or boolean_true)
# not True is False so...
print(not boolean_false)
result = boolean_false or not boolean_true
print(result)<jupyter_output>False<jupyter_text>**Control Flow and Conditional Statements**<jupyter_code># if
price = 12.5
offer = 15
sell = False

if offer > price:
    print('sold!')<jupyter_output>sold!<jupyter_text>What happens if you lower the offer below the price in the above code?\if and "if" statement is false and there is no other condition it will just skip this in the program<jupyter_code># if
price = 12.5
offer = 10
sell = False

if offer > price:
    print('sold!')
else:
    print('you need to increase your offer by ' + str(price - offer) + ' more.')<jupyter_output>you need to increase your offer by 2.5 more.<jupyter_text>In this case, the else satement will execute if the 'if' statement does not. Notice there is no conditional statement after the else (no matter what else).<jupyter_code># if
price = 12.5
offer = 8
sell = False

if offer > price:
    print('sold!')
elif price - offer < 3:
    print('so close, you just need to increase your offer a bit')
else:
    print('WAY too low, get outa here!')<jupyter_output>WAY too low, get outa here!<jupyter_text>in python conditional statements always are followed by a colon and all the conditional operations which are affected are indented (4 spaces). You cannot use else or elif by itselfelif combines else and if. So it will only be considered if 'if' failsyou can have multiple elif statements **While Loops**<jupyter_code># some variables
value = 1
upper_bound = 5
# run this multiple times
if value <= upper_bound:
    print(value)
    value = value + 1
# same variables
value = 1
upper_bound = 5
while value <= upper_bound:
    print(value)
    value = value + 1<jupyter_output>1
2
3
4
5<jupyter_text>while loops are like if conditionals that you want to run for as long as that condition is trueyou need to somehow update this condition within your loop (like above)<jupyter_code># you must becareful to avoid infinite loops
print('this will run forever..')
while True:
    print('and ever')<jupyter_output>this will run forever..
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and ever
and [...]<jupyter_text>while loops are very useful and can control a lot of logic in your programlet's do this with an example of<jupyter_code>import random
#First you need to set the parameters of the gambling game

#In this game, when you win you gain a dollar
#when you loose you loose a dollar

#The probability of winning is determined by
#the variable named prob_win
#in this simulation you win with a
#probabilityof 30%
prob_win=.3

#The starting amount of money is set by the variable
#called start_money
start_money=500

#You also need away to keep track of the
#Current amount of money the user has throughout the simulation
#We’ll call this curr_money and initially it should
#be the same value as the starting money
curr_money = start_money

#The amount of money the player wants to get before stopping
goal=1000

#The simulation should run until either the player reached
#their goal or runs out of money

#These, therefore should be the conditions for the while loop
while curr_money != 0 and curr_money < goal:
    #first get a random float from0to1
    
    random_draw = random.random()
    #based on the probability of winning specified above
    #see if the random draw resulted in a win or loss for the
    #player
    if random_draw < prob_win:
        curr_money = curr_money + 1
    else:
        curr_money = curr_money - 1
    #After the while loop finishes, tell the player if they won or lost
if curr_money == 0:
    print("Sorry, you lost all of your money")
else:
    print("Congratulations! You won "+str(curr_money - start_money)+" dollars.")<jupyter_output>Sorry, you lost all of your money<jupyter_text>You've just simulated one round of gambling but to really want to simulate many rounds to get an understand of what works over many replicates **For Loops**\a special kind of while loop the variable that changes is defined when we make the loop<jupyter_code>n_bananas_to_count = 5
banana_count = 0
while banana_count < n_bananas_to_count:
    print(banana_count, 'bananas')
    banana_count = banana_count + 1
for banana_count in range(n_bananas_to_count):
    print(banana_count, 'bananas')<jupyter_output>0 bananas
1 bananas
2 bananas
3 bananas
4 bananas<jupyter_text>We save 2 lines of code! Lets run a lot of gambling simulations<jupyter_code># define how many rounds of simulations to run
num_rounds = 1000

#The amount of money the player wants to get before stopping
goal=550

#probability of winning still 30%
prob_win=.30

#starting money for each round
start_money=500

#total winnings
#we will track this during all our simulations, starting with 0
total_money = 0

#we should also keep track of how many games we have simulated, starting with 0
total_games_played = 0 

#we should also track the number games we come out ahead. starting from 0
total_wins = 0

for sim_num in range(num_rounds):
    
    # start with the same amount each time
    curr_money = start_money

    # track the number of games playe dthis round
    games_played = 0
    
    # play until we're out of money
    while curr_money > 0 and curr_money < goal:
        #first get a random float from0to1
        random_draw = random.random()
        
        #based on the probability of winning specified above
        #see if the random draw resulted in a win or loss for the
        #player
        if random_draw < prob_win:
            curr_money = curr_money + 1
        else:
            curr_money = curr_money - 1
        
        # update the number of games played
        games_played = games_played + 1
        
    #After the while loop finishes if we won
    if curr_money != 0:
        # record the win
        total_wins = total_wins + 1
        
    # record the number of games played in the simulation
    total_games_played = total_games_played + games_played

    # record the total winnings
    total_money = total_money + curr_money

prob_win = total_wins / num_rounds

average_money = total_money / num_rounds

average_games = total_games_played / num_rounds

print('A player has a ', prob_win, 'chance of hitting their goal of ',
      goal, ' dollars if they start with ', start_money, ' dollars.')

print('They play ', average_games, ' games each round on average.')

print('They win ', average_money, ' on average per round.')
# define how many rounds of simulations to run
num_rounds = 100

#The amount of money the player wants to get before stopping
goal=550

#probability of winning still 30%
prob_win=.30

#starting money for each round
start_money=500

#total winnings
#we will track this during all our simulations, starting with 0
total_money = 0

#we should also keep track of how many games we have simulated, starting with 0
total_games_played = 0 

#we should also track the number games we come out ahead. starting from 0
total_wins = 0

sims_curr_money_over_time = []

for sim_num in range(num_rounds):
    curr_money_over_time = []
    
    # start with the same amount each time
    curr_money = start_money

    # track the number of games playe dthis round
    games_played = 0
    
    # play until we're out of money
    while curr_money > 0 and curr_money < goal:
        #first get a random float from0to1
        random_draw = random.random()
        
        #based on the probability of winning specified above
        #see if the random draw resulted in a win or loss for the
        #player
        if random_draw < prob_win:
            curr_money = curr_money + 1
        else:
            curr_money = curr_money - 1
        
        # update the number of games played
        games_played = games_played + 1
        
        curr_money_over_time.append(curr_money)

    sims_curr_money_over_time.append(curr_money_over_time)
        
    #After the while loop finishes if we won
    if curr_money != 0:
        # record the win
        total_wins = total_wins + 1
        
    # record the number of games played in the simulation
    total_games_played = total_games_played + games_played

    # record the total winnings
    total_money = total_money + curr_money

prob_win = total_wins / num_rounds

average_money = total_money / num_rounds

average_games = total_games_played / num_rounds

print('A player has a ', prob_win, 'chance of hitting their goal of ',
      goal, ' dollars if they start with ', start_money, ' dollars.')

print('They play ', average_games, ' games each round on average.')

print('They win ', average_money, ' on average per round.')
import matplotlib.pyplot as plt

for sim in sims_curr_money_over_time:
    plt.plot(sim)
plt.show()
# define how many rounds of simulations to run
num_rounds = 100

#The amount of money the player wants to get before stopping
goal=550

#probability of winning still 30%
prob_win=.30

#starting money for each round
start_money=500

#total winnings
#we will track this during all our simulations, starting with 0
total_money = 0

#we should also keep track of how many games we have simulated, starting with 0
total_games_played = 0 

#we should also track the number games we come out ahead. starting from 0
total_wins = 0

sims_curr_money_over_time = []

n_games = 5

for sim_num in range(num_rounds):
    curr_money_over_time = []
    
    # start with the same amount each time
    curr_money = start_money

    # track the number of games playe dthis round
    games_played = 0
    
    # play until we're out of money
    for game in range(n_games):
        curr_money_over_time.append(curr_money)
        sims_curr_money_over_time.append(curr_money_over_time)
        #first get a random float from0to1
        random_draw = random.random()
        
        #based on the probability of winning specified above
        #see if the random draw resulted in a win or loss for the
        #player
        if random_draw < prob_win:
            curr_money = curr_money + 1
        else:
            curr_money = curr_money - 1
        
        # update the number of games played
        games_played = games_played + 1
        
    #After the while loop finishes if we won
    if curr_money != 0:
        # record the win
        total_wins = total_wins + 1
        
    # record the number of games played in the simulation
    total_games_played = total_games_played + games_played

    # record the total winnings
    total_money = total_money + curr_money

prob_win = total_wins / num_rounds

average_money = total_money / num_rounds

average_games = total_games_played / num_rounds

print('A player has a ', prob_win, 'chance of hitting their goal of ',
      goal, ' dollars if they start with ', start_money, ' dollars.')

print('They play ', average_games, ' games each round on average.')

print('They win ', average_money, ' on average per round.')
import matplotlib.pyplot as plt

for sim in sims_curr_money_over_time:
    plt.plot(sim)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06810272410896435, 'fraction_numerical': 0.014700588023520941, 'mean_word_length': 3.8919870854123864, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 79, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '369166', 'license': 'MIT', 'n_tokens_mistral': 5488, 'n_tokens_neox': 5086, 'n_words': 2088, 'path': 'Python101.ipynb', 'repo_name': 'Lukas-Koch/ASAB-2019-MLWS', 'size': '16666'}