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

Day 4 Python Standard Library

Tutorial for python standard library

Uploaded by

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

Day 4 Python Standard Library

Tutorial for python standard library

Uploaded by

Chit Su
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Python Standard Library

Python is lib based programming

• Python 3
Standard Libraries in Pythons
• The core Python programming language comes with functions such as max()
and sum() and classes such as int, str, and list.
• In addition to the core functions and classes, Python has many, many more
functions and classes defined in the Python Standard Library.
• The Python Standard Library consists of thousands of functions and classes
organized into components called modules.
• Each module contains a set of functions and/or classes related to a particular
application domain.
• More than 200 built-in modules together form the Python Standard Library.
• Each module in the Standard Library contains functions and classes to support
application programming in a certain domain.
Standard Libraries in Pythons

• . The Standard Library includes modules to support, among others:


• Network programming
• Web application programming
• Graphical user interface (GUI) development
• Database programming
• Mathematical functions
• Pseudorandom number generators
Module math

• The core Python language supports only basic mathematical operators.


• For other mathematical functions such as the square root function or the
trigonometric functions, the math module is required.
• The math module is a library of mathematical constants and functions.
• To use a math module function, the module must first be explicitly imported:
• >>> import math
• The import statement makes available all the math functions defined in module
math.
Module math
• Listed are some functions and constants in the module:
h=sqrt(a^2+b^2)
Practice Problem
• Write Python expressions corresponding to the following:
• (a) The length of the hypotenuse in a right triangle whose other two
sides have lengths a and b h=sqrt(a^2+b^2)
• (b) The value of the expression that evaluates whether the length of
the above hypotenuse is 5
• (c) The area of a disk of radius a
• (d) The value of the Boolean expression that checks whether a point
with coordinates x and y is inside a circle with center (a, b) and
radius r
Module fractions

• The fractions module makes available a new type of number: the Fraction
type.
• The Fraction type is used to represent fractions and do rational arithmetic,
such as:

• To use the fractions module, we first need to import it:


• >>> import fractions
Module fractions
Python Programs
Python Program

• A Python program that implements a computer application is a sequence of


multiple Python statements.
• This sequence of Python statements is stored in one or more files created by
the developer using an editor.
Our First Python Program
Function print()
• >>> print(0)
0
• >>> print([0, 0, 0])
[0, 0, 0]
• >>> print('zero’)
zero
• >>> x = 0
• >>> print(x)
0
Interactive Input with input()
• Often an executing program needs to interact with the user.
The input() function is used for that purpose. It is always
used on the right side of an assignment statement, as in:
• x = input('Enter your first name: ‘)
• >>> name = input('Enter your first name: ‘)
• Enter your first name: Ljubomir
• >>> name 'Ljubomir'
String Data Type and input() Method
String Data Type and input() Method
• We just saw that when the input function is called, whatever the user types
is treated as a string. Let’s check what happens when the user enters a
number:

• The Python interpreter treats the value entered as a string '5', not integer 5.
We check this:
Adding Two Numbers

• num1=input (‘Enter first number : ‘)


• num2=input (‘Enter second number : ‘)
• result= int(num1) + int(num2)
• print ( num1 + ‘+’+num2+ ‘is’ + str(result))
Special Note
The input() function will
always treat whatever the
user types as a string.
Function eval()

• The function eval() takes a string as input and evaluates


the string as if it were a Python expression. Here are
some examples:
Function eval() and input()
• The function eval() can be used together with the function input() when we
expect the user to type an expression (a number, a list, etc.) when
requested.
Practice Problem
Execution Control
Structures
Execution Control Structures

• Computer applications usually do different things depending on the values


input.
• For example, the game you just finished playing may stop or continue
running, depending on whether you click on the Exit or the Play Again
button.
• We now introduce Python statements that can control which statements
are executed and which statements should be executed repeatedly.
One-Way Decisions
• Suppose we want to develop a program that asks the user to enter the
current temperature and then prints an appropriate message only if it is
more than 86 degrees. This program would behave as shown if the user
enters 87:
if statement

• The if statement is used to implement conditional execution.


One Way
Decision
if <condition>:
<indented code block>
<non-indented statement>
Practice Problem

• Translate these conditional statements into Python if statements:


• (a) If age is greater 62, print 'You can get your pension benefits’.
• (b) If name is in list ['Musial', 'Aaraon', 'Williams', 'Gehrig', 'Ruth'], print 'One
of the top 5 baseball players, ever!’.
• (c) If hits is greater than 10 and shield is 0, print 'You are dead...’.
• (d) If at least one of the Boolean variables north, south, east, and west is
True, print 'I can escape.'.
In a one-way decision if
statement, an action is
performed only if a condition is
true.

Then, whether the condition is


Two-Way true or false, execution resumes
with the statement following the
Decisions if statement.

In other words, no special action


is performed if the condition is
false.
Two-Way
Decisions
if <condition>:
<indented code block 1>
else:
<indented code block 2>
<non-indented statement>
Practice Problem

• Translate these into Python if/else statements:


• (a) If year is divisible by 4, print 'Could be a leap year.'; otherwise print
'Definitely not a leap year.’
• (b) If list ticket is equal to list lottery, print 'You won!'; else print 'Better luck
next time...'
Practice Problem

• Implement a program that starts by asking


the user to enter a login id (i.e., a string).
The program then checks whether the id
entered by the user is in the list ['joe', 'sue',
'hani', 'sophie'] of valid users. Depending on
the outcome, an appropriate message
should be printed. Regardless of the
outcome, your function should print 'Done.'
before terminating. Here is an example of a
successful login:

You might also like