‹ Kinyanjui Wangonya

Tags / python


What happens when a variable is assigned to another variable in python? For example:

>>> x = 5
>>> y = x

Both x and y will have the value 5. But, when x was assigned to y, y was not created as a completely new/separate object. Instead, an alias for x was created. That is, y points to the memory location of x. It does not have it’s own memory location - yet.

>>> id(x)
140428600776960
>>> id(y)
140428600776960
>>> x is y
True

The range() function is often useful when iterating over a set of integers:

for n in range(50):
    ...

#

for n in range(10, 30):
    ...

or a list of strings:

for fruit in ["apple", "mango", "banana"]:
    ...

Say we have a list of strings: _list = [...,] and user input _input = '...', how do we find the items in _list that most closely resemble _input?

Python has a built-in package called difflib with the function get_close_matches() that can help us.

Note: * represents a required parameter.

join()

Parameters

string.join(iterable)

iterable*: a python iterable e.g a list, tuple, string, dictionary or set.

Return value

Returns a string concatenated with the elements of the passed in iterable.

capitalize()

Parameters

string.capitalize()

capitalize() takes no parameters.

Return value

Returns a copy of the string with the first letter of the first word capitalized and all the other characters of the string in lowercase.

Note: * represents a required parameter.

find()

Parameters

string.find(substring, start, end)

substring*: the substring to be searched in the string.

start: index from which to begin the search.

end: index at which to end the search.

Return value

Returns an integer value pointing to the index of first occurrence of the substring. If the substring is not found, it returns -1.

A lot of times when I’m doing ifs in Python, I find myself wondering whether to use is or == for the check.

# do I do
if a is b:
    ...

# or
if a == b:
    ...

It can be a bit confusing if you’re new to Python, and it’s easy to assume the two can be used interchangeably. So, what’s the difference?

If you’re just creating a package for learning purposes, there’s no need (IMO) to pollute the official PyPi index with it. Also, you may want to release a package to a select number of users for testing before you actually release it officially. For these purposes, you’re better off using TestPyPi:

a separate instance of the Python Package Index that allows you to try distribution tools and processes without affecting the real index.

It’s good practice to, as much as possible, write tests for your code. If you’re working with Python, pytest makes the process of writing and running tests much smoother. I wrote a few posts some time back on getting started with testing with pytest, so if you’re completely new to it, you might want to take a look at them:

For testing CLI apps, Click provides a convenient module: click.testing which has some useful functions (notably CliRunner()) to help us invoke commands and check their behavior.

We’ll go ahead and test each part of our app - creating, reading, updating and deleting.

For the next phase in this series, we’ll learn how to make a CLI app to perform CRUD operations, i.e, Creating, Retrieving, Updating and Deleting data. We’ll use Firebase to store our data.

« Older posts Newer posts »