-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPython04.py
73 lines (57 loc) · 2.52 KB
/
Python04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Python04.py
# IJ BAR: https://github.com/tferr/Scripts#scripts
####################################################
# 4. Functions and modules
####################################################
# Functions are defined with <def>. Return values are
# specified by a <return> statement. Here is a function
# without arguments:
def today():
"""This function returns a string of today's date"""
import datetime # https://docs.python.org/2/library/datetime.html
td = datetime.date.today()
return td
print "[Line 16]", "today() returned", today()
print "[Line 17]", "str(today()) returned", str(today()) # https://docs.python.org/2/library/functions.html?highlight=str()
# A Function with arguments:
def minFunction(a, b):
"""This function returns the smallest of two arguments"""
if a < b:
return a
else:
return b
print "[Line 28]", minFunction(10,20), "is smaller"
# NB: Python already features a built-in function that returns the
# smallest of two or more arguments. Try this:
print "[Line 33]", "The smallest of all is", min(10,20,-10,"-20") # https://docs.python.org/2/library/functions.html?highlight=min()
# A practical scenario: Try to import something that may not be present.
# If we cannot find it, we try to define it in some other way so that the
# script can progress. In this example, we are trying to use the function
# <theBestDrinkEver> from the (rather fictitious) module <cocktail>
def myCocktail(a, b):
"""This function mixes a and b in the right amount
if the fictitious cocktail module is installed """
try:
from cocktail import theBestDrinkEver
print "[Line 45]", "theBestDrinkEver was imported"
except ImportError:
print "[Line 47]", "Please install the cocktail package!"
def theBestDrinkEver():
return "Cuba Libre!"
print "[Line 51]", "The best drink ever is " + theBestDrinkEver()
# Modifying global variables from within functions: Python wants you to
# confirm that the function is really expected to modify a global variable
# declared outside its scope, by explicitly using the <global> keyword:
best_rum = "Bacardi"
def set_best_rum_to_brugal():
global best_rum
best_rum = "Brugal"
def set_best_rum_to_abuelo():
best_rum = "Ron Abuelo"
print "[Line 66]", "Best rum: " + best_rum
set_best_rum_to_brugal()
print "[Line 68]", "Best rum: " + best_rum
set_best_rum_to_abuelo() # This will have no effect
print "[Line 70]", "Best rum: " + best_rum
## To know more:
# http://www.jython.org/jythonbook/en/1.0/DefiningFunctionsandUsingBuilt-Ins.html