1.10. Examples: Lecture 1#

1.10.1. First script example#

  • script_01.py


print ('hello world')

1.10.2. Definition of a function#

  • script_02.py

def squared (x) :
  return x*x

if __name__ == "__main__":
  number = float (input ('insert a number:'))
  print ('the square power of ' + str (number) + ' is: ' + str (squared (number)))

1.10.3. Use of an external linbrary#

  • script_03.py

import sys

def squared (x) :
  return x*x

if __name__ == "__main__":

  number = float (sys.argv[1])
  print ('the square power of ' + str (number) + ' is: ' + str (squared (number)))

1.10.4. Definition of a library#

  • script_04.py

import sys
from operations import squared

if __name__ == "__main__":

  number = float (sys.argv[1])
  print ('the square power of ' + str (number) + ' is: ' + str (squared (number)))
  • operations.py

def squared (x) :
  """calculates the square of a number
  
  Args:
    x (float): a number
  
  Returns:
    (float): the square of x
  """
  return x * x


# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 

    
if __name__ == "__main__":

  number = 3.
  print ('the square power of ' + str (number) + ' is: ' + str (squared (number)))