Friday, January 30, 2015

Create Your Own Module

Example of a simple module.

Source Code - Module: example14.py

Source Code - Main Application: example15.py

Output: Python Shell

Wednesday, January 28, 2015

Module

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.

One of the example of a module is tkinter module. The tkinter module is a tool for creating gui programs in python.

Source Code: example12.py
Output: Python Shell - GUI
 Reference: www.tutorialspoint.com

Defining Function

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

As you may have known, Python already provide you a wide variety of built-in function such as print() and input(). But you yourself can create/define a function to use in your programs. This is called user-defined-funtion.

Source Code: example11.py
Output: Python Shell
Reference: www.tutorialspoint.com

Python Dictionary

A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values.

Source Code: example10.py
Output: Python Shell
 Reference: www.tutorialspoint.com

Tuple

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists use square brackets.

Source Code: example9.py
Output: Python Shell
 Reference: www.tutorialspoint.com

Python Lists

The list is a most versatile data type available in Python which can be written as a list of comma-separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type.

Source Code: example8.py
Output: Python Shell
 Reference: www.tutorialspoint.com

While Loop

While loops, like the for loop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is met.

Source Code: example7.py

Output: Python Shell
 Reference: wiki.python.org

For Loop


For loops are traditionally used when you have a piece of code which you want to repeat n number of times.

Source Code: example6.py
Output: Python Shell
 Reference: wiki.python.org