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/24 16:05] – 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/ | ||
- | |||
- | ==== Common Commands ==== | ||
- | * **Exponents** - to write an exponent in Python you have to use a " | ||
- | < | ||
- | 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() | ||
==== Tips for Coding ==== | ==== 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. | + | * **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 | A=4+4/2 | ||
Line 29: | Line 18: | ||
## Set the radius (this line would be ignored by the program) | ## 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) | R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored) | ||
+ | </ | ||
+ | |||
+ | ==== Common Commands ==== | ||
+ | * **Exponents** - to write an exponent in Python you have to use a " | ||
+ | < | ||
+ | A=5.42*10**4 | ||
+ | </ | ||
+ | * **Creating Objects** - in the code we often want to create an object as a visual for the model. For example, we might want to create a sphere to represent a cloud or an arrow to represent the electric field. Python has the objects built in, but you have to tell it the characteristics of the object you want to create. For example, in the following code, the command sphere tells Python to make a sphere, pos=vec(0, | ||
+ | < | ||
+ | cloud = sphere(pos=vec(0, | ||
+ | </ | ||
+ | * **Object characteristics** - depending on what object you are creating there may be different characteristics that you want to define (pos, length, width, radius, color, etc). You can also change the characteristics of an object later in the code. This can be particularly useful for the updating the position of an object, which can let you simulating a moving object. In the example below, the first line of code creates a green, 2X2 box that is centered at the origin (0,0,0). In the second line of code, the box position is changed to (3,4,5) without changing the other characteristics of the box. | ||
+ | < | ||
+ | object1=box(pos=vec(0, | ||
+ | object1.pos=vec(3, | ||
+ | </ | ||
+ | * **Vectors** - In Python, you can define a variable as a vector using the " | ||
+ | < | ||
+ | position1=vec(3, | ||
+ | position2=vec(2, | ||
+ | separation=position2-position1 | ||
+ | testing=separation-4 #This will give an error | ||
+ | </ | ||
+ | * **Vector magnitude** - the " | ||
+ | < | ||
+ | vector1=vec(1, | ||
+ | mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2) | ||
+ | </ | ||
+ | * **Printing values** - The command " | ||
+ | < | ||
+ | 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: | ||
+ | < | ||
+ | A=vec(1, | ||
+ | B=vec(4, | ||
+ | AB=dot(A, | ||
+ | print(AB) | ||
+ | </ | ||
+ | * **Cross Product** - the cross product is another way to [[184_notes: | ||
+ | < | ||
+ | C=vec(5, | ||
+ | D=vec(3, | ||
+ | CD=cross(C, | ||
+ | print(CD) | ||
</ | </ |