Keywords & Functions in Python

toni thinker
3 min readMay 3, 2021
The Zen of Python, the philosophy to python programming.
The Zen of Python.

So it’s week two of my journey and i was introduced to keywords and functions in python, and with that being said here’s a brief warning as we proceed…

DISCLAIMER : This read is going to be a bit more technical and more on the educational side than my last piece so let’s grab our learning hats, shall we?! *smiley face* I promise to keep it simple and use easy to understand words ;).

Keywords are the reserved words in Python. In python, they are case sensitive. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.

There are about 33 keywords in python 3.7, and i’ll be going through a handful of them below.

  • as
  • return
  • finally
  • lambda

The “as” clause in python is used to create an alias. That is, import datetime as A, now we can refer to datetime using A as the identifer instead. The “as clause is used in the following contexts:

  • Accompanied by the with statement to name the resource being allocated
  • In the fromimport statement and the import statement as a means to bind the module to a name
  • In the except statement to associate a name to the exception being passed.

The return is used to end the execution of the function and return the result (value of the expression following the return keyword) to the caller. Expressed in this format | return [expression]|

finally is used with exceptions, a block of codes that will be excuted no matter if there is an exception or not. This is useful to close objects and clean up resources.

lambda is used to create a small anonymous function, can take up any number of arguments but can only have one expression.

side note: an argument is the value that are sent to the function when it is called, and not the typical “give reasons or cite evidence in support of an idea, action, or theory, typically with the aim of persuading others to share one’s view.” Right, here’s an expression of this keyword.

|lambda argument : expression|

|A = lambda B: B+10|

|Print(A(8))|

this will result in 18 printed to IDLE.

and that’s that on keywords, now let’s jump into functions and see how some work.

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. And i’ll be going through five (5) unique functions: oct(), ord(), repr(), str() and type().

So straight to it then, oct() function converts an integer into an octal string. Octal strings in Python are prefixed with 0o.

oct (int)

x = oct(200)

print (x)

which results in 0o310

The ord() function in Python accepts a string of length 1 as an argument and returns the unicode code point representation of the passed argument.

sidenote: Unicode is a computing standard for the consistent encoding symbols.

print(ord(‘5’)) # 53
print(ord(‘A’)) # 65
print(ord(‘$’)) # 36

By the way, the ord() function is the inverse of the Python chr() function.

In Python, rep() function returns a printable representation of the given object. __repr__ is a special method used to represent a class’s objects as a string. __repr__ is called by the repr() built-in function. You can define your own string representation of your class objects using the __repr__ method.

The str() function of Python returns the string version of the object. str() and repr() both are used to get a string representation of object.

type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.

Two different types of arguments can be passed to type() function, single and three argument. If single argument type(obj) is passed, it returns the type of given object. If three arguments type(names, bases, dict) is passed, it returns a new type object.

Syntax :

type(object)

type(names, bases, dict)

And this is where we bring today’s read to an end. There’s a lot to cover on this topic and i dare say not enough time to further dive in but as required by my instructor, i hope this was an insightful read and we learnt something new today. So until next time, keep learning.

--

--