0% found this document useful (0 votes)
3 views

Year 8 Computing Notes - Program Libraries (2)

Uploaded by

jngaruiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Year 8 Computing Notes - Program Libraries (2)

Uploaded by

jngaruiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAM LIBRARIES

• The purpose of a library is to enable readers to borrow and


read books for a set period of time, without paying a fee.
• Program libraries contain pre-written and tested modules, or
self-contained programs, that can be called by another
program.
• Program libraries save programmers time in writing and
testing newly created functions.
• Libraries also allow less experienced programmers to include
complex things that they may not yet be able to create for
themselves.
OUTLINE THE PURPOSE OF PROGRAM LIBRARIES

1. Building Blocks of Code:


∙ Imagine you're building a complicated structure with LEGO bricks. Program libraries are like
pre-made LEGO pieces that you can use to create your programs without starting from scratch.
2. Saving Time and Effort:
∙ Libraries contain ready-to-use code for common tasks, so you don't have to write everything
by yourself.
3. Sharing and Cooperation:
∙ Just like you share knowledge and ideas with classmates, developers share pieces of code in
libraries. This helps everyone work together, making the whole coding community stronger.
4. Avoiding Mistakes:
∙ Libraries have been tested a lot by smart developers. Using them is like using a recipe that
many people have tried and confirmed is delicious. It helps you avoid making the same
mistakes others already fixed.
5. Being Creative:
∙ With libraries, you can focus on being creative and making your program unique, rather than
spending all your time on the basic stuff. It's like having a canvas with the background already
painted, and you get to add the finishing touches.
Identify and use library functions in programs

Identifying Library Functions:


1. Understanding Functions:
Functions are like mini-programs within a program. They perform specific
tasks.
Libraries provide collections of functions that can be reused in different
programs.
2. Library Naming:
Libraries have unique names. Examples include "math," "string," or "random."
You'll often see these names at the beginning of your code when you import
them.
3. Import Statements:
To use a library in your program, you need to import it.
Common import statements: import math, import random, etc.
4. Documentation:
Each library has documentation that explains what functions it offers and how
to use them.
Online resources or the official documentation help you understand the
available functions.
Knowing How to Use Library Functions:
1. Importing Libraries:
Use import statements to bring in the entire library.
Example: import math allows you to use functions from the
math library.
2. Function Syntax:
Understand the syntax for calling functions. It includes the library
name, a dot, and the function name.
Example: library_name.function_name(arguments)
3. Providing Arguments:
Functions often require additional information to work. These are
called arguments.
Example: math.pow(2, 3) raises 2 to the power of 3.
4. Storing Results:
Functions can return values that you can store in variables for
later use.
Example: result = math.sqrt(16) stores the square root of 16
5. Error Handling:
Understand common errors, such as misspelling a function name
or using the wrong number of arguments.
Read error messages to identify and fix issues.
6. Exploring Examples:
Look at examples in documentation or online resources to see
how functions are used in different scenarios.
Experiment with code snippets to understand how functions
behave.
7. Practice and Experimentation:
The more you practice, the more comfortable you'll become with
using library functions.
Experiment with different functions and see how they affect your
program.
By understanding how to identify, import, and use library functions,
you'll unlock a world of pre-built functionality that can enhance the
power and efficiency of your text-based programs.
Examples
1. Use of math library
# Importing math library
import math

num = 16
print(math.sqrt(num))

2. Use of
# Importing specific items
from math import sqrt, sin

num = 16
pi = 3.14
print(sqrt(num))
print(sin(pi))
3. Use of random library
import random

num1 = random.random()
print(num1)

num2 = random.randrange(56,876)
print(num2)

num3 = random.randrange(56)
print(num3)

list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
import random
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))

r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))

list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
string = "students"
print(random.choice(string))
tuple1 = (1, 2, 3, 4, 5)
print(random.choice(tuple1))
import random
sample_list = [1, 2, 3, 4, 5]

print("Original list : ")


print(sample_list)

random.shuffle(sample_list)
print("\nAfter the first shuffle : ")
print(sample_list)

random.shuffle(sample_list)
print("\nAfter the second shuffle : ")
print(sample_list)

You might also like