Fer said:
Rectangle area...
http://dpaste.com/hold/1424686/
This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.
Operators work with one or more objects and can perform tasks such as math, comparison, and inspection.
There are standard operators for arithmetic:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
// | Integer Division |
% | Modulus Division |
** | Exponent |
Additionally, you can modify a named value and assign the output of an operator to the name in one line with inline assignment operators.
>>> a_number = 1 >>> a_number += 1 >>> a_number 2 >>> a_number *= 8 >>> a_number 16 >>> a_number **= 2 >>> a_number 256 >>> a_number /= 2 >>> a_number 128 >>> a_number %= 3 >>> a_number 2 >>> 3.123 // a_number 1
Uhm I'm really bad at math - actually, I suck. Can someone please explain to me in simple and plain language how to create the program needed for the exercise? Assume that I'm a little kid and take a step-by-step instructions approach. Thanks a million!
The key is to write the formula as math first (I do realize you need to know the formula).
Area equals the height of rectangle times the width of rectangle.
Math people usually say: Area = H x W which is close to the Python idea of using variables.
Next change that to Python style code.
areaRect = h * w
Finally, use the inputs to get values for h and w and put the formula line after them and do a print of the answer.
Hope that helps.
It actually helps a whole lot. I'll write it down and work around it to solve the exercise.
Thanks!
Sorry if I bother you again: I'm gonna do this with raw_input, right?
In principle you could do so... though that's not asked directly. The excercise could be understood as just doing the calculations. But probably it's better if you ask the user, it would be more useful :)
Yes, raw_input will work, but be sure to convert the input values to integer after the input.
Area of a Rectangle using a Function( yes, I know we haven't learned functions yet):
Be sure you have the required indenting.
def area_rectangle(length,width):
area=length * width
print "The area of this rectangle is:",area
Now, the nicest thing your example did was make me try to understand the function concept, especially the way variables get handled. I'd read about local vs. global variables, but was confused by the idea. However, trying to implement the function so I could get the results of the function to print both "inside" (as a variable local to the function) and "outside (as a global variable captured out from the function made me puzzle why my examples were not working...but now I get it!
Thanks for spurring me to figure it out, canuckfan34.
Here's how I processed the information...with many trials and frustrating prints of the value of zero for the final print.
The big key for me was seeing the difference of calling the function:
area_rectangle(5,7)
which shoves the two values into the function and gives variable area work inside as a local var.
The second half of my slow realization came when I saw that the value inside not only needed a return from inside, but that the call of the function needed to be assigned to the global area variable.
area = 0 # initializes the global variable
def area_rectangle(length,width): # vars pulled into the function
area = length * width
print "The area of the rectangle is: ", area # local "inside" var
return area
# The next line sort of works, getting the values into the function.
# area_rectangle(5,7) # but it doesn't work to capture the return attempt
# The big realization was that the inside return needed to be assigned
# to the outside (global) variable, EVEN IF THEY WERE THE SAME VAR NAME
area = area_rectangle(5,7) # this assignment captures the inside local
# via the return to the outside, global area var
# The inside area isn't the same as the outside one even though they
# share the same human-readable name.
print "The area (global) is: ", area
# Over and over this stayed zero, until I saw the need to assign the
# return value of the inside (local) to the outside (global)
# during the function call.
A more readable version is here: https://gist.github.com/algotruneman/5868694
Thank you, canuckfan34. Your example helped me significantly advance my understanding of Python.
I did mine in Ipython3.
The code is here.
I randomized the values to make it a little more interesting.
#Use the theorem of pythagoras about rectangular triangles:
a = raw_input ('size of side a: ')
b = raw_input ('size of side b: ')
print "c = ", round((float(a)**2+float(b)**2)**0.5,2)
#calculate the surface of a circle
d = raw_input('The diameter is: ')
print "The circle's surface is: ", round(3.1415*float(d)**2/4,4)
#calculate the surface of a circle
import math
d = raw_input('The diameter is: ')
print "The circle's surface is: ", round(math.pi*float(d)**2/4,4)
#calculate the surface of a rectangle
length = raw_input('length: ')
width = raw_input('width: ')
print "The surface of the rectangle is: ", float(length)*float(width)
FInding the circumference of a circle:
How to calculate the area of a rectangle:
j= 'What is the base of the rectangle?'
k= raw_input (j)
l='What is the side of your rectangle?'
m=raw_input (l)
print int(m)*int(k)
a = 'What is the radius of your circle? '
b = raw_input(a)
pi = 3.14159265359
area = float(pi)*float(b)**2
Anyone pls help. How do i compile and run my program without using the command line that just gives results line by line?
You just need to save your script in a file with the extension .py then run the file using python
I use text wrangler as a free editor
#Calculate the Area of a circle
import math
print('To Calculate the Area of a Circle')
radius=float(input('Enter the Radius of a circle:'))
area=math.pi*radius**2
print('The Area of a Circle is:',format(area,'.2f'))
Output:
To Calculate the Area of a Circle
Enter the Radius of a circle:5
The Area of a Circle is: 78.54
script for calculating volume of sphere can be found here:
http://dpaste.com/771135/
Calculate the area of a circle (with pi as 3.14159265359)
Simple exponent calculator (python 3):
x = input( "Base: " ) y = input( "Power: " ) print( float( x ) ** float( y ) )
Ex1 and 2 from the book
and computation of rectangle area:
Calculate volume and surface of a cone.
Here is my simple program to calculate area and volume of a cylinder:
Here is my program to calculate the circumference and the area of a circle given the radius. I used 22/7 for pi because I wasn't sure how to express it in python. Corrections welcome.
http://dpaste.com/hold/745262/
You need to import the math module and then you can use math.pi. See http://pastebin.com/EFhE4Vsa