Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
184_notes:python_syntax [2018/05/25 14:34] – dmcpadden | 184_notes:python_syntax [2020/08/24 19:31] (current) – dmcpadden | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | / | ||
+ | |||
===== Common Commands and Tips for Python ===== | ===== 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/ | 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/ | ||
+ | |||
+ | ==== 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. | ||
+ | * **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 "#" | ||
+ | * **Notating units** - especially in long equations or calculations, | ||
+ | * **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) | ||
+ | </ | ||
==== Common Commands ==== | ==== Common Commands ==== | ||
Line 7: | Line 25: | ||
A=5.42*10**4 | A=5.42*10**4 | ||
</ | </ | ||
- | * Objects (box, sphere, | + | * **Creating |
- | * Object characteristics (pos, length, width, radius, color, etc) | + | < |
- | * Vectors - vec() | + | cloud = sphere(pos=vec(0, |
- | * Magnitude | + | </ |
- | * Print - print() | + | |
+ | < | ||
+ | object1=box(pos=vec(0, | ||
+ | object1.pos=vec(3, | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | position1=vec(3, | ||
+ | position2=vec(2, | ||
+ | separation=position2-position1 | ||
+ | testing=separation-4 #This will give an error | ||
+ | </ | ||
+ | * **Vector magnitude** - the "mag()" command will calculate the [[184_notes: | ||
+ | < | ||
+ | vector1=vec(1, | ||
+ | mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2) | ||
+ | </ | ||
+ | * **Printing values** | ||
+ | < | ||
+ | A=7*9/3 | ||
+ | print(A) #This will then print out the number 21 | ||
+ | </ | ||
* **Dot Product** - the dot product is a way to [[184_notes: | * **Dot Product** - the dot product is a way to [[184_notes: | ||
< | < | ||
A=vec(1, | A=vec(1, | ||
B=vec(4, | B=vec(4, | ||
- | AB=dot(A,B) | + | AB=dot(A, |
+ | print(AB) | ||
</ | </ | ||
* **Cross Product** - the cross product is another way to [[184_notes: | * **Cross Product** - the cross product is another way to [[184_notes: | ||
Line 22: | Line 62: | ||
C=vec(5, | C=vec(5, | ||
D=vec(3, | D=vec(3, | ||
- | CD=cross(C, | + | CD=cross(C, |
- | </ | + | print(CD) #This will print out the vector |
- | + | ||
- | ==== Tips for Coding ==== | + | |
- | * **Check your parentheses** - the computer | + | |
- | < | + | |
- | A=4+4/2 | + | |
- | B=(4+4)/2 | + | |
- | </ | + | |
- | * ** Watch upper/lower cases** | + | |
- | * **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 "#" | + | |
- | | + | |
- | * **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 | + | |
- | R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored) | + | |
</ | </ |