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

Ten essential Python tips for beginners

by Kunal Deo

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…

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
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • Wayne

      “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

      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

      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!

    • Pingback: Ten more essential Python tips | Linux User

    • Pingback: Another ten essential Python tips | Linux User

    • chris

      Some examples are quite pointless without the proper indentation.

    • http://www.annlat.com Annlat

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

    • http://none Frank Townsend

      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.

    • frank

      I’ve already install version 3.2.1 on my computer, but version 2.6 runs as default, and i don’t know how to set 3.2.1 as default. Pls help me!

    • http://itreinvent.com/ Morpheus

      The Beauty of Python is in the ample modules available. it makes the coding really a fun.