In [1]:
x=5
y=7
z=max(x,y) #max is the function. x and y are the arguments
print(z) #print is the function. z is the argument
In [ ]:
!pip install easygui
#pip: python installer program
# ! run the program from the shell (not from python)
# easygui: a python library for GUI widgets
In [2]:
import easygui #Imports easygui into the current namespace. We now have access to functiona and objects in this library
easygui.msgbox("To be or not to be","What Hamlet elocuted") #msgbox is a function in easygui.
In [3]:
import math #imports the math namespace into our program namespace
math.sqrt(34.23) #Functions in the math namespace have to be disambiguated
Out[3]:
In [4]:
import math as m #imports the math namespace into our program namespace but gives it the name 'm'
m.sqrt(34.23) #Functions in the math namespace have to be disambiguated using the name 'm' rather than 'math'
Out[4]:
In [ ]:
In [5]:
from math import sqrt #imports the sqrt function into our program namespace. No other math functions are accessible
sqrt(34.23) #No disambiguation necessary
Out[5]:
In [6]:
def spam(x,y,k):
if x>y:
z=x
else:
z=y
p = z/k
return p #Only the value of p is returned by the function
In [7]:
spam(6,4,2)
Out[7]:
In [8]:
def eggs(x,y):
z = x/y
print(eggs(4,2))
In [9]:
def foo(x,y,z):
if z=="DESCENDING":
return max(x,y),min(x,y),z
if z=="ASCENDING":
return min(x,y),max(x,y),z
else:
return x,y,z
In [10]:
a,b,c = foo(4,2,"ASCENDING")
print(a,b,c)
In [11]:
a = foo(4,2,"ASCENDING")
print(a)
In [12]:
a,b = foo(4,2,"DESCENDING")
In [ ]:
def bar(x,y):
return x/y
bar(4,2) #x takes the value 4 and y takes the value 2
In [13]:
def bar(x,y):
return x/y
bar(y=4,x=2) #x takes the value 2 and y takes the value 4 (Explicit assignment)
Out[13]:
In [14]:
def order_by(a,b,order_function):
return order_function(a,b)
print(order_by(4,2,min))
print(order_by(4,2,max))
In [19]:
def change(x):
x = (1,)
print(x)
x = (1, 2)
change(x)
print(x)
In [26]:
def replace(test_string, replace_string):
start_index = test_string.find(replace_string)
result = ""
x = "bodega"
if start_index >= 0:
result = test_string[start_index:start_index+len(replace_string)]
result = test_string.replace(result,x)
return result
print(replace("Hi how are you?", "yu"))
In [ ]: