Python Modules, Strings, and Functions

Hello and welcome to this weeks blog posting. This evening we’re going to discuss a couple of topics regarding the Python coding language; modules, strings, and functions. Following a brief discussion on these topics, we’ll then look at a couple of examples via code.

Python modules are simply collections of multiple Python functions, variables, and even code; wrapped into a centralized package. Once you have a module defined, you’re able to reference that module internally within your working file by simply executing it, or via importing (if externally available; e.g. CDN, NFS, or even on your local machine within a different directory), and executing it.

Python functions are blocks of code typically designed to perform a single task or operation. Functions are generally utilized by programmers to be reusable code to perform a repeatable action. For example if you had a need to multiply a integer by 2 more than a single time, you may want to consider creating a simple function for this. This applies two important concepts in programming; keeping D.R.Y. and less maintenance intensive coding. D.R.Y. Don’t Repeat Yourself, the practice of ensuring you write code once and reuse it. This goes directly into less intensive maintenance. If you were to have more than a single location within your program that performs the same task or operation, when those blocks of code are being modified, you’ll have to always remember to modify in both places.

Below is a simple program that utilizes multiple functions, in fact these happen to be nested. The program takes an input, converts to an integer, and multiplies the input integer by two.

Python string data types are like any other string data type in programming or databases. They’re simple to understand, can be manipulated into array’s, lists, dictionaries, and tuples to name a few. Additionally, there are quite a few accessor methods that are available from string data types. From concatenation, substringing, length, formatting, getBytes, indexOf, hashCode, etc…

Below is simple program that utilizes string interpolation to accept an input string, modify an existing string, and return the manipulated string to the user.

That wraps up this weeks blog post, for a copy of the above code and other examples in Python, please visit my GitHub page: https://github.com/joshsnyder/python-data-science

Leave a comment

Design a site like this with WordPress.com
Get started