Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

1

Preliminary Computer science

COMP0001

School of Science, Computing and Artificial Intelligence

Lecturer: Mr Ilenius Ildephonce

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 1 / 44


2

Lecture 8

Mathematical Functions, Strings, and


Objects

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 2 / 44


Contents
3

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 3 / 44


Content
4

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 4 / 44


Python Built-in functions
5

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 5 / 44


Built-in functions
6

Built-in functions
»> max(2, 3, 4) # Returns a maximum number
4
»> min(2, 3, 4) # Returns a minimu number
2
»> round(4.51) # Rounds to its nearest integer
4
»> round(4.4) # Rounds to its nearest integer
3
»> abs(-3) # Returns the absolute value
3
»> pow(2, 3) # Same as 2 ** 3
8

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 6 / 44


The math Functions
7

MathFunctions.py
Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 7 / 44
Problem: Compute Angles
8

Problem: Compute Angles

ComputeAngle.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 8 / 44


Content
9

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 9 / 44


Strings and characters
10

A string is a sequence of characters. String literals can be enclosed in


matching single quotes (’) or double quotes ("). Python does not have a
data type for characters. A single-character string represents a character.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 10 / 44


Unicode and ASCII Code
11

Python characters use Unicode, a 16-bit encoding scheme. Unicode is an


encoding scheme for representing international characters. ASCII is a small
subset of Unicode.
DisplayUnicodes.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 11 / 44


Functions ord and chr
12

Functions ord and chr


»> ch = ’a’
»> ord(ch)
»> 97
»> chr(98)
»> ’b’

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 12 / 44


Escape Sequences for Special Characters
13

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 13 / 44


Printing without the Newline
14

Printing without the Newline


print(item, end = ’anyendingstring’)

print("AAA", end = ’ ’)
print("BBB", end = ”)
print("CCC", end = ’***’)
print("DDD", end = ’***’)

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 14 / 44


The str Function
15

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 15 / 44


The String Concatenation Operator
16

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 16 / 44


Reading Strings from the Console
17

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 17 / 44


Case study:Minimum Number of Coins
18

This program lets the user enter the amount in decimal representing
dollars and cents and output a report listing the monetary equivalent in
single dollars, quarters, dimes, nickels, and pennies. Your program should
report maximum number of dollars, then the maximum number of
quarters, and so on, in this order.
ComputeChange.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 18 / 44


Content
19

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 19 / 44


Objects and Methods
20

In Python, all data—including numbers and strings—are actually objects.


An object is an entity.
Each object has an id and a type.
Objects of the same kind have the same type. You can use the id function
and type function to get these information for an object.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 20 / 44


Object Types and Ids
21

The id and type functions are rarely used in programming, but they are
good pedagogical tools for understanding objects.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 21 / 44


Object vs. Object reference Variable
22

Object vs. Object reference Variable


For n = 3, we say n is an integer variable that holds value 4.
Strictly speaking, n is a variable that references an int object for value 4.
For simplicity, it is fine to say n is an int variable with value 4.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 22 / 44


Content
23

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 23 / 44


Methods
24

You can perform operations on an object. The operations are defined


using functions.
The functions for the objects are called methods in Python.
Methods can only be invoked from a specific object. For example, the
string type has the methods such as lower() and upper(), which returns a
new string in lowercase and uppercase.
Here are the examples to invoke these methods:

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 24 / 44


str Object Methods
25

str Object Methods


»> s = "Welcome"
»> s1 = s.lower() # Invoke the lower method
»> s1
’welcome’
»> s2 = s.upper() # Invoke the upper method
»> s2
’WELCOME’

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 25 / 44


Striping beginning and ending Whitespace Charac-
26
ters

Another useful string method is strip(), which can be used to strip the
whitespace characters from the both ends of a string.
»> s = "\t Welcome \n"
»> s1 = s.strip() # Invoke the strip method
»> s1
’Welcome’

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 26 / 44


Testing Strings
27

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 27 / 44


Searching for Substrings
28

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 28 / 44


Converting Strings
29

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 29 / 44


Striping Whitespace Characters
30

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 30 / 44


Problem: Guessing Birthday
31

The program can guess your birth date. Run to see how it works.

GuessBday.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 31 / 44


Content
32

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 32 / 44


Formatting Numbers and Strings
33

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 33 / 44


Formatting Floating-Point Numbers
34

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 34 / 44


Formatting in Scientific Notation
35

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 35 / 44


Formatting as a Percentage
36

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 36 / 44


Justifying Format
37

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 37 / 44


Formatting Integers
38

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 38 / 44


Content
39

1 Python Built-in functions

2 Strings and characters

3 Introduction to Objects and Methods

4 Methods

5 Formatting Numbers and Strings

6 Drawing Various Shapes

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 39 / 44


Turtle Pen Drawing State Methods
40

Turtle Pen Drawing State Methods


A turtle contains methods for moving the pen and setting the pen’s size
and speed.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 40 / 44


Turtle Motion Methods
41

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 41 / 44


Turtle Drawing with Colors and Fonts
42

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 42 / 44


43

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 43 / 44


End of Week 9
44

End of Week 9.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 8 November 5, 2021 44 / 44

You might also like