<jupyter_start><jupyter_text>COURSE: Master math by coding in Python SECTION: Arithmetic https://www.udemy.com/course/math-with-python/?couponCode=MXC-DISC4ALL INSTRUCTOR: sincxpress.comNote about this code: Each video in this section of the course corresponds to a section of code below. Please note that this code roughly matches the code shown in the live recording, but is not exactly the same -- the variable names, order of lines, and parameters may be slightly different.   VIDEO: Addition, subtraction, multiplication, division  Addition<jupyter_code># use Python like a calculator
4+5 # press Ctrl-Enter to find the answer!
# can use multiple numbers at the same time:
4 + 3/4 + 9.4<jupyter_output><empty_output><jupyter_text>Subtraction<jupyter_code># exactly how you'd think it should work!
4 - 3 - 1<jupyter_output><empty_output><jupyter_text>Multiplication<jupyter_code># Use * to indicate multiplication (not x!)
3*4 
# can mix operations
3*4 - 3*2<jupyter_output><empty_output><jupyter_text>Division<jupyter_code># use /
3/4
# error! Always use the forward slash
3\4<jupyter_output><empty_output><jupyter_text>Exercises!<jupyter_code>#1)
5 - (2/4) * (3/5)
#2)
(4-5) / 3+5*6<jupyter_output><empty_output><jupyter_text>VIDEO: Using variables in place of numbers<jupyter_code># create a variable by assigning a number to it
x = 17
# then use that variable in place of a number
x - 2
# you can re-assign variables whenever you need to
x = 10
# note that nothing prints out to the screen here. 
# We didn't ask Python to compute anything, just to set a variable.
# you can even use a variable to reassign itself!
x = x-2*5
x
# multiple variables can be really useful
x = 10
y = 20

x + y*z # can't use a variable before it's created!
z = 30
x + y*z
# only the final line of code will print out an answer;
# otherwise, you can use the print() function

40+10

50+3

print(40+3)
print(10+6)<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># 1)
x = 7
y = -2
z = 5

# 1)
3*x*(4+y)
# 2)
-y - (x+3)/z<jupyter_output><empty_output><jupyter_text>VIDEO: Printing out equations in IPython<jupyter_code># the line below will import special functions that are not otherwise available in the main Python environment
from IPython.display import display, Math

display('4 + 3 = 7')

display('4 + 3 = ' + str(4+3))

# can also use string variables for printing

expr = '4+5-9 = 0'

display(expr)
display(Math(expr)) # note that this line produces slightly nicer output
# mixing variable types in outputs
x = 4
y = 5

display(Math( str(x) + ' + ' + str(y) + ' = ' + str(x+y) )) # note: plus sign here is for concatenating strings, not for summing!
# without using latex
display(Math('4/5 = .8'))

# with latex (much nicer!)
display(Math('\\frac{4}{5} = ' + str(4/5)))

# alternative method for integrating numbers with string text
display(Math('\\frac{4}{5} = %g' %(4/5)))

# Note: We'll be using the basics of substitution (%g and %s) in this course; for more details and formatting options, see
# https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

# more variables in the same line

x = 3.4
y = 17

display(Math('%g \\times %g = %g' %(x,y,x*y)))<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code>x = 7
y = -2
z = 5

# 1)
ans = 3*x*(4+y)
display(Math('3x(4+y) = ' + str(ans)))
display(Math('3\\times%g(4+%g) = %g' %(x,y,ans) ))


# 2)
ans = -y - (x+3)/z
display(Math('-y - \\frac{x+3}{z} = %g'%ans))

display(Math('-%g - \\frac{%g+3}{%g} = %g'%(y,x,z,ans)))<jupyter_output><empty_output><jupyter_text>VIDEO: Exponents (powers)<jupyter_code># use **
3**2
# careful not to confuse with ^
3^1
# this is actually the symbol for the XOR logical problem
# variables can also be useful
x = 2
3**x
# Note about importing functions or modules: You need to do this only once per session.
# Thus, you don't need the following line if you've already imported these functions.
# On the other hand, it doesn't hurt to import multiple times ;)
from IPython.display import display, Math


# using the print function
print( 2**x )

# print out the whole equation
display(Math('x^%g = %g'%(x,2**x)))
# the law of exponents
print(3**3)
print(3*3*3)
print( 3**2 * 3**4 )
print( 3**(2+4) )
# square root as fractional power
x**(1/2)<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code>x = 5
y = 5.1

# 1)
ans = x**(3/4) * 4**y
display(Math('x^{3/4}\\times 4^y = %g'%ans))

# 2)
solution = (3**3) / (x**y)
display(Math('\\frac{3^3}{x^y} = %g'%solution))

# 3)
ans = 10**(x-4)
display(Math('10^{x-4} = %g'%ans))<jupyter_output><empty_output><jupyter_text>VIDEO: Using for-loops to compute powers<jupyter_code># basic for loop construction

for i in [0,1,2,3]:
    print(i)

# using the range function
list( range(0,3) )
for i in range(0,3):
    print('This is the %gth element.'%i)
for i in range(0,5):
    num = i**2
    print(num)<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># write code to print the following:
# 2 to the power of 0 is 1
# and so on for 2**0 through 2**9

base = 2

for i in range(0,10):
    print('%g to the power of %g is %g'%(base,i,base**i))<jupyter_output><empty_output><jupyter_text>VIDEO: Order of operations  Reminder about order of operations:PEMDAS:    Parentheses    Exponents    Multiplication    Division    Addition    Subtraction    (Please excuse my dear aunt Sally)<jupyter_code># examples from slides

print( 4 + 3 * 2 - 1 ) # spacing has no effect

# will this be 24 or 10?
4 + 2 * 3
# how about this one? can you make the be 7 or -11?
2 - 4 + 9<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># add parentheses as needed to make the following equations true

# 1) 
print( 4*5/(7+3) )

# 2)
print( 9/(3+6)-1 )<jupyter_output><empty_output><jupyter_text>VIDEO: Testing inequalities and Boolean data type<jupyter_code># The result of the below expression can only be True or False
4>5
print(5<5.1)
b = 10 > 3*3.33334

print(b)
print(type(b)) # the variable class is boolean
4 >= 2+2
5 <= 3*2-1.00000000001
# testing for equalities requires two equals signs

# 4 = 4
4 == 4

test = 4 == 5
print(test)<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># which variable value(s) will make the following inequalities true?
# comment out the incorrect answers!
x = 2
x = 3
x = 4

# 1)
print( 4*x+3 < 17-x**2 )

#2) 
print( 8*x - 2 <= -3*x + 42 )<jupyter_output><empty_output><jupyter_text>VIDEO: If-statements and logical operators<jupyter_code># start with basics:
if 4+2 == 6:
    print('True!')
else:
    print('Nope!')
# using variables

x1 = 3
x2 = 4

if x1>x2:
    print('%g is greater than %g' %(x1,x2))
elif x1<x2:
    print('%g is greater than %g' %(x2,x1))
else:
    print('%g is equal to %g' %(x1,x2))

# with logical operators

x = 1
y = 2
z = 3

if x+y<z or x-z>y:
    print('Yeah, the first thing.')
elif 2*x+y>z and 2*z-x>y:
    print('the second thing')<jupyter_output><empty_output><jupyter_text>Exercise<jupyter_code>from IPython.display import display, Math

for i in range(0,4):
    for j in range(0,5):
        if j>0 and i>0:
            display(Math('%g^{-%g} = %g' %(i,j,i**-j)))<jupyter_output><empty_output><jupyter_text>VIDEO: Absolute value<jupyter_code>a = -4
abs(a)
b = abs(a)
a,b
# printing out with latex
x = -4.3

# If the following line crashes, you need to import the display and Math functions as shown earlier
display(Math('|%g| = %g' %(x,abs(x)) ))<jupyter_output><empty_output><jupyter_text>Exercise<jupyter_code># report the absolute value of a set of numbers only if they are less than -5 or greater than +2
numbers = [-4,6,-1,43,-18,2,0]

# for-loop
for i in numbers:
    if i<-5 or i>2:
        print( 'Absolute value of %g is %g.' %(i,abs(i)) )
    else:
        print( str(i) + ' was not tested.' )<jupyter_output><empty_output><jupyter_text>VIDEO: Remainder after division (modulus)<jupyter_code># pick some numbers
a = 10
b = 3

# division
a/b
# integer division
int(a/b)
# with a remainder!
a%b
# now for a nicer printing
# set variables for outputs
divis = int(a/b)
remainder = a%b

print("%g goes into %g, %g times with a remainder of %g." %(b,a,divis,remainder))<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># determining whether a number is odd or even

nums = range(-5,6)

for i in nums:
    
    # check for first character spacing
    firstchar = '' if i<0 else ' '
    
    # test and report
    if i%2 == 0:
        print('%s%g is an even number' %(firstchar,i))
    else:
        print('%s%g is an odd  number' %(firstchar,i))<jupyter_output><empty_output><jupyter_text>VIDEO: Create interactive math functions<jupyter_code># create a function that takes two numbers and reports the numbers, their integer division, and remainder

# define the function
def computeremainder(x,y):
    division  = int( x/y )
    remainder = x%y
    
    print("%g goes into %g, %g times with a remainder of %g." %(y,x,division,remainder))
computeremainder(10,3)
# create a function that inputs two numbers and reports the numbers, their integer division, and remainder

# define the function
def computeremainder():
    x = int( input('Input numerator: ') )
    y = int( input('Input denominator: ') )
    division  = int( x/y )
    remainder = x%y
    
    print("%g goes into %g, %g times with a remainder of %g." %(y,x,division,remainder))
# now test the function!
computeremainder()
# create a function that takes two numbers as input, 
# and returns the larger-magnitude number to the power of the smaller-magnitude number

def powerfun():
    n1 = int( input('Input one number ') )
    n2 = int( input('Input a second number ') )
    
    # test
    if abs(n1)>abs(n2):
        return n1,n2,n1**n2
    else:
        return n2,n1,n2**n1

# now test the function
first,second,outcome = powerfun()

display(Math('%g^{%g} = %g '%(first,second,outcome)))<jupyter_output><empty_output><jupyter_text>Exercises<jupyter_code># input three numbers from user, x, y, and a switch option
#    Create two functions that will compute x**y and x/y
#    call the appropriate function and print the result

from IPython.display import display,Math

# create functions
def powers(x,y):
    display(Math('%g^{%g} = %g' %(x,y,x**y)))

def division(x,y):
    display(Math('\\frac{%g}{%g} = %g' %(x,y,x/y)))
    
def mainfunction():
    x = int(input('input X: '))
    y = int(input('input Y: '))
    display(Math('Press "1" to compute %g^{%g}\\text{ or "2" to compute }\\frac{%g}{%g}' %(x,y,x,y)))
    which = int(input(' '))
    
    if which==1:
        powers(x,y)
    elif which==2:
        division(x,y)
    else:
        print('Invalid selection!')

# run the main function

mainfunction()

float(input(' '))<jupyter_output><empty_output><jupyter_text>VIDEO: Interactive functions: Guess the number!<jupyter_code># guess a number!
# write code to generate a random integer and then input user guesses of that integer. 
# The code should tell the user to guess higher or lower until they get the right answer.

from numpy import random

def guessTheNumber():
    num2guess = random.randint(1,101)
    
    userguess = int( input('Guess a number between 1 and 100  ') )
    while userguess!=num2guess:
        if userguess>num2guess:
            print('Guess lower!')
        elif userguess<num2guess:
            print('Guess higher!')
        
        userguess = int( input('Guess again ') )
    
    print('Got it! The right number was %g and your final guess was %g'%(num2guess,userguess))

guessTheNumber()<jupyter_output><empty_output><jupyter_text>VIDEO: ARITHMETIC BUG HUNT!!! Note about this code: The code below shows the SOLUTIONS! You should first go through the file called "MXC_pymath_arithmetic_bughunt.ipynb." Then you can check your answers here!<jupyter_code>x = '1'
y = '2'

print( int(x) + int(y) )
display(Math('\\frac{1}{2}'))
x = 4
y = 5
display(Math('%g + %g = %g'%(x,y,x+y)))
display(Math('\\frac{%g}{%g} = %g'%(x,y,x/y)))
3**2 # should equal 9
for i in range(0,3):
    display(Math('%g\\times2 = %g'%(i,i*2)))
a = 10
b = 20

result = 2*a <= b

print(result)
if a+b*2 > 40:
    print(str(a+b*2) + ' is greater than ' + str(40) + '. Time to celebrate!')

var = input('input a number!')
print(var)

# division
4/10
# sum two numbers
w = float( input('Input a number: ') )
z = float( input('Input another number: ') )

print('The sum of %s and %s is %s' %(w,z,w+z))

display(Math('\\frac{9}{3} = ' + str(9/3)))
display(Math('\\frac{9}{3} = ' + str(9/3)))
# print the numbers 1 through 10
t = 1
while t<11:
    print(t)
    t=t+1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1424478752188445, 'fraction_numerical': 0.030479070507719242, 'mean_word_length': 4.218853820598007, 'pattern_counts': {'":': 0, '<': 120, '<?xml version=': 0, '>': 122, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '341532', 'license': 'MIT', 'n_tokens_mistral': 4870, 'n_tokens_neox': 4648, 'n_words': 1444, 'path': 'MXC_pymath_arithmetic.ipynb', 'repo_name': 'stefan-cross/mathematics-with-python', 'size': '12566'}