Episode #28 | How to get help from Python


Thursday, 16 May 2019 Kudzai Nyandoro 10:19 Minutes

Description:

In Episode #27, we learned about some of Python's values, types, and Reserved Keywords. In this episode, we are going to learn about how to use the Python interpreter and the built-in functions dir and help to learn more about Python.

Notes:

Python is an object-oriented language and everything in Python is an object. Objects are pieces of memory, collections of variables and functions that have values.

# Assign a string value to the variable called river.
>>> river = 'zambezi'
# Assign the value of 1 to the variable a
>>> a = 1
# Assign the value of 2 to the variable b
>>> b = 2
# Assign the value of the sum of a plus b to the variable c
>>> c = a + b
# Find out what type c belongs to
>>> type(c)
# A class is a blueprint used for creating objects
<class 'int'>
# Get the memory address of a
>>> id(a)
4561771696
# Get the memory address of b
>>> id(b)
4561771728
# Get the memory address of c
>>> id(c)
4561771760
>>> type(name)
<class 'str'>
  • We can use Python's built-in function dir to get help and learn more about the objects we are working with.

  • When we pass an object to the dir function, Python will return a list of all valid attributes available to the object.

  • We are going to ignore the attributes with the double underscores, also known as dunder or magic methods. Attributes with double underscores are beyond the scope of this tutorial.

  • Lets instead focus on attributes without the double underscores at the end. These attributes are functions we can call on the objects in question.

# Assign a string value to the variable called name.
>>> name = 'kudzai nyandoro'

# Get all the valid attributes available to the name object.
>>> dir(name)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
 '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 
'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 
'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

# Get help and learn more about what the upper function does.
>>> help(name.upper)

help on built-in function upper:

upper() method of builtins.str instance
    Return a copy of the string converted to uppercase.
(END)

# Use the upper function.
>>> name.upper()
'KUDZAI NYANDORO'

# Get help and learn more about what the title function does.
>>> help(name.title)
Help on built-in function title:

title() method of builtins.str instance
    Return a version of the string where each word is titlecased.

    More specifically, words start with uppercased characters and all remaining
    cased characters have lower case.
(END)

# Use the title function.
>>> name.title()
'Kudzai Nyandoro'
>>>

# Get help and learn more about what the find function does.
>>> help(name.find)

Help on built-in function find:

find(...) method of builtins.str instance
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
(END)

# Use the find function.

>>> name.find('k')
0
>>> name.find('r')
13
>>> name.find('0')
-1

  • The built-in functions dir and help accept objects or data type names as arguments.
# Create a variable
>>> name = 'kudzai nyandoro'
# Use dir to get all attributes that belong to name
>>> dir(name)
# Get help on how to use the upper function
>>> help(name.upper)
# Use dir to get all the attributes that belong to the string type
>>> dir(str)
# Get help on how
>>>help(str.upper)

Python Build-in Functions