CS 125 Lecture 9

Calling Builtin Functions

x = abs(y)
print(type(3.0))

Calling Functions From Modules

import math
print(math.sqrt(x**2 + y**2))
from math import sqrt, 
print(sqrt(x**2 + y**2))
n = trunc(x))
from math import *
print(sqrt(x**2 + y**2))

Classes

>>> type("abc")
<class 'str'>
>>> type(type("abc"))
<class 'type'>
>>> type(int)
<class 'type'>
>>> type(type(int))
<class 'type'>

Methods

>>> s = "ABCD"
>>> s.lower()
'abcd'
>>> s = "abcd"
>>> s.upper()
'ABCD'

Object Expressions

>>> ("abc" + "def").upper()
'ABCDEF'

String Methods

>>> s = "abc"
>>> s.center(15)
'      abc      '
>>> s = "Now is the time for everyone to get a covid vaccination."
>>> s.count("t")
5
>>> s.count("on")
2
>>> s = "image.pdf"
>>> s.endswith(".pdf")
True
>>> s.endswith(".jpg")
False
>>> s = "Now is the time for everyone to get a covid vaccination."
>>> s.find("t")
7
>>> s.find("ti")
11
>>> s.find("N")
0
>>> s.find("t", 10)
11
>>> s.find("t", 20, 30)
29
>>> s.find("t", 20, 25)
-1
>>> s = "abc"
>>> s.islower()
True
>>> s = "abd"
>>> s.ljust(10)
'abd       '
>>> s.rjust(10)
'       abd'
>>> s="image.png";
>>> s.replace(".png", ".pdf")
'image.pdf'
>>> s = "    foo   "
>>> s.strip()
'foo'

Chaining Method Calls

>>> s = "      foobar     "
>>> s.upper().replace("FOO", "xxx").strip()
'xxxBAR'

Special Methods

>>> (-3).__abs__()
3
>>> "abcd".__len__()
4