Official website for Linux User & Developer
FOLLOW US ON:
Jan
22

Ten essential Python tips for beginners

by Kunal Deo

Python is a programming language that lets you work more quickly and integrate your systems more effectively and is one of the most popular programming languages in the open source space. Look around and you will find it running everywhere, from various configuration tools to XML parsing. We’ve compiled ten essential Python tips especially for the new user, so without further ado, let’s get started…

1. Running Python scripts
On most of the UNIX systems, you can run Python scripts from the command line like so:

$ python mypyprog.py

2. Running Python programs from Python interpreter
The Python interactive interpreter 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
kunal@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.
>>> <type commands here>

In this article, all the code starting at the  >>> symbol is 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.

Pages: 1 2

7 Comments »

  • Wayne said:

    “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.

  • Chris said:

    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

  • Roland said:

    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!

  • Ten more essential Python tips | Linux User said:

    [...] 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 [...]

  • Another ten essential Python tips | Linux User said:

    [...] 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 [...]

  • chris said:

    Some examples are quite pointless without the proper indentation.

  • Annlat said:

    I hope we can see more high level lessons. Thank you.

What's your opinion?

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.