Category Archives: Python 101

How to solve a string “splosion” problem in Python using for loop

First of all, what is a string “splosion”. Let’s consider a non-empty string, “hello” for example. You will need to return a string like “hhehelhellhello”.

def stringSplosion(initial_string):
	resulted_string = ""
	for s in range(len(initial_string)):
		resulted_string = resulted_string + initial_string[:s+1]
	return resulted_string

So, we declare a function called stringSplosion which takes in an argument, the string initial_string.
Within the function we create an empty string which is going to be our result.
Now, we start iterating over the initial string. Every time we iterate we add the characters from the beginning of the string up to the s plus 1.

Continue reading

How to open, read, and write text files in Python

Let’s say that you need to open, read, and write text files in Python. First you can use the open() built-in function to open the file. Once the file is opened you can use the two built-in methods read() and write() to read from the file and to write to the file.

>>> opened_file = open('file.txt')
>>> the_file_content = opened_file.read()
>>> the_file_content
'Hi there! I am a .txt file!'

The opened_file is a file descriptor object. In the above example, the read() method reads the entire content of the file. If you wish, you can read just a portion of the file, as is shown below:

Continue reading

How to copy files using shutil module in Python

The shutil module contains many functions but two of them can be used to copy files from one location to another. To demonstrate this, I created two text files as shown in the image below:

copy files with shutil

You can copy the content of the file1.txt to the file2.txt. This way, both files will have the same content but they will still have their original names.

>>> import shutil
>>> new_file_path = shutil.copy('file1.txt', 'file2.txt')

Continue reading

How to create classes of iterators using itertools module in Python

The itertools module provides iterators that you can use in your projects. One of the commonly used method is accumulate.

>>> import itertools
>>> accumulator = itertools.accumulate(range(10))
>>> next(accumulator)
0
>>> next(accumulator)
1
>>> next(accumulator)
3
>>> next(accumulator)
6
>>> next(accumulator)
10
>>> next(accumulator)
15

Continue reading

How to iterate over data that has no iterator in Python

Data structures in Python are almost entirely already iterable. However, sometimes you do need a generator for the cases when the data is not iterable.

A generator is just a function that yields back to the section where it was called from. This is a signal for Python that you want to create a generator therefore it will automatically save the state of the function at the point of the yield statement. At this point you can return to it when next() is called.

A classic example will be a generator that generates the sequence of squares.

>>> def squares(value = 5):
... 	while True:
... 		value = value + 1
... 		yield (value - 1) * (value - 1)
... 
>>> generator = squares()
>>> next(generator)
25
>>> next(generator)
36
>>> next(generator)
49

Continue reading

How to iterate over the contents of a file in Python

Many times you need to work with files in Python. Let’s say that you have to iterate over the content of a file for further processing. You can use for this task the open function which returns a file object that can be iterated over line by line.

First create a text file and name it file.txt for example. I placed it on my desktop. Below is the content of the file:

Line 1
Line 2
Line 3

Continue reading

How to extract and filter the content of an iterator in Python

Let’s say that you have an iterator and you need to list all the containing elements within it. In order to do that you can use the enumerate() function which will take an iterable object and return a list of tuples (count + value).

>>> bikes = ['Honda', 'Yamaha', 'Suzuki']
>>> bike_enum = enumerate(bikes)
>>> next(bike_enum)
(0, 'Honda')
>>> next(bike_enum)
(1, 'Yamaha')
>>> next(bike_enum)
(2, 'Suzuki')

Continue reading

How to iterate over the content of a list in Python

Let’s say that you need to iterate over the content of a list. In order to do that you need to use the iter function that allows you to get access to the associated iterator.

>>> list_one = [1, 2, 3, 4]
>>> iterator_one = iter(list_one)
>>> iterator_one

Continue reading

How to get the current date and time in Python

Let’s say you need the current date and time from your system to place it in a Python application. For example to place the date and time as a timestamp in a blog application so the date and time when a blog article was posted will be visible on the screen.

For that, we need to import a module called datetime which contains a class with the same name datetime.

>>> import datetime
>>> current_datetime = datetime.datetime.now() 
>>> print(current_datetime)
2017-12-11 08:18:18.685000

Continue reading