What does the python code herein mean? def pyramid(rows=8):

what's the meaning of the python code here-below?
def pyramid(rows=8):
for I in range(rows):
print ' '*(rows-i-1) + '*'*(2*i+1)
pyramid(8)
mtlles015
Asked Mar 10, 2014
EDIT: this website seems to be horrible for programming discussion as it killed the tabulation in my answer when I published it, I'm putting three '_' where there should be a tabulation to solve the problem. It's so weird but everything else I tried failed.

That 'script' has a little fail, it sets a variable named 'I' which later tries to refer with a non capital 'i'. if you put it like that on a python interpreter it won't work.
The original micro-'script' had to be something like this:

def pyramid(rows=8):
___for I in range(rows):
______print ' '*(rows-i-1) + '*'*(2*i+1)

pyramid(8)

Also the 'pyramid' function should be called something like 'print_pyramid' to improve legibility but on such a simple script it has no sense to worry about that.

Now the explanation, it can be short or long, the short answer could be like this:

This script defines a function to draw a pyramid with as tall as you want it to be, and then draws a 8-floor one.

But I think you may like a more detailed one really?
This is what the script does step by step:

----line 1; def pyramid(rows=8):
It defines a function 'pyramid' wich receives the argument 'rows' that defaults to 8 when you don't specify when calling the function.
Next lines with a single tab (or 4 spaces) or more on their beggining describe what the function does.

A function is a named piece of code you can call wherever you are in the script by writing it's name and passing it some necessary arguments, for example;
if in a script you're gonna compute lots of circles' areas basing on it's radius, it may be useful to define a 'get_area_from_radius' function (no spaces allowed in names of anything) which takes the argument 'radius' and returns it, this will be done like this:

def get_area_from_radius(radius):
___area = 3.14*(radius**2)
___return area

#this could print the area of a circle with radius = 3 if you used it this way in the same file:

print get_area_from_radius(3)

#or this one:
radius = 3
area = get_area_from_radius(radius)
print area


----line 2; ___for I in range(rows):
Here it defines a 'for' bucle inside the function; a 'for' bucle is used to repeat a piece of code with any list of values.
'for I in [1,2,3]:' means: do what I'm going to tell you folllowing one time setting 'i' to 1, another setting 'i' to 2 and another setting 'i' to 3.
'range' is a python function which returns a list of numbers, if you pass it a single argument, x, it returns a list that counts from 0 to x-1, this way when you write 'range(4)', it's like if you wrote '[0,1,2,3]'.
This line means: do what i'm going to tell you following making the 'i' variable count from 0 to the number specified in the call.

----line 3; ______print ' '*(rows-i-1) + '*'*(2*i+1)
Note that it has one-level larger tabulation than the previus, it's because it's the content of the 'for' bucle.
When you put a variable after 'print' in python, that variable will be put on the console output of python, and if instead of a variable you put something computable by python, it will solve it and print it. This line prints the string (text) resulting of repeating the right amount of spaces and asterisks to draw the pyramid.
In python you can create strings by quoting letters, and multiplying a string results in another string with the letters of the first repeated over and over ( 'hi'*4 = 'hihihihi' ).
the first line in the output will have a lot of blank space (the number of lines the pyramid occupy minus 1) and a single asterisk, then in the next line it will put one less space and two more asterisks, and that again and again until the pyramid is ended.

----line 4;
One of the main features of python is the readabality of the code written in it. To preserve that feature it's essential to visually divide scripts in paragraphs, instead of not leaving an empty line, as well as give proper names to variables, functions, classes...

----line 5; pyramid(8)
This is a call to the function passing 8 as the value for 'rows'.
The code doesn't actually do nothing except preparation until this line where it executes what we wrote before.


I recomend python as programming language as long as you don't really need awesome speed on your program, it's easily debuggable, readable, and generally intuitive, and you can even get a good speed using numpy for the heavy stuff. Sorry if I mistoke any word here, I'm spanish and I wouldn't like at all to make python look like a programing language for fools, we are talking about a great tool which is simple as hell to learn and code quickly, and will eventually get better as you get experienced with it and you know how to use it more efficiently.
clusterfut
Answered Mar 11, 2014
Edited Mar 11, 2014

TIP: If it's not your answer to this question, please click "Leave a Comment" button under the question to communicate with the question owner.

Categories