The following is part of a on-going collection of Jupyter notebooks. The goal being to have a library of notebooks as an introduction to Mathematics and technology. These were all created by Gavin Waters. If you use these notebooks, be nice and throw up a credit somewhere on your page.
This one the most common conditional statements in all of programing, it simply sets up a boolean test and then if it passes does something. The structure in python is very easy.
if boolean-test :
do this stuff
more stuff
even more stuff
last bit of stuff
after the stuff is done the program continues
if 4>=3 :
print('yowzaa')
yowzaa
if 4<=3 :
print('yowzaa')
if 3!= 4:
print('Handy != not equal to')
Handy != not equal to
You can also attach an else condition to the if statement.
if 4>=3:
print('blue')
else:
print("red")
blue
if 4<=3:
print('blue')
else:
print("red")
red
There is also elif conditions that can placed in sequence, first the if then the next elif condition etc etc until some boolean condition becomes true then it exits the statement.
if 4<=3:
print('blue')
elif 5<=3:
print("red")
elif 3<=3:
print("yellow")
elif 2<=3:
print("black")
else:
print('white')
yellow
if 4<=3:
print('blue')
elif 5<=3:
print("red")
elif 6<=3:
print("yellow")
elif 2>=3:
print("black")
else:
print('white')
white
Neat little things used for repedative tasks. You should have a parameter that you are moving through a list that is set up.
Like the if statement in that you have the for then a boolean with " : "
for a in range(0,4):
print(a)
0 1 2 3
for a in range(0,4):
if a>=3:
print(a)
3
Similar to the for loop.
a=0
while a<=4:
print(a)
a+=1 # do not forget to increment a, or it will forever print out a
0 1 2 3 4
a=0
while a<=7:
if 6<=a:
print('blue ' + str(a))
elif 5<=a:
print('red ' + str(a))
elif 4<=a:
print('yellow ' + str(a))
elif 2>=a:
print('black ' + str(a))
else:
print('white ' + str(a))
a+=1
black 0 black 1 black 2 white 3 yellow 4 red 5 blue 6 blue 7
a =0
while a<=5:
print('Before the increment:' + str(a))
a+=1
print('After increment : ' + str(a))
Before the increment:0 After increment : 1 Before the increment:1 After increment : 2 Before the increment:2 After increment : 3 Before the increment:3 After increment : 4 Before the increment:4 After increment : 5 Before the increment:5 After increment : 6
Notice that the full while statement is carried out and then the while conditional is checked.
Consider the following example, $f(x)=x^2+6x-16=(x+8)(x-2)$. We know there is some roots somewhere between -20 and 20. Lets see if there is an integer root.
a=-20
rootfound = False
while rootfound==False:
if a**2+6*a-16==0:
print('This is a root: '+ str(a))
rootfound==True
a+=1
if a>=20:
break
This is a root: -8 This is a root: 2
Lets assume that $f(x)=42x^2-17x-15$. Can we set up a script to guess where there root is?
while True:
a=input("enter your guess? ")
a=float(a)
if (42*a*a-17*a-15)*(42*a*a-17*a-15)<=0.005:
print('close enough!!!!')
break
elif 42*a*a-17*a-15>=0:
print('positive')
else:
print('negative')
enter your guess? 20 positive enter your guess? 0 negative enter your guess? 10 positive enter your guess? 5 positive enter your guess? 2 positive enter your guess? 1 positive enter your guess? 0.5 negative enter your guess? 0.7 negative enter your guess? 0.9 positive enter your guess? 0.8 negative enter your guess? 0.830 negative enter your guess? 0.84 positive enter your guess? 0.835 positive enter your guess? 0.8325 close enough!!!!