Lab10… (10) - JupyterLab

.pdf

School

Texas Tech University *

*We aren’t endorsed by this school

Course

1330

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

14

Uploaded by DoctorMouse2563

Report
9/25/23, 3:21 PM Lab10 (8) (1) localhost:8921/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab10 (8) (1).ipynb 1/14 SEP 20th – 011-D60 Laboratory 10: FUNctions HirosLaptop hiroslaptop\landy C:\Users\landy\anaconda3\python.exe 3.11.4 | packaged by Anaconda, Inc. | (main, Jul 5 2023, 13:38:37) [MSC v.1916 64 bi t (AMD64)] sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial=0) Full name: Landyn H. Ramos R#: 11920651 Title of the notebook: Funcs Date: In [1]: # Preamble script block to identify host, user, and kernel import sys ! hostname ! whoami print ( sys . executable ) print ( sys . version ) print ( sys . version_info )
9/25/23, 3:21 PM Lab10 (8) (1) localhost:8921/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab10 (8) (1).ipynb 2/14 What is a function in Python? Functions are simply pre-written code fragments that perform a certain task. In older procedural languages functions and subroutines are similar, but a function returns a value whereas a subroutine operates on data. The difference is subtle but important.
9/25/23, 3:21 PM Lab10 (8) (1) localhost:8921/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab10 (8) (1).ipynb 3/14 More recent thinking has functions being able to operate on data (they always could) and the value returned may be simply an exit code. An analogy are the functions in MS Excel . To add numbers, we can use the sum(range) function and type =sum(A1:A5) instead of typing =A1+A2+A3+A4+A5 Calling the Function We call a function simply by typing the name of the function or by using the dot notation. Whether we can use the dot notation or not depends on how the function is written, whether it is part of a class, and how it is imported into a program. Some functions expect us to pass data to them to perform their tasks. These data are known as parameters( older terminology is arguments, or argument list) and we pass them to the function by enclosing their values in parenthesis ( ) separated by commas. For instance, the print() function for displaying text on the screen is \called" by typing print('Hello World') where print is the name of the function and the literal (a string) 'Hello World' is the argument. Program flow A function, whether built-in, or added must be defined before it is called, otherwise the script will fail. Certain built-in functions "self define" upon start (such as print() and type() and we need not worry about those funtions). An example below will illustrate, change the cell to code and run it, you should get an error. Then fix the indicated line (remove the leading "#" in the import math ... line) and rerun, should get a functioning script. Using arithmetic square root of 4.0 is 2.0 Using math package square root of 4.0 is 2.0 In [4]: # reset the notebook using a magic function in JupyterLab % reset -f In [3]: # An example, run once as is then activate indicated line, run again - what happens? x = 4. sqrt_by_arithmetic = x ** 0.5 print ( 'Using arithmetic square root of ' , x , ' is ' , sqrt_by_arithmetic ) In [4]: import math # import the math package ## activate and rerun sqrt_by_math = math . sqrt ( x ) # note the dot notation print ( 'Using math package square root of ' , x , ' is ' , sqrt_by_arithmetic )
9/25/23, 3:21 PM Lab10 (8) (1) localhost:8921/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab10 (8) (1).ipynb 4/14 Using arithmetic square root of 4.0 is 2.0 Using math package square root of 4.0 is 2.0 Built-In in Primitive Python (Base install) The base Python functions and types built into it that are always available, the figure below lists those functions. Notice all have the structure of function_name() , except __import__() which has a constructor type structure, and is not intended for routine use. We will learn about constructors later. Added-In using External Packages/Modules and Libraries (e.g. math) Python is also distributed with a large number of external functions. These functions are saved in files known as modules. To use the built-in codes in Python modules, we have to import them In [5]: # Here is an alternative way: We just load the function that we want: # reset the notebook using a magic function in JupyterLab % reset -f # An example, run once as is then activate indicated line, run again - what happens? x = 4. sqrt_by_arithmetic = x ** 0.5 print ( 'Using arithmetic square root of ' , x , ' is ' , sqrt_by_arithmetic ) from math import sqrt # import sqrt from the math package ## activate and rerun sqrt_by_math = sqrt ( x ) # note the notation print ( 'Using math package square root of ' , x , ' is ' , sqrt_by_arithmetic )
9/25/23, 3:21 PM Lab10 (8) (1) localhost:8921/lab/tree/C%3A%5CUsers%5Clandy%5COneDrive%5CDesktop%5CC Engr 1/Lab10 (8) (1).ipynb 5/14 into our programs first. We do that by using the import keyword. There are three ways to import: 1. Import the entire module by writing import moduleName; For instance, to import the random module, we write import random. To use the randrange() function in the random module, we write random.randrange( 1, 10);28 2. Import and rename the module by writing import random as r (where r is any name of your choice). Now to use the randrange() function, you simply write r.randrange(1, 10); and 3. Import specific functions from the module by writing from moduleName import name1[,name2[, ... nameN]]. For instance, to import the randrange() function from the random module, we write from random import randrange. To import multiple functions, we separate them with a comma. To import the randrange() and randint() functions, we write from random import randrange, randint. To use the function now, we do not have to use the dot notation anymore. Just write randrange( 1, 10). 7 6 4 The modules that come with Python are extensive and listed at https://docs.python.org/3/py- modindex.html . There are also other modules that can be downloaded and used (just like user defined modules below). In these labs we are building primitive codes to learn how to code and In [6]: # Example 1 of import % reset -f import random low = 1 ; high = 10 random . randrange ( low , high ) #generate random number in range low to high Out[6]: In [7]: # Example 2 of import % reset -f import random as r low = 1 ; high = 10 r . randrange ( low , high ) Out[7]: In [8]: # Example 3 of import % reset -f from random import randrange low = 1 ; high = 10 randrange ( low , high ) Out[8]:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help