



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Files to submit: Contents:
**1. warrior.py
T-Square Forum - See Lab 1. TA Helpdesk - Schedule posted on class website. Email TA's Don't forget to include the required comments and collaboration statement (as outlined on the course syllabus). Do not wait until the last minute to do this assignment in case you run into problems. If you find a significant error in the homework assignment, please let a TA know immediately.
Windows users should download the Windows installer: http://www.python.org/ftp/python/2.5.1/python-2.5.1.msi Download and run the file above. Just follow the friendly instructions on screen to complete the installation.
Installation instructions depend on which version of Mac OS X you are using. If you are not sure, which version of Mac OS X you're running, open "About this Mac" under the apple menu (below on left). The version number is listed right below the logo (below on right). Once you know which version of OS X you are running, follow the instructions at: http://www.python.org/download/mac/ Installing Python and IDLE can sometimes be confusing, so if you are confused and have questions, BE SURE TO ASK!!
In 1978, a significant book in the history of Computer Science was published: The C Programming Language by Kernighan and Ritchie. The first example they presented was a simple program that printed the words "hello world" on the screen. Since then, the traditional first program written when learning a new programming language is Hello World. We will also implement a hello world program in Python. We will walk you through it step-by-step. First open IDLE. (the screenshots in this tutorial are from Windows, but OSX should behave the same way) .Your screen should look like this: This is the interpreter window. Python is an interpreted language, meaning it is not compiled. If this makes no sense to you, don't worry about it - it will be covered later in the semester. Now, at the prompt (>>>), type 2 + 2 and hit enter: IDLE should display: >>> 2 + 2 4 >>> Now try this: >>> print "Hello, world." IDLE should display: >>> print "Hello, world." "Hello, world." >>> Now you want to save this code so you can submit it later. You won't be able to save code directly from this window, though, so you must open an editor window. Do this by selecting File->New Window from the Menu Bar (or pressing Ctrl-N). This should open a screen like this:
Congratulations! You have just finished your first Python program. Now with the skills you have just learned, write a program called warrior.py that prints out the phrase, “I am a Python warrior” instead of “Hello, world.”
Built-in Python Functions : http://docs.python.org/lib/built-in-funcs.html
You should have seen this function in class, but here's a quick example of how it works to get input from the user. Example: >>> s = raw_input('--> ') --> Monty Python's Flying Circus >>> s "Monty Python's Flying Circus"
Converts a string or a number to floating point number (a number with decimals, referred to as a float). If the argument is a string, it must contain only numbers (and maybe the minus sign in front). Otherwise, the argument may be an integer or a float (a float would just return the same float).
Returns a string containing a nicely printable representation of an object. For strings, this returns the string itself. Numbers just gain quotes around them to become string representations of the original numbers. If no argument is given, it returns the empty string, ' '. Math Module Info : http://docs.python.org/lib/module-math.html (You need to place import math before your code to use this). math.pi: A variable stored in the math module approximately equal to π.
Remember from high school geometry that the volume of a sphere is:^4 / π R 3 , where R is the radius of 3 the sphere. We are now going to write a program to calculate the volume of a sphere. We can break this problem into 3 steps. You'll need to:
To compute the volume, you will need to multiply all of the parts together (the fraction 4/3, pi, and the cubed radius). Remember that python uses integer division, so 4/3 =1, but 4.0/3.0 = 1.3333333. Since we want our calculation to be fairly exact, we should use Python's built-in value for pi, not just 3.14. To use the pi constant, you need to import the math module. (it is best to put the import math statement as your first line of code) Once you have imported the module, you can treat the constant math.pi just like any other number. To compute exponents, we can use Python's built-in exponentiation operator: **
For example: 4 ** 2 = 16 (four squared equals 16) When we want to print out our answer, we need to convert it to a string using str(). If it helps, for the purposes of this homework, you can think of str() as the opposite of float(). For example: z = 32. print "The value of z is " + str(z) + "units" >>>"The value of z is 32.0 units" Write a program using IDLE which does exactly what is described above and looks like the sample output (given below) when run. If you wish, you may use string formatting (the %f thing Monica showed in class) to round the volumes to 2 decimal places. Save this file as sphere.py. Sample output: >>> What is the radius of the sphere? 5 Volume of sphere is 523.598775598 cubic units >>> >>> What is the radius of the sphere? 20 Volume of sphere is 33510.3216383 cubic units >>> Once you have completed parts 1-3, check you code to make sure it runs smoothly ( Don't just double- click on your file to open it. When you right-click on the file, select Edit with IDLE and then select Run Module to run your file). Be certain you name your files exactly as indicated; SPHERE.PY is not the same as sphere.py. Instructions for submitting this assignment will be given next week as soon as the details are finalized. Adapted from Fall 2006’s HW 1, written by Joshua Silver - CS1301 Fall 2007