This is a tutorial series which will teach you how to code in Python. We will start with the absolute basics of installation of Python in Windows and Python Tools in Visual Studio. We will then go through basic constructs in Python and write a couple of programs to summarize what we have learned. We will end with an Object Oriented Approach using Python and a specific feature of Python Tools in Visual Studio: Mixed mode C/C++/Python Debugging
Part 1: Get Started with Python summarized the steps involved to setup Python and Visual Studio for Python Development. We essentially learned how to install Python in Windows, Visual Studio and Python Tools for Visual Studio.
Part 2: Get Started with Python took you through the basics in programming constructs such as output, input, variables and control flow statements including conditional statements and loops including while and for. Using these tools was enough to get started with coding basic applications in Python.
Part 3: Functions and File Handling
Welcome to this weeks edition of Get Started with Python! In this section, let us take a deeper dive. We will be looking into Functions and File Handling. After having gone through this, we will develop an application using Functions and Files to demonstrate the ease of use and utility of these constructs.
Functions are blocks of code which are logically grouped to perform some action. In Python, a function is defined using the ‘def’ keyword, followed by the name of the function, followed by the list of parameters in parenthesis, followed by ‘:’. The distinctive feature in Python being that the block of code is not delimited by brackets or parenthesis, but by an indentation of a tab space. The first line is an optional line in quotes which is called as a ‘docstring’ and can be used by third party tools for documentation purposes. The code snippet below gives a brief description of the syntax and the usage.
As most other procedural languages, the most important object in File handling is the File Object handler, the difference being that there is no need to import or include any additional libraries. We get the file object handler using the ‘open’ construct. The file can be opened in either read, write or append mode. We use the ‘write’ construct to write contents in the file while we use the ‘read’ construct to read contents from the file. After we finish our operations with the file, we close the file using the ‘close’ construct. The following code snippet shows the syntax and the usage.
Now that we have gone through function and file handling, let us look into how both of these can be used to build an application. We will first define the objective of the application, explain the design which will be followed by the code.
Expand regularly used short-forms in text message jargon. For eg: lol, rofl and lmao will be expanded to “laughing out loud”, “rolling on the floor laughing” and “laughing my a** out” respectively.
def mainfunction() : This will make the call to all the other functions
def filewrite(): This will write contents to the file
def filecheck(): Will open and check the validity of the existence of the file
def separatewordlist(): separate the contents of the file into words
def dictionarylist(): creates 2 lists, one containing the words and the other contains the expansion
def compare(): compare the final list of words
def printMessage(): prints the final message
Find the details of the code of the app:
The output of this application on execution looks as follows:
Understanding and using files and functions is key to the development for any application. Now you are definitely equipped with enough arrows in your quiver to start developing your applications on Python. In the next part, I will cover an object oriented approach to programming in Python which will include classes and objects. So stay tuned and hope to see you there !