Monday, January 20, 2014

10 Basic Tips For Python Newbies

We all know that Python is one of the most sought after programming language in the open source space. It is one language that lets you work very quickly and integrate your system more effectively. So, if you have just stepped into Python, we bring to you ten essential Python tips that will help ease out your journey in this world.

Python, Programming, Python tips, 10 tricks, python tips and tricks, python tricks, Linux, open source, python coding, python programming language




1. Running Python scripts

On major of the UNIX systems, you can run Python scripts from the command line using folowing command.

$ python mypyprog.py

2. Running Python programs from Python interpreter

The Python interactive interpreter is used interchangeable with Python shell. This is where the Python commands can be typed and executed. This makes it easy to try your first steps in programming and using all Python commands. You just issue each command at the command prompt, one by one, and the answer is immediate.

Python interpreter can be started by issuing the command:

$ python
atithya@ubuntu:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

In this article, all the code starting with the >>> symbol are meant to be given at the Python prompt. It is also important to remember that Python takes tabs very seriously – so if you are receiving any error that mentions tabs, correct the tab spacing.

3. Dynamic typing

In Java, C++, and other statically typed languages, you must specify the data type of the function return value and each function argument. On the other hand, Python is a dynamically typed language. In Python, you never have to explicitly specify the data type of anything. Based on what value you assign, Python will keep track of the data type internally.

4. Python statements

Python uses carriage returns to separate statements, and a colon and indentation to separate code blocks. Most of the compiled programming languages, such as C and C++, use semicolons to separate statements and curly brackets to separate code blocks.

5. == and = operators

Python uses ‘==’ for comparison and ‘=’ for assignment. Python does not support inline assignment, so there’s no chance of accidentally assigning the value when you actually want to compare it.

Today, Python is one of the most popular programming languages in the open source space. It’s a vast language and there are many gems to discover – here are ten tips for new users to get the ball rolling…

6. Concatenating strings

You can use ‘+’ to concatenate strings like so:

>>> print 'atit'+'hya'
atithya

7. The __init__ method

The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. The __init__ method is analogous to a constructor in C++, C# or Java.

[Example]
class Person:
def __init__(self, name):
self.name = name
def sayHi(self):
print ‘Hello, my name is’, self.name
p = Person(‘Atithya’)
p.sayHi()

[Output]
[~/src/python $:] python initmethod.py
Hello, my name is Atithya

8. Modules

To keep your programs manageable as they grow in size, you may want to break them up into several files. Python allows you to put multiple function definitions into a file and use them as a module that can be imported into other scripts and programs. These files must have a .py extension.

[Example]
# file my_function.py
def minmax(a,b):
if a <= b:
min, max = a, b
else:
min, max = b, a
return min, max
Module Usage
import my_function
x,y = my_function.minmax(25, 6.3)

9. Module defined names

The built-in function ‘dir()’ can be used to find out which names a module defines. It returns a sorted list of strings.

[Example]
>>> import time
>>> dir(time)
[‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘accept2dyear’, ‘altzone’, ‘asctime’, ‘clock’, ‘ctime’, ‘daylight’, ‘gmtime’, ‘localtime’, ‘mktime’, ‘sleep’, ‘strftime’, ‘strptime’, ‘struct_time’, ‘time’, ‘timezone’, ‘tzname’, ‘tzset’]

10. Module internal documentation

You can see the internal documentation (if available) of a module name by looking at .__doc__.

[Example]
>>> import time
>>> print time.clock.__doc__
clock() -> floating point number

This example returns the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.

No comments:

Post a Comment