5 Things To Know To Advance Python Programming
By: Laxman Singh
Tags: Python, Learning, Python3, Technology, Programming

5 Things To Know To Advance Python Programming

Python is Interpreted, Interesting, Most Popular and Fun to learn New Generation Language

Python is a buzzword. Being most popular modern generation language with one of the biggest community of developers and capable of performing machine learning makes Python unique and should learn programming language. As per definition, it looks like the perfect programming language with all the ingredients. It has different libraries for covering different use cases, UI frameworks like flask, Django etc, machine learning libraries and visualization libraries.

1. Virtual Env setup

Virtual environment setup is very important to work on projects in python. It helps to distinguish and maintain different projects’ libraries, versions used for the given problem statement. It could be possible one of your project is running in different version of python, different version of python libraries. All can be achieved by simple using virtual env. Maintaining virtual environment is very simple and easy. It provides flexibility, portability and easy maintainability. Let us start creating virtual environment

# to install virtual env setup

pip install virtualenv

python -m venv <<envname>>

source <env>/bin/activate

After that, anything installed will get installed in activated virtual env. Jupyter notebook is also another best thing which allows to run python programs and see the output. Try installing that and see the magic!

2. Iterators, Generators & Decorators

Iterators: As a name suggest, something which allows to loop through and extract / manipulate values.

__next__ : returns the next item

__iter__ : returns the iterator

text = iter('Learning')



print(next(text))

print(next(text))

# output is first two characters

Generators: With the usage of yield keyword, function will return the value in terms of iterator or as and when required. It helps to boost the performance, return the result asap and return from the function could be sequence of elements. This is nice feature and can be exploited based on requirements

def function_name():

valueString = 'Learning'

for chars in valueString:

yield(chars)





retString = function_name()

print(next(retString))

print(next(retString))

#above two prints will return first two characters

Decorators: As a name suggests, they provide good information about the function and makes it easy to understand. These decorators can be parameterized and could be very useful in different use cases.

@staticmethod: defines function as static

@classmethod: defines method as class method

3. Important libraries and Use

Python has the biggest developer community and it means there is progressive development every minute or second. It means there are many libraries existing which can be utilized to suit your requirement. In simple, we can say there is no need to invent the wheel rather than utilize the existing resources.

os : It allows to perform generic statements related to operating system. Like, getting the current directory, changing the directory etc.

glob : This library allows to pick all the files using pattern and process the same. Very useful while implementing multiple dataset from different files.

numpy : As a name suggests, useful library for numeric calculations. It allows every kind of calculation in terms of matrices and multi dimensional arrays.

pandas : One of the most used libraries to read data from different format files like csv, tsv, txt, xls etc. It helps to convert read files in data frames. And, dataframes are in tabular format which can be joined and easily manipulative.

scikit-learn : Must learn library to begin your machine learning journey. It contains all the ingredients to train and evaluate the model

matplotlib : One of the most used library and must to learn to visualize data in different graphs. To surprise, one single command is capable of turning data into different graphs.

tensorflow / pytorch : This is machine learning libraries and could be very useful in implementing neural networks. These are fast, advanced and scalable to handle huge amount of calculations in quick manner.

4. Testing and Coverage

Testing is very important aspect of every programming language. Python has inbuilt libraries like unittest and thirdparty libraries like pytest and nose. Unittest library is more inspired by Junit and it gives almost same feel.

import unittest class Test_Example(unittest.TestCase):

def setUp(self):

#default function to define initial variable for all test cases



def test_case1(self):

self.assertEqual(call_test_code, exp_result) def test_case2(self):

self.assertEqual(call_test_code, exp_result) if __name__ == '__main__':

unittest.main()

As we see, we can define common variables in setUp and then write other functions starting with test keyword to test different scenariors.

python -m unittest test_File #it will run all the test cases in file

Alog-with the above options, you have various options to skip test cases conditionally and it allows to skip certain code so that coverage to the code can be increased. And, it has other options to calculate coverage for the code.

# below option is kept in front of function or code which need not be taken care in coverage

# pragma: no cover

5. Packaging

Once you have written a program or an utility, it is very easy in python to package it. Packaging your solution allows it to be installed by other as other existing packages like pandas, numpy etc. And, then your things can be utilized in a very simple manner using import statement. Sample setup.py for packaging your code. You can get more guidance in official documentation. It provides lots of options and flexibility. The more you use, you start feeling comfortable with this.

import setuptools

from setuptools import setup



setup(

name= 'ExampleCode',

version = '1.0',

description = 'Description of Your Package',

url = "github repository link",

author="Laxman Singh",

author_email="Mail id",

long_description_content_type="readme.md",

packages=setuptools.find_packages(),

classifiers=[

"Programming Language :: Python :: 3",

"License :: Not Applicable",

"Operating System :: OS Independent",

],

python_requires='>=3.7',

install_requires = ['sklearn','numpy'],

)

Packaging allows to have the flexibility of keeping all the dependencies for your program to be installed. It will make your solution robust and easy to implement by other programmers. This is very diverse in python and simplicity of packaging gives amazing feeling. It is an enjoyment for every programmer to implement their code with such a ease. A must try for every programmers.

Conclusions

Python is open source and highly utilized modern generation programming language. Alongwith the above features, python allows to have parallel programming, use of regular expressions and multi tasking. It also has some lightweight UI frameworks like flask which is very easy to implement and create easy to use API framework.

Overall, Python is inerpreted, high level and advanced new generation programming languages which is growing very fast. It is must learn programming language for all and we should let this come as unique gift to our knowledge area. I hope above things will help you to advance in your programming journey. Happy Coding!