184_notes:python_syntax

This is an old revision of the document!


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.)

  • Exponents - to write an exponent in Python you have to use a “$**$” rather than “^”. For example, to write $A=5.43*10^4$ in code you would write:
A=5.42*10**4
  • Objects (box, sphere, arrow)
    • Object characteristics (pos, length, width, radius, color, etc)
  • Vectors - vec()
  • Magnitude - mag()
  • Print - print()
  • Dot Product - dot()
  • Cross Product - cross()
  • 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.
  • Comments - comments are informational statements that are not read or used by the program, but help you when analyzing the code. In Python, you can turn any line or part of a line in the code into a comment by putting a “#” or “##” in that line (see the examples below). You can use comments in several different ways:
    • Notating units - especially in long equations or calculations, keeping track of the units can help you evaluate whether your solution or equation is right. After any variable, you can add the units of that variable with a comment (see the example below).
    • Denoting what specific lines of code do - before a chunk of code it's often helpful to write out what the purpose of those lines of code are in plain English. This is especially helpful when trying to use code that is quite long and complicated or if you’re trying to share code with a team member. This can also be helpful when working with others communicate any misunderstanding of the code or suggest potential changes.
    • Fixing errors in your code - if you are not sure which line is giving you trouble, instead of deleting the entire line, just comment it out. This way if you find that that line was not the problem later, you don’t have to rewrite it, you just have to uncomment it (uncommenting is done by deleting the #). A suggestion is to never delete lines of code unless you are 100% positive that either 1: you don’t need it or 2: it’s wrong. Basically, comments let you erase lines of code without actually erasing them.
## Set the radius (this line would be ignored by the program)
R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored)
  • 184_notes/python_syntax.1527177921.txt.gz
  • Last modified: 2018/05/24 16:05
  • by dmcpadden