top of page

8 ways to use Python Programming


Python is really a language which has swept the scene in recent years in terms of popularity, elegance, and functionality. Research shows that 8 out 10 computer science departments in the U.S. now teach their introductory courses with Python, surpassing Java. Top-ranked CS departments at MIT and UC Berkeley have switched their introductory courses to Python. And the top three MOOC providers (edX, Coursera, and Udacity) all offer introductory programming courses in Python. Not to mention, Python is known to have an easy syntax that makes learning much more tolerable, and it scales up easily – especially in cloud-based environments.

As powerful as Python is, there’s always room for improvement. If you want to make your Python code even faster and more efficient, then stay tuned. Below we’ve listed 7 ways to improve the performance of your Python environment. Read on!

1. Use some of Python’s “speedup” applications

Python has developed a reputation as a solid, high-performance language. Lots has been done in recent years to get to this point. The PyPy project aims to speed up Python as a whole (and is doing a great job of it). And Numba is another tool that can offer amazing speedups by implementing high performance functions written directly in Python.

2. Using generators & sorting with keys

Generators are helpful in memory optimization because they allow you to create a function that returns one item at a time rather than all the items at once. A good example would be when you’re creating a large list of numbers and adding them together.

Also, when you’re sorting items in a list, be sure to use keys and the default sort() method whenever possible. In the following, notice that in each case the list is sorted according to the index you select as part of the key argument. This approach works just as well with strings as it does with numbers:

import operator

somelist = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]

somelist.sort(key=operator.itemgetter(0))

somelist

#Output = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]

somelist.sort(key=operator.itemgetter(1))

somelist

#Output = [(6, 2, 4), (1, 5, 8), (9, 7, 5)]

somelist.sort(key=operator.itemgetter(2))

somelist

#Output = [(6, 2, 4), (9, 7, 5), (1, 5, 8)],

3. Using the latest releases of Python

Python is maintained through a community of developers who are committed to keeping the software current and robust. Each new release of the language is technically going to be faster and more optimized than before, so it’s a good idea to plan your move. Just be sure that your favorite supporting libraries are compatible with the newest versions.

4. Avoid unwanted loops

The consensus is out that too much looping in any programming language is not a good thing, and puts unnecessary strain on your server. Some simple tweaks like storing the length of an array in a different variable instead of making it read the length at every iteration of the loop, can go a long way to optimizing your code and ensuring things run much more efficiently. Also, try refactoring your code to use intersections and unions. For example, instead of this:

for x in a:

for y in b:

if x == y:

yield (x,y)

Try this instead:

return set(a) & set(b)

5. Try out multiple coding approaches

It’s typical to employ the same general coding techniques throughout your application. But why not try a little experimentation to see if one technique is better or more optimal than another. This will help keep you sharp and innovative in your approaches to coding, no matter what language you’re working in. By learning to “think outside the box” you’ll creatively apply new coding techniques to obtain faster results with your applications.

6. Keep Python code small and light

It is often the case in programming that simplest is fastest. In this day and age performance is critical and it’s especially important to keep your Python code as compact as possible to reduce latency and speed things up. One article provides some organizing questions that are worth asking in the development stage: “Do we really need this module?”, “Why are we using this framework? Is it worth the overhead?”, “Can we do this in a simpler way?”

7. Cloud-based application performance monitoring

Your Python-based applications and websites are only as good as the performance of your infrastructure, which is why application performance is too important to leave to chance. What this requires is the best-in-class monitoring capability for all your IT systems. That’s exactly what you’ll find in Monitis. As the trusted leader in this market, Monitis offers a 24/7 cloud-based monitoring platform for websites, servers, applications, and networks. Through an easy to follow and intuitive web-interface, Monitis tracks all of your business-critical applications to ensure everything is running optimally and that you’re alerted to issues long before your customers are.

8. "White Hat" Hacking & Cyber Security

Python is an easy to learn language which can be helpful to penetration testers to create their custom tools which they can use to achieve coverage. Thus plugging in holes which are at times created by vulnerability scanners because they are unable to hit certain pages due to one or the other reason. Users can create reusable code by using python’s object orientation which can help them create classes that can be inherited and extended. Python can not only be used for quick and dirty scripting to achieve small automation tasks but also be used to create enterprise class vulnerability scanning routines.

Example SQL Injection Script:

import urllib

import urllib2

location = "http://test_target.site/login.aspx"

values = {"username":"'","password":"password","btnSubmit":"Login"}

data = urllib.urlencode(values)

req = urllib2.Request(location,data)

response = urllib2.urlopen(req)

page_data = response.read()

print page_data

Recent Posts 
Serach By Tags
No tags yet.
bottom of page