Ten essential Python tips for beginners
6. Concatenating strings
You can use ‘+’ to concatenate strings like so:
>>> print ‘kun’+’al’
kunal
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(‘Kunal’)
p.sayHi()
[Output]
[~/src/python $:] python initmethod.py
Hello, my name is Kunal
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.
Do you have any essential Python tips beginners’ tips? Put them in the comments thread for other readers to make use of…
This is an excerpt of an article from Linux User & Developer. Click here to find more tutorials featured in the magazine.











“In Python you never have to explicitly specify the data type of anything.”
Not quite right. Python requires the type to be used consistently. For example, you cannot concatenate a string with a number using a string concatenation method.
Your various examples have left out a crucial part of the Python language that actually should have been one of the tips:
Flow control via indentation
And let’s not forget #11: Python creates blocks of code with indentation levels, and he examples in this article have been butchered by the blog engine, so copy & paste operations will actually fail. Not important for a pythonista, but at the beginner level, this *will* trip people up!
[...] is a vast language and there are many gems to discover. Following on from the popularity of our Ten essential Python tips for beginners article, we’ve compiled a further collection of ten gems to make your Python experience [...]
[...] it running everywhere, from various configuration tools to XML parsing. To compliment our ‘ten essential Python tips for beginners‘ and ‘ten more essential Python tips‘ features, we’ve compiled yet another [...]
Some examples are quite pointless without the proper indentation.
I hope we can see more high level lessons. Thank you.
Please be careful to note both the version of
python you are using and the reasons for not using
later version such as 3.1.2 which is recommended
for new projects. I am (sob,sob) trying to work cross
platform between Windows Vista 64 bit, Mac OS X
10.4, & various Ubuntu’s using 3.1.2. It is a real
pain to read through a site such as PyGt or yours only to
realize after two days it doesn’t work with 64 bit
python 3.1.2, only 32 bit.
What's your opinion?