Category: Python

Python Data Structures Idioms

Significant portion of our time we as a developers spend writing code that manipulates basic data structures: traverse a list, create a map, filter elements in a collection. Therefore it is important to know how effectively do it in Python and make your code more readable and efficient.

Continue reading

Generators in Python

In previous articles I’ve wrote about how to create an iterator in Python by implementing iterator protocolor using the yield keyword. In this article I’ll describe generators: a piece of Python syntax that can turn many iterators into one-liners.

Continue reading

Efficient Iterators in Python

While you can write iterators in Python by implementing iterator protocol it usually requires a lot of code and looks cumbersome. To facilitate this task Python provides a powerful syntax to create iterators. By using these constructions we can write complex iterators using just few lines of code.

Continue reading

Anatomy of a Python Iterator

Iterator is a powerful pattern that was recognised at least as early as 1994 and since then it was incorporated in syntax of almost every modern programming language.

Python also implements this pattern providing a pithy and concise syntax to iterate over lists, maps, dictionaries and other data structures:

for i in [1, 2, 3, 4]:
    print i

In this article I will write about how an iterator is used in Python, how to implement your own iterator and what types of iterators exist in Python.

Continue reading

Python in One Hour. Part 2

This is the second part of the “Python in 1 hour” tutorial. It will go into more advanced Python features that will help you to develop complex and robust applications.

The only prerequisite for this article is that you should be familiar with the content of the part of this tutorial that you can find here.

Continue reading

Python in One Hour. Part 1

This is a relatively short and concise article that will give you all that you need to know to start reading/writing Python code. It will start with defining a variable and will discuss types, data structures, if-else statements, loops, and functions.

This tutorial does not require any prior knowledge of Python. The only prerequisite for this article is knowledge of basics of any other object-oriented programming language.

Most of the examples in this article are applicable in both Python 2 and Python 3. I point to differences between Python versions where applicable.

Continue reading

How to implement string interpolation in Python

String interpolation is a process of substituting values of local variables into placeholders in a string.

It is implemented in many programming languages such as Scala:

//Scala 2.10+
var name = "John";
println(s"My name is $name")
>>> My name is John

Perl:

my $name = "John";
print "My name is $name";
>>> My name is John

CoffeeScript:

name = "John"
console.log "My name is #{name}"
>>> My name is John

and many others.

On the first sight, it doesn’t seem that it’s possible to use string interpolation in Python. However, we can implement it with just 2 lines of Python code.

Continue reading

© 2017 Brewing Codes

Theme by Anders NorenUp ↑