![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 86
Rep Power: 0
![]() |
def my_abs()?
i was doing a tutorial a while ago and got stuck on the following part
i never finished the tutorial because i never understood the purpose for this: a = 23
b = -23
def my_abs(num):
if num < 0:
num = -num
return num
if my_abs(a) == my_abs(b):
print "The absolute values of", a,"and",b,"are equal"
else:
print "The absolute values of a and b are different"a = 23
b = -23
if a < 0:
a = -a
if b < 0:
b = -b
if a == b:
print "The absolute values of", a,"and",b,"are equal"
else:
print "The absolute values of a and b are different"i understand that the "my_abs" is just a variable and can have another name same with "(num)" but i dont understand the if my_abs(a) == my_abs(b): |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
The 'def' statement defines a so called function. You define it, and then you can use it with whichever numbers you want.
def my_abs(num): #num is a placeholder, so you can put in whichever
if num < 0: number you like
num = -num
return numif my_abs(a) == my_abs(b): my_abs(a) simply replaces every 'num' in the function with a 23, and then returns that value, so it can be compared to the absolute value of 'b', which is determined the same way. I hope this helped a bit... ![]() |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 86
Rep Power: 0
![]() |
where it sais (num) "num" can be anything?
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
It can. I would advise only sending numbers though.
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 86
Rep Power: 0
![]() |
if you looked in the beginning of this thread i said that the tutorial said that the second way was easier
to me the first one is easier even if it takes up more space(since when do we worry about space saving these days anyway?) but in other programs is "def" necessary? |
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
Quote:
It is not necessary, but it sure makes the job easier. For example, if you did not use a def statement to define the absolute value function, you would have to type the whole thing every time you want two numbers compared by their absolute value. It is not about space saving, but about time saving... |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2005
Posts: 86
Rep Power: 0
![]() |
i have all the time in the world
if eventualy i want to do it quicker than i will learn it thanks thats all i realy needed to know |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 10
Rep Power: 0
![]() |
It isn't just a matter of code reuse. If you want to harness the full object oriented power of python you will need to learn about methods and classes.
But for small programs or scripts you could get by without using def (though it would help you even then). Cheers, b01c |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
By using the right function names you can make your code more readable too. Take a look at this ...
[PHP]# a quick look at Python functions def PieCookingStatus(CookingTime): if CookingTime < 30: print "Still cooking!" elif CookingTime <= 40: print "Almost done!" else: print "Take pie out of the oven!" def PiePercentCooked(CookingTime): return 100*CookingTime/40 # check the apple pie in the oven after 30 minutes PieCookingStatus(30) print "Pie is %d percent cooked." % PiePercentCooked(30); [/PHP]
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2005
Posts: 86
Rep Power: 0
![]() |
so "def" gives another name to a variable?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|