Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Learning Python (Help for Programmers)    (ISBN: 1565924649)


 

 List Price: $34.95
 Our Price: $24.47
 Used Price: $8.78

 Release Date: May, 1999
 Manufacturer: O'Reilly & Associates (Paperback)
 Sales Rank: 11,253

 Author: Mark Lutz, David Ascher









More Info

 Functions in Python 2002-03-14 03:09:45
  python 
Category: source:python:general
Description: Example on creating and using custom functions.
Platform: unix
Author: mind
Viewed: 2825
Rating: 2.8/5 (9 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
functions.py
#!/usr/local/bin/python
#
# function example by mind@metalshell.com
#
# here is an example on creating and using custom functions.
#
# 03/12/2002
#
# http://www.metalshell.com
#

# example of making a custom function using integers
# def func_name(string1, string2, ...)
# def func_name(integer1, integer2, ...)
# def func_name(string1, integer1, ...)
#
# bad example:
# def func_name(string1, integer1, integer2):
#   if len(string1) == 0:
#     return 1
#
#   if integer1 < integer2:
#     return 2
#
#
# if func_name(some_string, 3, some_string2) == 2:
#   do_whatever..
#
# obviously you cant pass a string to a function that thinks its an integer
# unless you change the string to an integer or the other way around in the
# function.

# define our integers
test1 = 10
test2 = 12

def add_two(num):
  num = num + 2
  return num

def sub_two(num):
  num = num - 2
  return num

def is_equal(str1, str2):
  if str1 == str2:
    return 1
  else:
    return 0

if is_equal(test1, test2) == 1:
  print test1, "equals", test2
else:
  print test1, "does not equal", test2

if add_two(test1) == test2:
  print test1, "+ 2 equals", test2
else:
  print test1, "+ 2 does not equal", test2

if sub_two(test1) == test2:
  print test1, "- 2 equals", test2
else:
  print test1, "- 2 does not equal", test2

if sub_two(test2) == test1:
  print test2, "- 2 equals", test1
else:
  print test2, "- 2 does not equal", test1


# ok you get the idea... you can also do the same thing with strings:

test3 = "This is a test string"
test4 = "This is a test string"
test5 = "string test a is This"

def match(str1, str2):
  if str1 == str2:
    return 1
  else:
    return 0


if match(test3, test4) == 1:
  print "\"", test3, "\"", "and", "\"", test4, "\"", "both match"
else:
  print "\"", test3, "\"", "and", "\"", test4, "\"", "do not match"

if match(test4, test5) == 1:
  print "\"", test4, "\"", "and", "\"", test5, "\"", "both match"
else:
  print "\"", test4, "\"", "and", "\"", test5, "\"", "do not match"

print "Example finished!"
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?