10 Python Tips and Tricks for Beginner and 9th Is Interesting

Nov 28, 2016

5 mins read

Published in

Today Python is one of the most popular languages for coding in Unix/Linux world. You can find it everywhere from Web development to Server Component development, XML parsing to configuration management. Here I will be mentioning 10 Python tips and tricks for developers.

Python is pre-installed in many Operating Systems which released in last two year. Ubuntu, Redhat, Mac OS, Free BSD , Solaris, etc.

To check which version of python is installed go to shell and type:

$ python

This command will give you the version information of Python and start Python’s interactive interpreter.

Here are the few famous applications which are built using Python such as :

  • Youtube
  • OpenStak
  • Dropbox
  • Instagram

Looking for beginner python developer jobs? Check them here.

Here are 10 Python tips and tricks for Beginners and Experts.

1. Use Python Development Environment

By using Development Environment or IDE it will increase your productivity and easy to resolve error and warning. It will also help to debug the code, easy to navigate , integrate source control such as SVN, Git.

These are the recommended IDEs for Python Development.

  • PyDev – It’s free and Open Source Eclipse-based IDE . http://www.pydev.org/
  • IDLE: The Python built-in IDE, with auto-completion, helps to Format, Run, and Debug python code.
  • Other commercial versions are PyCharm(has Community version), WingIDE.

2. Calculate days between Today and Date.

1
2
3
4
5
6
7
8
9
import datetime

today = datetime.date.today()

someday = datetime.date(2017, 1, 1)

diff = someday - today

print (str(diff.days) +" days")

datetime module is used to find the days between two dates. this example will find the days between today and 1 Jan 2017.

3. Command line arguments while running Python Scripts

1
2
3
4
5
#file name is commandlinetest.py
import sys

print 'No of arguments passed:', len(sys.argv)
print 'List of arguments :', str(sys.argv)

run this script:

$ python commandlinetest.py 10 20 30

Output
No of arguments passed: 3<br /> List of argumenrs : ['commandlinetest.py', '10', '20', '30']<br />

sys module will have the information about arguments passed by accessing sys.argv variable. sys.argv is a list of strings representing the arguments on the command-line.

4. String Searching made easy

The first Method will check if a string contains another string by using the in syntax. in takes two arguments, first on the left and second on the right, and returns True if the left argument is contained within the right argument.

1
2
3
4
5
6
7
8
9
s = "This is a String finding program"

# Method 1
if 'String' in s:
    print('String is found in this program')

# Method 2
if s.find('String') != -1:
    print('String is at ' , s.index('String'))

Second Method offers a Library method called find(). This method is used with String Variable and it returns the position of the index of desired string. If the string is not found then -1 is returned.

5. Generating Random Numbers

Python is having the random library for generating random numbers. By default random library uses system time to generate random numbers.

1
2
3
4
5
6
7
import random

print('Random number:-&gt;' , random.randrange(10));

print('Random Range Between 10 to 20:-&gt;' , random.randrange(10,20));

print('Random with Floating values:-&gt;' , random.uniform(0,10));

Output :

Random number:-> 9
Random Range Between 10 to 20:-> 13
Random with Floating values:-> 5.012076169955613

random.randrange(10) sets the limit from 0 to 10 and return numbers between 0 to 10.

random.randrange(10,20) gives random values between 10 to 20.

random.uniform gives the result with floating value.

6. Opening WebBrowser from Python Script

1
2
3
import webbrowser

webbrowser.open('https://codebeautify.org')

webbrowser module provides the easy way to open system’s default browser with URL mention in open method.

7. Calculate Program Execution time

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import time

start_time = time.time()

add = 0
for i in range(1, 10000):
    add =add + i
print(add)

print("\n--- %s seconds ---" % (time.time() - start_time))

Many applications require a very precise time measurement. For this purpose, Python provides methods in time module. These methods will help you to measure execution time taken by the block of code.

In Linux and Unix, this command can let us know the execution of the python program.

$ time python nameofprogram.py

8. Converting the List to a String for Print

1
2
3
4
5
#declaring List
mylist = ['one', 'two', 'three']

print (', '.join(mylist))
print ('\n'.join(mylist))

To combine any list of strings into a single string, use the join method of a string object. First print statement will join string with ” , ” and second will add new line.

Output:

one, two, three
one
two
three

9. Strings Concatenating using ” + ” and more.

Python uses + and * from manipulating String.

Using +

1
2
3
$ python
>>> print "Jhon" + "Doe"
JhonDoe

Using *

1
2
3
$python
>>> print "Jhon" * 3
JhonJhonJho

10. Sorting Array of String

1
2
3
4
5
6
mylist = ['one', 'two', 'three', 'four', 'five']

mylist.sort()

for x in mylist:
    print (x);

Output:

five
four
one
three
two

Let me know your views and best Python tips in a comment below.

Sharing is caring!