Use Python Lists Like a Pro

Image for post
Image for post
Photo by Corinne Kutz on Unsplash

In this article, I will try to explain Python lists, along with exploring why and when to use them, meanwhile giving you some hints about the correct usage of the list methods.

Let’s understand the Python list data structure in detail with step by step explanations and examples.

What are Lists in Python?

Lists are one of the most frequently used built-in data structures in Python. You can create a list by placing all the items inside square brackets[ ], separated by commas. Lists can contain any type of object and this makes them very useful and versatile.

Fundamental characteristics of Python lists are as follows; They are mutable, ordered, dynamic and array type (sequence) data structures. …


With easy to understand examples in 5 Minutes!

Image for post
Image for post
Photo by Joshua Aragon on Unsplash

Generators are special functions that return a lazy iterator which we can iterate over to handle one unit of data at a time. As lazy iterators do not store the whole content of data in the memory, they are commonly used to work with data streams and large datasets.

Generators in Python are very similar to normal functions with some characteristic differences listed below;

  • Generator functions have yield expression, instead of return used in normal functions.
  • Both yield and return statements return a value from a function. …


Use Python Dictionaries Like a Pro

Image for post
Image for post
Photo by freestocks on Unsplash

Python programming language is widely used by developers in data science projects. To complete such projects, understanding data structures plays an important role. Python has several built-in data structures such as lists, sets, tuples, and dictionaries, in order to support the developers with ready to use data structures.

In this article, I will try to explain why and when to use Python dictionaries, meanwhile giving you some hints about the correct usage of the dictionary methods.

Let’s understand the Python dictionaries in detail with step-by-step explanations and examples.

What is a Python Dictionary?

In a nutshell, a dictionary can be defined as a collection of data stored in key/value pairs. Keys must be an immutable data type (such as string, integer or tuple), while values in a dictionary can be any Python data type. …


With easy to understand examples!

Image for post
Image for post
Photo by Zach Kadolph on Unsplash

When your Python code grows in size, most probably it becomes unorganised over time. Keeping your code in the same file as it grows makes your code difficult to maintain. At this point, Python modules and packages help you to organize and group your content by using files and folders.

  • Modules are files with “.py” extension containing Python code. They help to organise related functions, classes or any code block in the same file.
  • It is considered as a best practice to split the large Python code blocks into modules containing up to 300–400 lines of code.
  • Packages group similar modules in a separate directory. They are folders containing related modules and an __init__.py file which is used for optional package-level initialisation. …


With Function and Operator Overloading

Image for post
Image for post
Photo by Nolan Marketti on Unsplash

Overloading in Python allows us to define functions and operators that behave in different ways depending on parameters or operands used.

Operator Overloading

As an example, we can use “+” operator to do arithmetic calculations on numerical values while the same “+” operator concatenates two strings when strings operands used. This is called operator overloading and it allows us to use the same operator on different object types to perform similar tasks.

As shown below, we can overload “+” operator to use it with our custom-made object types as well.

# No overloading, task is performed by 'add' method
cost1 = Cost(10)
cost2 = Cost(24)
cost_total = cost1.add(cost2) …


With easy to understand examples!

Image for post
Image for post
Photo by Josie Josie on Unsplash

Generators are special functions that return a lazy iterator which we can iterate over to handle one unit of data at a time. As lazy iterators do not store the whole content of data in the memory, they are commonly used to work with data streams and large datasets.

Generators in Python are very similar to normal functions with some characteristic differences listed below;

  • Generator functions have yield expression, instead of return used in normal functions.
  • Both yield and return statements return a value from a function. …


With easy to understand examples!

Image for post
Image for post
Photo by Zach Kadolph on Unsplash

When your Python code grows in size, most probably it becomes unorganised over time. Keeping your code in the same file as it grows makes your code difficult to maintain. At this point, Python modules and packages help you to organize and group your content by using files and folders.

  • Modules are files with “.py” extension containing Python code. They help to organise related functions, classes or any code block in the same file.
  • It is considered as a best practice to split the large Python code blocks into modules containing up to 300–400 lines of code.
  • Packages group similar modules in a separate directory. They are folders containing related modules and an __init__.py file which is used for optional package-level initialisation. …


Understand the basics with a concrete example!

Image for post
Image for post
Photo by Alexandru Acea on Unsplash

There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.

  • Syntax errors are similar to grammar or spelling errors in a Language. If there is such an error in your code, Python cannot start to execute your code. You get a clear error message stating what is wrong and what needs to be fixed. Therefore, it is the easiest error type you can fix.
  • Missing symbols (such as comma, bracket, colon), misspelling a keyword, having incorrect indentation are common syntax errors in Python.
  • Exceptions may occur in syntactically correct code blocks at run time. When Python cannot execute the requested action, it terminates the code and raises an error message. …


Understand the basics with a concrete example!

Image for post
Image for post
Photo by Matthew Fournier on Unsplash

When your Python code grows in size, most probably it becomes unorganised over time. Keeping your code in the same file as it grows makes your code difficult to maintain. At this point, Python modules and packages help you to organize and group your content by using files and folders.

  • Modules are files with “.py” extension containing Python code. They help to organise related functions, classes or any code block in the same file.
  • It is considered as a best practice to split the large Python code blocks into modules containing up to 300–400 lines of code.
  • Packages group similar modules in a separate directory. They are folders containing related modules and an __init__.py file which is used for optional package-level initialisation. …


Understand the basics with a concrete example!

Image for post
Image for post
Photo by Joshua Aragon on Unsplash

In Python, abstract base classes provide a blueprint for concrete classes. They don’t contain implementation. Instead, they provide an interface and make sure that derived concrete classes are properly implemented.

  • Abstract base classes cannot be instantiated. Instead, they are inherited and extended by the concrete subclasses.
  • Subclasses derived from a specific abstract base class must implement the methods and properties provided in that abstract base class. Otherwise, an error is raised during the object instantiation.

Let’s write a Python3 code that contains simple examples of implementing abstract base classes:

from abc import ABCMeta, abstractmethodclass AbstactClassCSV(metaclass = ABCMeta):

def __init__(self, path, file_name):
self._path = path
self._file_name …

About

Erdem Isbilen

Machine Learning and Data Science Enthusiasts, Automotive Engineer, Mechanical Engineer, https://www.linkedin.com/in/erdem-isbilen/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store