0% found this document useful (0 votes)
17 views14 pages

Computer Notes 2

The document provides an overview of program libraries in Python, explaining their purpose, contents, and how to use them. It covers the definition of sub-routines, specifically functions and procedures, and illustrates how to define and call functions, including the use of parameters. Additionally, it details common libraries like math and random, their functions, and examples of their usage in programming.

Uploaded by

karvishah137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

Computer Notes 2

The document provides an overview of program libraries in Python, explaining their purpose, contents, and how to use them. It covers the definition of sub-routines, specifically functions and procedures, and illustrates how to define and call functions, including the use of parameters. Additionally, it details common libraries like math and random, their functions, and examples of their usage in programming.

Uploaded by

karvishah137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Library Programs

Learning Objectives
●​ Understand why program libraries exist.​

●​ Know what a program library contains.​

●​ Learn how to import a library into a Python program.​

●​ Know how to call a library function.​

●​ Understand how to use the return value from a library function.

1. What You Already Know


✔ Programming languages

●​ Programming languages can be block-based (e.g., Scratch) or text-based (e.g., Python).​

●​ Text-based programs run using an IDE (Integrated Development Environment).​

✔ Libraries in everyday life

●​ A library stores many resources for people to use.​

●​ Similar idea in programming: a program library stores ready-made code.​

✔ What is a sub-routine?

●​ A sub-routine is a self-contained algorithm with its own name (identifier).​

●​ You “call” a sub-routine by using its name.​

●​ After the sub-routine finishes, the program returns to the main code.​
Library Programs
2. Sub-routines
There are two types of sub-routines:

A. Functions
●​ A function returns a value to the main program.

B. Procedures
●​ Procedures do NOT return a value.​

●​ They simply perform instructions.​

👉 In Python, both are usually just called functions.​


3. How Functions Work


✔ Defining a function
def functionName():
code inside function

●​ The keyword def starts a function.​

●​ Function names:​

○​ must start with a letter​

○​ cannot have spaces​

○​ cannot start with a number

Example:​
def myFunction():
return "Hello"
print(myFunction())
Library Programs
✔ Calling a function
print(myFunction())

✔ Return value
Whatever is returned replaces the function call.​
Example:​

print(myFunction())
becomes​

print("Hello")

4. Example: Function with Input and


Return
def first():
name = input("Enter your name")
return name
def second():
colour = input("What is your favourite colour?")
return colour

Main program:

print("Welcome to the best program in the world")


userName = first()
print("Hello", userName)
favColour = second()
print(favColour, "is a good choice")
Library Programs
5. Parameters
●​ A parameter is a value that is sent to a sub-routine/function when a sub-routine/function
is called.​

●​ The function uses this value inside it.​

Example:

A function calculates the area of a square, but it needs to know the length of each side of the
square.

The function takes the length as a parameter.

def squareArea(length):
return length * length

Calling with a parameter:

area = squareArea(5)

Here, 5 is a parameter.

Example:

def squareArea(n):
return n * n
side=int(input(“Enter a side:”))
area1=squareArea(side)
print(area1)
Library Programs
1. What is a Program Library?
A program library is:

●​ A collection of sub-routines (functions and procedures)​

●​ Already written by other programmers​

●​ Provided for you to use in your program​

You don’t need to know how the sub-routine works—only how to call it.

Example:​
Python's math library has ready-made functions such as:

●​ pi
●​ floor()
●​ ceil()

2. Why Do Program Libraries Exist?


Program libraries exist to:

✔ Save time

Programmers don’t need to rewrite common algorithms (for example, area of a circle uses π).

✔ Reduce errors

Library code has already been tested.

✔ Use advanced algorithms

Some tasks (AI, graphics, data science) are too complex for beginners to write.​
Libraries allow programmers to use them easily.

✔ Improve reliability
Library Programs
Many people use libraries, so errors are found and fixed quickly.

✔ Increase productivity

Programmers can build bigger, more useful programs faster.

3. Which Library Should I Use?


Different libraries are used for different purposes:

●​ math → mathematical functions​

●​ random → random numbers​

●​ Pandas → data handling​

●​ TensorFlow → artificial intelligence​

●​ NumPy → numerical data & arrays​

Every programming language has its own set of libraries.

4. How Do I Use a Library?


To use a library in Python:

💡 Step 1: Import the library


import math

💡 Step 2: Call functions from the library


[Link](25)
[Link](8.9)

Some libraries need installation, but math and random come built-in with Python.
Library Programs
⚠ Note:

Using libraries is NOT cheating.​


Libraries are meant to be used—but you still need to understand and write your own code.

5. Benefits and Drawbacks of Libraries


✔ Benefits

●​ Make programs faster to write​

●​ Well tested → reliable​

●​ Provide advanced tools​

●​ Prevent rewriting the same code​

❓ Needs checking
Even tested libraries can sometimes give unexpected results.

✖ Be careful when downloading

Some libraries may be unsafe (e.g., viruses).​


Use trusted sources only.
Library Programs
6. Python Libraries – Common Examples
Library Description

math Mathematical functions (pi, rounding, etc.)

random Generates random numbers

Pandas Data structures for tables

TensorFlo Machine learning and AI


w

NumPy Mathematical arrays and scientific computing

7. The Math Library in Python


To import:

import math

Functions included (you will learn these three):

●​ floor()​

●​ ceil()​

●​ pi
Library Programs
8. The floor() Function
✔ What it does

Rounds a number down to the nearest whole number.

Example:

[Link](22.6) → 22
[Link](4.55) → 4

✔ Syntax
[Link](number)

✔ Example in code
print([Link](22.6))

Output: 22

To store the returned value:

roundedNumber = [Link](100.2)

Example program:

import math
firstNumber = 2.6543
print("What is", firstNumber, "rounded down to the nearest whole
number?")
answer = int(input())
if answer == [Link](firstNumber):
print("Correct")
else:
print("Incorrect")
Library Programs
9. The ceil() Function
✔ What it does

Rounds a number up to the next whole number.

Example:

[Link](22.6) → 23
[Link](4.55) → 5

✔ Syntax
[Link](number)

✔ Example in code
print([Link](22.6))

Output: 23

To store the returned value:

roundedNumber = [Link](100.2)

Example program:

import math
firstNumber = 33.426
print("What is", firstNumber, "rounded up to the nearest whole
number?")
answer = int(input())
if answer == [Link](firstNumber):
print("Correct")
else:
print("Incorrect")
Library Programs
10. The pi Function ([Link])
Sometimes you need the value of pi (π) in calculations such as:

●​ Area of a circle​

●​ Circumference of a circle​

Python’s math library provides the value of pi as a constant.

✔ Syntax
[Link]

✔ Example
circleCircumference = [Link] * diameter

Note:

●​ No brackets after pi​

●​ It behaves like a number (≈ 3.14159)​

Using pi – Circle Calculations


Common formulas using pi:

Calculation How it works Equation

Area of a circle radius² × π πr²

Diameter 2 × radius 2r

Circumference 2 × radius × π 2πr

Example Program
import math
radius = 2
Library Programs
area = (radius * radius) * [Link]
print("The area is", area)

11. Random Numbers


A random number is a number chosen by chance.​
You cannot predict which number will appear next.

Importing the random Library

To use the random library:

import random

The randint() Function

randint() returns a random whole number between two limits.

✔ Syntax
[Link](lowest, highest)

Example
[Link](1, 6)

This returns any number between 1 and 6.

Both parameters must be whole numbers.


Library Programs
Examples of randint() Values
Function call Parameters Possible
outputs

[Link](1, 5) 1→5 1,2,3,4,5

[Link](0, 3) 0→3 0,1,2,3

[Link](11, 16) 11 → 16 11–16

[Link](-2, 1) -2 → 1 -2,-1,0,1

Example Program Using randint()


import random
numberToGuess = [Link](0, 3)
guess = int(input("What number am I thinking of?"))
if numberToGuess == guess:
print("Correct")
else:
print("No, the number is", numberToGuess)

This program:

●​ Picks a random number​

●​ Asks the user to guess it​

●​ Compares the guess to the random number


Library Programs
Summary of All math Functions Learned
Function Purpose Example Output

[Link](x) Round down [Link](4.9) 4

[Link](x) Round up [Link](4.1) 5

[Link] Value of π [Link] * r 3.14159 × r

[Link]( Random whole [Link]( 1–6


a,b) number 1,6)

You might also like