Introducing Python

Python is a powerful, dynamic, and interpreted programming language that is used in a wide variety of applications. Some of its features include:

  • A clear and readable syntax
  • A very extensive standard library, where through additional software modules, we can add data types, functions, and objects
  • Easy-to-learn rapid development and debugging; the development of Python code in Python can be up to 10 times faster than the C/C++ code
  • Exception-based error handling
  • A strong introspection functionality
  • Richness of documentation and software community

Python can be seen as a glue language. Using Python, better applications can be developed because different kinds of programmers can work together on a project. For example, when building a scientific application, C/C++ programmers can implement efficient numerical algorithms, while scientists on the same project can write Python programs that test and use those algorithms. Scientists don't have to learn a low-level programming language and a C/C++ programmer doesn't need to understand the science involved.

Note

You can read more about this from https://www.python.org/doc/essays/omg-darpa-mcc-position.

Getting ready

Python can be downloaded from https://www.python.org/downloads/.

Although you can create Python programs with Notepad or TextEdit, you'll notice that it's much easier to read and write code using an Integrated Development Environment (IDE).

There are many IDEs that are designated specifically for Python, including IDLE (http://www.python.org/idle), PyCharm (https://www.jetbrains.com/pycharm/), and Sublime Text, (http://www.sublimetext.com/).

How to do it…

Let's take a look at some examples of the very basic code to get an idea of the features of Python. Remember that the symbol >>> denotes the Python shell:

  • Operations with integers:
    >>> # This is a comment
    >>> width = 20
    >>> height = 5*9
    >>> width * height
    900
    

    Only for this first example, we will see how the code appears in the Python shell:

    How to do it…

Let's see the other basic examples:

  • Complex numbers:
    >>> a=1.5+0.5j
    >>> a.real
    1.5
    >>> a.imag
    0.5
    >>> abs(a) # sqrt(a.real**2 + a.imag**2)
    5.0
    
  • Strings manipulation:
    >>> word = 'Help' + 'A'
    >>> word
    'HelpA'
    >>> word[4]
    'A'
    >>> word[0:2]
    'He'
    >>> word[-1] # The last character
    'A'
    
  • Defining lists:
    >>> a = ['spam', 'eggs', 100, 1234]
    >>> a[0]
    'spam'
    >>> a[3]
    1234
    >>> a[-2]
    100
    >>> a[1:-1]
    ['eggs', 100]
    >>> len(a)
    4
    
  • The while loop:
    # Fibonacci series:
    >>> while b < 10:
    ... print b
    ... a, b = b, a+b
    ... 
    1
    1
    2
    3
    5
    8
    
  • The if command:

    First we use the input() statement to insert an integer:

    >>>x = int(input("Please enter an integer here: "))
    Please enter an integer here: 
    

    Then we implement the if condition on the number inserted:

    >>>if x < 0:
    ... print ('the number is negative')
    ...elif x == 0:
    ... print ('the number is zero')
    ...elif x == 1:
    ... print ('the number is one')
    ...else:
    ... print ('More')
    ...
    
  • The for loop:
    >>> # Measure some strings:
    ... a = ['cat', 'window', 'defenestrate']
    >>> for x in a:
    ... print (x, len(x))
    ... 
    cat 3
    window 6
    defenestrate 12
    
  • Defining functions:
    >>> def fib(n): # write Fibonacci series up to n
    ... """Print a Fibonacci series up to n."""
    ... a, b = 0, 1
    ... while b < n:
    ... print (b),
    ... a, b = b, a+b
    ... 
    >>> # Now call the function we just defined:
    ... fib(2000)
    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
    
  • Importing modules:
    >>> import math
    >>> math.sin(1)
    0.8414709848078965
    
    >>> from math import *
    >>> log(1)
    0.0
    
  • Defining classes:
    >>> class Complex:
    ... def __init__(self, realpart, imagpart):
    ... self.r = realpart
    ... self.i = imagpart
    ... 
    >>> x = Complex(3.0, -4.5)
    >>> x.r, x.i
    (3.0, -4.5)