Setting a variable in python just means that a number or word will be named so that it can be used later. Whatever is written before the equal sign will become the name of the variable. In the code to the left, the number 4 has been named 'Number' and can be called later in the code. It is important to remember that variable names are case sensitive, so while the variable 'Number' is equal to 4, the variable 'number' is equal to 500. Small changes in wording can disrupt a full program. It is also important to keep names short and easy to remember. While 'How_old_I_am' and 'HowOldAmI' are both fine variable names, they can be made shorter so they are less likely to mistype by using the variable name 'Age' instead.
Print commands help take information that is needed and prints it on the screen. This can be done by typing "print()". Whatever is inside the parenthesis of the print command will be displayed on the screen. There are a few rules with the print command. The first is if you want to print a word, you need to put apostrophes around it like in the example 'Hello World'. The second is that if you do not put apostrophes around the word, it will look for the variable of the word you put. On line 6, the line of code says "print(x)". Because there are no apostrophes around x, the code finds the variable "x = 42" and prints 42. The last rule is that both words and variables can be in the same print line, but they must be separated by commas this can be seen in the last example "print('I am', Age, 'years old')"
While loops are the most important command that will be used in python. While loops will allow us to make objects move and create physics simulations. While loops read the condition that is set and "while" that condition is true, it will do everything in the code below (everything indented is in the while loop). When the condition is not true anymore it will stop.
In the example to the left, the condition we wrote is while t (time) is less than 5 (seconds), then move the marble using the equation d = v*t. The position of this object is called marble.pos, so we write marble.pos instead of d. The end of a while loop must always have code similar to "t = t + deltat". This increases the time after each time the while loop runs and makes sure that the while loop does not run forever. On line 6 of the code "t" was set equal to zero. After going through the while loop once, "t" will go through line 15 which says that t must increase by deltat which is 0.1. After line 15, "t" is no longer 0 it is now 0.1. When "t" goes through the while loop the second time, it is increased by deltat again so it is now 0.2. It will continue to go through the while loop until it is 5 and then it will stop because the condition set on line 5 of "t < 5" is not true anymore.
While loops for most of the simulations that we will be doing will need a rate() command in the loop. Computers run programs incredibly fast, and the rate command will slow down the simulation so we can see what is happening. Most of the time we will use a rate(100) command at the beginning of the while loop.
If statements are similar to while loops, but they will only run once. If statements read the condition and will run the code "if" the condition is true. If the condition is not true it will not run the code below.
In the first example (lines 3-7), the if statement says if a is equal to (two equal signs means equals to) b, then print "a is equal to b". Because a and b are both equal to 12, the code prints "a is equal to b".
In the second example (lines 9-13), the if statement says if c is greater than or equal to (>= means greater than or equal to) d, then print "c is greater than or equal to d". Because c, which is 10, is not greater than d, which is 12, the code is not printed.
If else statements are a more powerful if statement. They add a line of code that is read if the if statement is not true.
In the first example (lines 3-9), the if statement says if a is equal to b, then print "a is equal to b". The else statement says that if a is not equal to b, then print "a is not equal to b". Because a and b are both equal to 12, the code prints "a is equal to b" from the if statement.
In the second example (lines 11-17), the if statement says if c is greater than or equal to d, then print "c is greater than or equal to d". The else statement means that if c is not greater than or equal to d, then print "d is greater than c". Because c, which is 10, is not greater than d, which is 12, the code from the if statement is skipped and the else statement is printed so it says "d is greater than c".
Python Conditionals can be used in while loops, if statements, or if-else statements to execute the lines of code that we need the computer to read. A list of the conditionals can be seen here. The conditionals can be equal (==), not equals (!=), less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=).
Break statements will stop the code completely and break out of a loop. These are especially useful to stop anything that is not supposed to happen in the code. They are almost always used with if statements.
In the example, the while loop will print x for every number 0-100, unless x is equal to 13. If x is equal to 13, the loop will break and will stop. If you hit play on this code, you should see a list of numbers 0-12 with it stopping at 13 because of the break command.
Dot notation is a way to access specific parts of an object. It is usually written as the name of the object, then a ".", and then the attribute of the object that you want to call.
In the example, a red sphere named Sphere1 is created at the position (3,1,0) with a radius of 2.
The print command on line 5 prints "The position of Sphere1 is:" and then runs the dot command Sphere1.pos. The code will look for Sphere1 on line 3 and then find pos and will print the position (3,1,0), so the full statement is "The position of Sphere1 is: (3,1,0)
The dot notations can get even more complex and allow you to call a specific coordinate of that object. On line 7, the print command will print "The x-coordinate of Sphere1 is:" and then runs the dot command Sphere1.pos.x. This code doesn't just look for Sphere1's position, but it looks for the x-coordinate of this position. Once it finds the x-coordinate of Sphere1's pos on line 3, it prints it so the full statement is "The x-coordinate of Sphere1 is: 3".
Vectors are arrows that represent information. Vectors have 3 dimensions (we will only talk about 2 dimensions in our class). They have a x-component (left and right), a y-component (up and down) and a z-component (closer and farther). Remember right and up are positive and left and down are negative.
They are written in python with a "vec" to represent that it is a vector followed by (x,y,z) representing the x, y, and z-components of the vector. The z-component will be 0 in most cases for this class.
In the example code, you should see a Sphere1 uses vector notation to describe it's position. The position of Sphere one is the vector (3,1,0)