Download Hello Campers! - public.asu.edu

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Adventures in Computing Camp
Summer 2015
“Hello Campers!”
1
Goals of this Lab!
• Learn how to interact with the Python console
• Understand Python mathematical expressions
• How to format Python strings with output
1 of 6
Adventures in Computing Camp
Summer 2015
2
“Hello Campers!”
Starting Lab
To start the lab, we need to first open Spyder and create a new project.
• When Spyder first loads, click “File” → “New project...”. This will open a new dialog box that will
ask you to name your project. The project name has to be one word (no spaces), and only has letters
and numbers in it.
• Once we do this, we should get a screen similar to (the name of the project here is “HelloCamp”):
• Once we are here, we right-click the project name, and choose “New” → “File...”. This opens a save
dialog box where you can name your file anything you like, and saves it in the project directory. Once
you create the file, the Editor screen becomes a text editor where you can modify your Python file.
• There are several buttons above the Project explorer and Editor. The one that we will use most often
is the green arrow, which means to run the file currently shown in the Editor window.
• Optionally, you can remove the first line in the file, but it is not necessary:
# -*- coding: utf-8 -*Congrats, you are ready to start the lab!
2 of 6
Adventures in Computing Camp
Summer 2015
3
“Hello Campers!”
Interact With the Interactive Console...Interactively
The development environment (IDE) that we will be using for these labs is called Spyder, and it is suuuuuuper
cool, as we will see in later labs. For now there are 2 main features that we are going to be using. The first is
called the ”Console”, and it allows us to enter commands interactively and see responses immediately. Think
of it as a kind of code playground where we can play around with simple ideas without much configuration.
The second is called the ”Editor” and it allows you to rerun code that you have written before. When you
open up Spyder it should look something like the image below, we will be using the console portion in the
bottom-right, and the editor portion being the left side.
4
4.1
You Had Me At “Hello”
Printing in Python
Printing to the console is how the user will interact with the applications we create with the code we write.
To make a very simple application, that just prints “Hello World” to the screen, simply type the following
into the Editor and press “Run”:
print("Hello World")
It’s that easy! Any time that you want to print ”strings” (words or sentences) in Python, you put double
or single quotes around the words, and just call print. At this point, you should be able to have a layout in
Spyder similar to:
3 of 6
Adventures in Computing Camp
Summer 2015
“Hello Campers!”
Ok, so at this point you’re surely impressed that we have made the computer echo back to us. But wait,
there’s more!
4.2
It All Adds Up!
Computers are fine for echoing back things that we tell them, but where they really excel is in Computing.
What if we want to compute and print mathematical expressions, such as 2*(3+100)-(9*63)? We can do the
same thing in Python:
print(2*(3+100)-(9*63))
Try with a few more expressions, test out some division(/), and power functions (2 to the power of 3 is
written as 2**3). What happens when you try to add a “String” and a number? Python allows you to
convert things to strings explicitly by surrounding the expression with str() like below:
print("3 + 2 =" + str(3+2)) # this should print 5
4.3
Very Able Variables
Inserting calculations into python is also fine, but sometimes you might want to use the result of an expression
somewhere else. We do this by “saving to a variable,” where you specify the name of the variable (just like
in math!). For example, if we have the line:
x = 2*(3+100)-(9*63)
4 of 6
Adventures in Computing Camp
Summer 2015
“Hello Campers!”
then we save the result of this expression into the variable called “x.” This is called assignment, and works
as follows: the value of the right-hand-side of the equals sign is saved as the value of the left-hand-side. It
is important to remember that even though we have an equals sign’=’ it does not mean that the values will
always be equal, it is only saying that the result of the expression to the right of the side will be stored in
the variable on the left side.
Using this, we can re-use the value of “x” whenever we like, and also update the value:
x = 2
x = x * 3
# x has a value of 2 here
# the right hand side takes the previous value of x (which is 2),
# multiplies it by 3, and saves it back into x, now having value 6
print(x)
# prints 6
y = x * x
# create new variable y, which has value 6*6 = 36
print(y - x)
# prints 30 (36 - 6 = 30)
print("Result: ", (y-x)) # prints the word "Results: " and the comma means to
# put the value y-x right after
3*z = 2*x
# this will NOT work
In the console enter the following code to calculate the area of a circle:
radius = 5
pi = 3.14
area = pi * radius**2
print("The area of a circle with a radius of " + str(radius) + " is " + str(area))
What happens when you change the value of the radius and print the result again (without running the
other lines of code)? Why do you think that this might be?
4.4
Functionally Awesome
Most programming languages include ”functions” There are “functions” in Python that make it very easy
(and less of a hassle!) to compute various things, such as mathematical expressions. For example, we can
compute the square root of a number, using the “math” library (that we need to use the “import” keyword
to get):
import math # this is needed to use the math functions
x = 500
print(math.sqrt(x)) # calls the sqrt function
which correctly prints 22.360679775. There are other functions:
import math
x = 23.567
y = 2
print("Ceiling: ", math.ceil(x)) # the "ceiling" is essentially rounding up, math.ceil(1.5) -> 2
print("Floor: ", math.floor(x)) # the "floor" is essentially rounding down, math.floor(1.5) -> 1
print("Pow: ", math.pow(x, y)) # x^y (x multiplied by itself y times)
print("Exp: ", math.exp(x)) # e^x (e is a special mathematical constant, equal to ~2.72)
print("Log: ", math.log(x)) # gives the natural log of x ( the z value that satisfies e^z=x)
print("Log: ", math.log(x, y)) # gives the log of x (the y value that satisfies y^z=x)
Play around with those functions a few times (what happens if you take the ceil(floor(x)) )? what about the
opposite order? Is that surprising?
Once you are done with that enter the function below:
5 of 6
Adventures in Computing Camp
Summer 2015
“Hello Campers!”
# This tells python that we are creating a new function named "circleArea"
# And the function that we create needs one piece of outside information (the radius)
def circleArea(radius):
pi = 3.14
area = pi * radius ** 2
return area
Now we can call the function ourself like below:
print("The area of a circle with radius 10 = " + str(circleArea(10)))
5
Application
One well known formula is finding the roots of a polynomial f (x) = ax2 + bx + c, and they are:
xpos =
√
−b+ b2 −4ac
, xneg
2a
=
√
−b− b2 −4ac
2a
It would be great that we can just plug in the values of a, b, c and have our program give us the values
without having to manually calculate it! Here is what you will need:
• Variables a, b, c that will hold the values that you set.
• Using the functions sqrt and pow from the math library (how do you get it again?).
• Create variables xpos, xneg that will hold the values of the calculations.
• Print the values xpos, xneg.
Once you are ready, test out your code. Don’t forget to add some comments to make it easy to read!
Sometimes coming up with test cases is hard, so here are a few cases:
a:0 b:0 c:3 — [xpos:0, xneg:0]
a:1 b:0 c:0 — [xpos:0, xneg:0]
a:1 b:0 c:-4 — [xpos:2, xneg:-2]
6 of 6