Python — 4

Himashi Karunathilake
3 min readDec 5, 2019

--

Hi guys… 😍

In this post, we will continue our lesson about Python. I hope you guys have completed the previous tutorials and checked your answers and even done your own research too. If necessary, you can refer to the following links to revisit our previous chapters:
Python — 3 → https://himashikarunathilake.medium.com/python-3-493c1be17a06
Python — 2 → https://himashikarunathilake.medium.com/python-2-5d3b48c8da68
Python — 1 → https://himashikarunathilake.medium.com/python-1-ca8f34d2ee79

Functions

Functions can be used to reuse code segments at various times by simply calling the function name when and where necessary. These functions prevent repetition of code blocks.

In Python, a function is defined using the “ def “ keyword:

***** Functions may or may not have a list of parameters. *****

In order to execute a particular function, simply call its name when and where necessary as required.

Classes and Objects

In our introduction to this language, I mentioned that Python is an object — oriented programming language. Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from their respective classes, which are blueprints to create objects.

You can define a class as follows:

***** Here, the “self” keyword refers to the instance of the class. *****

You can create an object from this class as follows:

Now, if you want to access the variable or the function of this class, you can do it as follows:

A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. The values can be accessed using a key, which is any type of object instead of using its index.

Refer the following example on how to define and use a dictionary:

The above example can also be coded as follows:

If it is needed to iterate over the values of a dictionary, you can follow the given example:

To delete an entry, you can use the following methods:

*** Note that, dictionaries, unlike lists, do not keep the order, the values were stored in it. ***

So guys, that’s it for this post. I still hope to bring you a little more information about Python in the future although this Python series is coming to an end. The tutorial necessary for this post can be accessed in the folder Python — 4 in https://github.com/Himashi-Karunathilake/Python . Please try out the tutorial and check your answers from the script provided. Until I see you in the next one then… Happy Coding!

Ciao… 👋

Originally published at http://mysnowfrostpersonal.wordpress.com on December 5, 2019.

--

--

Himashi Karunathilake
Himashi Karunathilake

Written by Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!

No responses yet