This is an old revision of the document!
Common Commands and Tips for Python
In this class, we are often going to use VPython to create computational models, which will serve as a powerful tool to help us create visualizations and apply the ideas in this course to more real-world contexts. Below are some of the common Python commands that we will use in this course and some coding tips compiled by previous EMP-Cubed students. (Note: we do not expect you to have any coding experience prior to this course, and we will not expect you to write a program from scratch. We will primarily be asking you to interpret chunks of code with your group members and adjust/modify pieces of code that you will be given.)
Common Commands
- Exponents
- Objects (box, sphere, arrow)
- Object characteristics (pos, length, width, radius, color, etc)
- Vectors - vec()
- Magnitude - mag()
- Print - print()
- Dot Product - dot()
- Cross Product - cross()
Tips for Coding
- Check your parentheses - the computer will very strictly follow the order of operations (PEMDAS - Parentheses Exponents Multiplication Division Addition Subtraction). When writing out equations, make sure you have parentheses in the correct places. For example in the code below, the computer would say A = 6 but B = 4 - so these are not the same thing! Parentheses errors can be especially tricky because the program will likely run perfectly fine. Also, make sure that each of your parentheses match up – in other words every bracket has to have a buddy.
A=4+4/2 B=(4+4)/2
- Watch upper/lower cases - coding is case sensitive - meaning that lowercase and uppercase letters mean something different in coding. If I had two different variables named Num_1 and num_1, Python would treat these as two distinct objects. If you initially set your variable name to be Num_1 but wrote num_1 throughout the rest of your code, Python would not know what num_1 was. Just be conscious that when you are naming or using different elements in your code, that you are writing it the same way every time.
- Watch whitespace
- Comments