Chapter 10 - Let’s write some Code #
Starting Minimal #
Code is mostly math #
Heres the thing: Code is mostly math. If you’re anything like me, you probably just went ugggggghhhhhhh.
And I get it. But it’s not that bad for two reasons:
- The math isn’t like what you’re used to.
- The math is actually doing something. You’re not just getting a number to solve a problem, you’re making a tool that can solve many problems or watching the math cause something to happen on screen.
On point 1., it’s usally because code has lots of flow control and logic that you’re probably not used to seeing in math.
Things like
fake code for beginners:
|
|
or
|
|
slightly less fake code for people that have seen this before:
|
|
or
|
|
But, what about point 2.? Take a look at this:
This is tixy.land – Click on the moving dots to advanced though a lil' tutorial and some examples
Here in tixy land, you can try out some code. I don’t want to lead you to anything. Just, type something. See what happens.
Even if you don’t understand all the math or what the symbols mean I’m sure you can get a rough idea of what’s going on.
To give a quick run down of some operations you might want to use:
[TODO]
You can usually make something cool by just entering a bunch of trig and making complex functions you don’t truely understand. You’re learning, that’s fine, no shame. Here, for example, is one I made by wandering around trig functions asin((t/9*(i/32*sin(t/4))*y)%5)
https://hexy.now.sh is inspired by tixy but has some extra interesting features, if you’re having fun with tixy.land it’s worth checking out.
Python #

[TODO] Why start with python
The building blocks of programming #
All programming is just chaining logic, for example using treatments like “if a is b, then make c hold the value of a-3” followed by “if c is negitive then say hello”
Well, that’s actually an easy program
|
|
you should notice a few weird things here though, namely, on line 5 we used two equal signs to check equality. This is the case in most programming languages because a single equal sign, like is used on the first two lines, is used for assignment that is assigning the value of a
to 1 and b
to 1 or as on line 6 assigning the value of ( a - 3 )
to c
, because we’ve already used a single equal sign for assignment, two equal signs is used for checking that two things are actual equal. That is, one equal sign sets things to be equal, two checks that they are equal.
If you follow the logic here, a
and b
are both 1, so line 5 checks that to be true, so line 6 does happen. Now ‘c’ which previously held the value ‘1’ is now equal to ‘a-3’, where ‘a’ is ‘1’, so ‘c’ becomes ‘-2’. on line 8 we check if ‘c’ is less than ‘0’, and because it is we print the word ‘hello’. Here, you’ll notice ‘hello’ is in quotes. This is because if it were not, that is if it were print(hello)
it would try to print the value of the variable ‘hello’ which since hello isn’t defined, wouldn’t work. Let’s try this in the Python interpreter. You can find install instructions for your operating system here: https://www.python.org/downloads/. If you’re on Linux simply type python
into the terminal.
With the Python interpreter open try without quotes:
|
|
As you can see Python failed to print hello because the variable was not defined. Now lets try assigning a value to the hello variable before printing:
|
|
Anything you write to the interpreter can also be added to a file and ran that way. Try saving the examples above to a file called test.py, opening a terminal and running it with python test.py
.
Conditions using if
, elif
and else
#
You often want code only to be executed, if a certain condition is met. For example, take a look at the following code:
|
|
Using the if
statement, connected with a logical condition, you can control the program flow. In this case, a
is smaller than b
, so python will skip the code inside of the if
case and only execute what is within the else
part.
You can also combine multiple if cases like this:
|
|
Using the elif
statement, you can check for another condition if the one in the if
statement was false
.
Loops #
Some lines of codes have to be repeated very often. Instead of typing the same line over and over again, you can use loops.
There are two types of loops: The for
and the while
loop. While they both basically do the same thing, sometimes it is more intuitive to use one over the other.
TODO: Again on 0-based vs. 1-based indexing
The for
loop
#
The for
loop iterates over a list of items and stores the current element in a variable.
This is especially useful when working with lists, for example.
|
|
If you are familiar with loops in other programming languages (such as Java or C), you’re probably thinking: “That’s not a for loop, that’s a for-each!”. This is true, in python all for loops are for-each loops. If you want to iterate over a range of numbers, use pythons range()
function:
|
|
The while
loop
#
The for
loop is very useful, but sometimes, using the while
loop is easier and more straightforward.
For example, if you want to repeat something until a certain condition is met, using this loop is simple:
|
|
You can also combine the condition after the while
keyword with an else
statement:
|
|
[TODO] assignment, comparison, combined ops (+=), mod, exponent, floor, in/not in, is/is not, data types, functions libraries
Common methods used #
commenting-out print debugging Recursion, object orientation while(true) try/catch
Some simple programs #
Hello World #
As seen in the example above, the following will print Hello World!
:
print('Hello World!')
Lets get some input aswell:
|
|
Even or Odd #
Below is an example of a Python script which checks if the input number is odd or even.
|
|
Prime Numbers #
Prime number is a number greater than 1 whose only factors are 1 and itself. Few instances of prime numbers include 2, 3, 5, 11. Below is an example of python function which defines whether the given input number is prime or not.
|
|
Calculating sine and pi #
Sorting a list #
Palindromes #
Cypher #
Making a full project #
Part 0, Git #
Before we get started, I want to be sure you took the time to read the section on Git in Chapter 6.1 above. If you need to refer to a cheat sheet you can look in Apendix B.
Part 1, Starting easy #
[TODO] guide to write an OO python program with libraries to read and write entries to a database-like file and a real database the database should have 3 tables, a to-do list, a time-tracker list, and a list of articles
To-do: NAME, DUEDATE time-tracker: 2types, either manually enterend with NAME, TSTART, TSTOP or NAME, TTOTAL for the following program articles: NAME, PUBLISHED, TPUB, TEDIT
we’ll need some way to track what items in the time tracker were started, but never stopped
for now, everything should be strictly command line arguments and print statments
More advanced topics #
[TODO] Move this section
Lambdas #
A lambda expression is used to create a a small anonymous function. It can take any number of arguments, but can only have one expression. Below is an example of a lambda expression which can be used to compute cube of any given number.
|
|
And here is the other example of a lambda expression with three arguments.
|
|
threads/ parrallelization(high level only), interupts (actually, let’s save this until embbeded), apis,
A little more practice #
Weird projects
Easing into Graphical Programming with Creative Coding #
Coding Adventure: Ray Marching (YouTube)