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.
The input of Jupyter Notebooks can be activated by pressing shift+Enter at the same time.
To get to a new line in a cell you just can press enter
print('Hello World')
Hello World
You can assign the test a variable name, like Bob
Bob ="Hello World"
This is stored in the memory of the Kernel that this notebook is running on. Hence anytime you call on Bob the kernel will produce the string "Hello World"
Bob
'Hello World'
Notice that computers are precise, and they like it of you could try to be also.
bob
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-e01096b9ffe3> in <module>() ----> 1 bob NameError: name 'bob' is not defined
Luckily, notebooks will give a small bit of feedback on why you seem to be nonchalant about your coding.
print(Bob)
Hello World
Numbers in python are not variable names.
3
3
print(3)
3
print(3+2)
5
3+2
5
We can "concatenate" strings in python easily.
Jane = ', Goodbye cruel world'
print(Bob+Jane)
Hello World, Goodbye cruel world
print(Bob + Jane +" and other random stuff")
Hello World, Goodbye cruel world and other random stuff
The notebooks compute the cell based upon the knowledge the kernel has up to that point. If we were to restart the kernel, or shutdown this kernel and start anew, then all previous variables would be lost.
We know that our Bob variable is just a string. How long is it? can we slice and dice it?
Bob[0]
'H'
Bob[1]
'e'
Bob[2:]
'llo World'
Bob[:4]
'Hell'
In notebooks the cell only outputs the last thing it is told to do. That is unless it is specifically told to output information.
Bob[2]
Bob[6]
'W'
print(Bob[2])
print(Bob[6])
l W
Notice that the print function does not output the "" quotes. because we are not passing through the string as a variable
Fred = Bob[2:7]
Fred
'llo W'
We can even figure out how long the string is.
len(Fred)
5
print(len(Fred))
print(len(Bob))
5 11
for count in range(len(Bob)):
print(Bob[count])
H e l l o W o r l d
Simple yet the bane of alot programers. Do not get lost in a for loop. Make sure there is an end, omake sure that you are not exponentially generating computations.
The format is simple,
for parameter in set :
do some stuff that is indented
Stuff ends when the indent ends.
Fancier loops are only limited by your imagination.
for blah in range(len(Bob)+1):
print(Bob[:blah])
H He Hel Hell Hello Hello Hello W Hello Wo Hello Wor Hello Worl Hello World
Blank_space =" "
for blah in range(len(Bob)+1):
print(Bob[:blah] +" " + Bob[len(Bob)-blah:])
H d He ld Hel rld Hell orld Hello World Hello World Hello W o World Hello Wo lo World Hello Wor llo World Hello Worl ello World Hello World Hello World
for blah in range(len(Bob)+1):
print(Bob[:blah] +Blank_space[:2*(len(Bob)-blah)+1] + Bob[len(Bob)-blah:])
H d He ld Hel rld Hell orld Hello World Hello World Hello W o World Hello Wo lo World Hello Wor llo World Hello Worl ello World Hello World Hello World