Simple Python Programs

This article proposes a list of simple programs, written in Python. You can use them as samples, while learning to program with Python.

Among the numerous high-level programming languages we can choose from today, Python seems to be the most promising for those who wish to learn something new and have the real work done at the same time.

Python falls into object-oriented programming, which makes working out a problem almost as easy as writing down one's thoughts about the solution. Its super clear and easy-to-read syntax makes it a language that is fun to learn and use, which is not something we can freely say about most programming languages.

Programs, written in Python can run on almost any computer with no changes to the program. They are mostly text files, that can be edited with a standard text editor . Here is a list of simple programs, written in Python. You can use them as samples, while learning to program with Python

  1. Program, that prints the phrase 'Hello, World':
    >>>print 'Hello, world!'

     

    This is useful for testing things, just define new variables and then test to see if a particular line works correctly.

  2. Simple Math expression
    >>>8 + 4 * 2

    In this sample python program, the computer calculates 4 * 2 first, then adds 8 to it.

  3. Creating a phonebook
    >>>phonebook = {'John Smith':8806336, \ 
    'Will Herondale':6784346, \
    'Tessa Grey':7658344, \
    'Charlotte Wayland':1122345}

    Phonebook is essentially a dictionary. When you create a dictionary, elements should be in curly braces { } .
  4. Opening files
    >>>import glob
    # glob supports Unix style pathname extensions
    >>>python_files = glob.glob('*.py')
    >>>for file_name in sorted(python_files):
    print ' ------' + file_name
    with open(file_name) as f:
    for line in f:
    print ' ' + line.rstrip()
    >>>print

This is just scratching the surface when it comes to programming with Python. Python is powerful dynamic programming language, which is widely used for developing various web systems, including Plone, Pinax, Mezzanine, etc. For more information on our Python development services please contact us.

Connect with our experts Let's talk