Skip to main content

Section B.2 Python basics

OK, so you’ve logged into Syzygy and you’re ready to write some code. What does basic code look like in Python? The good news is that you don’t need to be a programmer to do linear algebra in Python. Python includes many different libraries that keep most of the code under the hood, so all you have to remember is what command you need to use to accomplish a task. That said, it won’t hurt to learn a little bit of basic coding.
Basic arithmetic operations are understood, and you can simply type them in. Hit shift+enter in a code cell to execute the code and see the result.
OK, great. But sometimes we want to do calculations with more than one step. For that, we can assign variables.
Sometimes you might need input that’s a string, rather than a number. We can do that, too.
Another basic construction is a list. Getting the hang of lists is useful, because in a sense, matrices are just really fancy lists.
Once you have an empty list, you might want to add something to it. This can be done with the append command.
Go back and re-run the above code cell two or three more times. What happens? Probably you can guess what the len command is for. Now let’s get really carried away and do some “for real” coding, like loops!
Notice the indentation in the second line. This is how Python handles things like for loops, with indentation rather than bracketing. We could say more about lists but perhaps it’s time to talk about matrices. For further reading, you can start here
 1 
developers.google.com/edu/python/lists
.
You have attempted of activities on this page.