Monday, 14 September 2015

DEVELOPING ANDROID APPS COMPLETELY IN PYTHON

The goal of this training is to show you how to start developing full Android applications using only Python. Different technologies will be demonstrated, including PySide-based QML GUIs using the Necessitas Qt port and the Py4A/SL4A-based approach, which can be combined with Android’s WebKit and re-use Python web frameworks.
While Android already has a good SDK out of the box, being able to use Python instead of Java is a big advantage for some developers - it allows for quicker turnaround times, and reuse of Python libraries. Python on Android uses a native CPython build, so its performance and compatibility is very good. Combined with PySide (which uses a native Qt build) and Qt’s support for OpenGL ES acceleration, you can create fluent UIs even with Python.
The resulting PySide-based applications run on Android, but also at least on Mac OS X, Windows, Linux and Maemo and MeeGo - basically all platforms on which Qt is available. The SL4A/Py4A-based applications will run on Android only, but will be able to utilize Android-specific APIs. Both approaches can also be combined.

Thursday, 16 July 2015

Hi viewers this is our python symbol.
Make use of it!!!!!!!!!!!!!!!!!!!!!

Python Hangman Game

Overview

This is a Python script of the classic game "Hangman". The word to guess is represented by a row of dashes. If the player guess a letter which exists in the word, the script writes it in all its correct positions.  The player has 10 turns to guess the word. You can easily customize the game by changing the variables.

Hangman Script

Make sure that you understand what each line does.
#importing the time module
import time

#welcoming the user
name = raw_input("What is your name? ")

print "Hello, " + name, "Time to play hangman!"

print "\n"

#wait for 1 second
time.sleep(1)

print "Start guessing..."
time.sleep(0.5)

#here we set the secret
word = "secret"

#creates an variable with an empty value
guesses = ''

#determine the number of turns
turns = 10

# Create a while loop

#check if the turns are more than zero
while turns > 0:         

    # make a counter that starts with zero
    failed = 0             

    # for every character in secret_word    
    for char in word:      

    # see if the character is in the players guess
        if char in guesses:    
    
        # print then out the character
            print char,    

        else:
    
        # if not found, print a dash
            print "_",     
       
        # and increase the failed counter with one
            failed += 1    

    # if failed is equal to zero

    # print You Won
    if failed == 0:        
        print "\nYou won"  

    # exit the script
        break              

    print

    # ask the user go guess a character
    guess = raw_input("guess a character:") 

    # set the players guess to guesses
    guesses += guess                    

    # if the guess is not found in the secret word
    if guess not in word:  
 
     # turns counter decreases with 1 (now 9)
        turns -= 1        
 
    # print wrong
        print "Wrong\n"    
 
    # how many turns are left
        print "You have", + turns, 'more guesses' 
 
    # if the turns are equal to zero
        if turns == 0:           
    
        # print "You Loose"
            print "You Loose\n"  
Enjoy it!!

Friday, 19 June 2015

Getting Started With Python Programming

Installing Python

Python is a very useful and popular computer language. Confusingly, Python has two major versions (2 and 3) and they are not fully compatible. We recommend using the most recent release of version 3. (This is the version that our Introduction to Programming with Python course uses -- if you are enrolled in that class, you must have Python 3.) There is absolutely nothing wrong with Python 2, as it is what most of today's technology supports and uses, but Python 3 is well on the way of replacing Python 2, so it will be more useful in a few years.
Python is open-source software and is free to install and use. Here are installation instructions:
  1. Go to the Python download page at http://www.python.org/download. Near the top of the page, there will be a list of download links for the Python 3.4.* installer. (The * will be replaced by a number -- as of mid-March 2015 the version is 3.4.3.) Click on the link that corresponds to your computer type (Windows or Mac, 32-bit or 64-bit -- if you're not sure, use the 32-bit version.) Some browsers will save the file automatically, others may pop up a box asking you if you want to save the file, in which case you should click the "save file" option. Depending on how your browser is configured, you may be asked where to save the file. If this is the case, keep track of where you save the installer.
  2. Find where the installer was downloaded and double click on it to run it. On most browsers, you should simply be able to double-click the installer from the browser's "Downloads" window or menu. You may also have to click "Run" or "Yes" to a security window -- do this if necessary.
  3. The setup wizard should launch. You should just click "Next" for every option in the setup wizard (i.e. use the defaults), unless you have some specific reason not to.
  4. Familiarize yourself with the Python shell and IDLE text editor by running through the two sections below.

Using the Python Shell

The program that you'll use to run Python is called IDLE. It may be listed on your computer as "IDLE (Python GUI)".
  • On a Mac, IDLE should be in the Applications folder.
  • On Windows, IDLE should be accessible from the Start menu in a folder named "Python 3.4" (or something similar).
The icon for IDLE looks something like this Idleicon.png or this Idleiconmac.png
When you first open IDLE, you'll see the Python Shell (the numbers on your shell might be different than those shown below):
Idle2-1.png
(The screenshots in this article are taken using IDLE on a Mac with the font increased. Thus IDLE may look a little bit different for you but should still function similarly.)
Note that the first line is the version of Python, which is 3.1.2 in the screenshot but should be 3.4.something if you installed it as directed above. Another thing to note is that in the lower right hand corner of the Python Shell you can see that it says "Ln: 4 Col: 4". This is just telling you where in the document your cursor is. In this case it's on line 4 and over in column 4. (The line and column number may be slightly different for your installation.)
When you first start up Python on a Mac, you might get the following warning:
>>> WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information.
If you get this warning, you'll need to update a graphics driver on your computer. Follow the link shown above and download and install the ActiveTcl driver that's recommended for the version of OS X that your Mac is running. This most likely will be 8.5.15.0, which you can also download directly from http://www.activestate.com/activetcl/downloads (IMPORTANT: you only need to do this step if you get the warning printed above when you start IDLE for the first time. If you don't get the warning, then everything is good to go.)

The Python Shell is very useful for quick math and short sequences of commands:
Idle2-2.2.png
Here we see a number of familiar operations: + for addition, - for subtraction, * for multiplication, and / for division. The last operation shown in the example, denoted by **, happens to be exponentiation. One neat feature to note about Python is that it can store arbitrarily large numbers (limited by the amount of memory your computer has). Trying some hefty exponentiation, we can see that we can compute with some pretty big numbers such as $2^{1000}$ as illustrated below.
Idle2-3.png
While Python can make for a pretty good calculator, it can do a whole lot more. One example is when dealing with strings as follows:
Idle2-4.png
Here we are concatenating the three strings "python", "is", and "cool" by using the + operator. Notice that previously we used + to add numbers but now with strings, Python concatenates them! You may also note that the output of the operation gives us a string with single quotes around it. In Python, you are able to use single quotes or double quotes to denote a string. You can use them interchangeably.
As a final example, we can even write code in the Python Shell that extends beyond a single line as shown below. We also see our first example of a $\verb=for=$ loop.
Idle2-5.png
As you type the above, the Python Shell will automatically indent the second line for you. To let the Python Shell know that you're done and are ready for it to run your code, you'll need to put in an extra blank line by hitting the Enter key again. At that point it should run your code and print your output.
Take some time to play around with the Python Shell. You'll want to go through a more extensive introduction to programming to learn the full extent of what you can do with Python, but you can still do some pretty nifty stuff by just playing around. The Python Shell also has an extensive built-in help system -- just type help() at the ">>>" prompt to get started and then follow the instructions it gives you.

The IDLE Text Editor and Your First Python Program

For most programming needs, you'll want to edit your program in a separate document and then run it. Happily, IDLE comes with its own built-in text editor.
To get started, go to the File menu of the Python Shell and click on "New Window". This should give you a blank document with the title "Untitled" as shown below:
Idle2-6.png
You'll need to save your file before running it, so you might as well save it now. Make sure that you name your file with a name that ends in .py, so that your computer knows it is a Python program. Here we save ours as test.py:
Idle2-7.png
To get acquainted with the text editor, let's write our first Python program! Let's write a program to the following task:
Find the sum of all the positive multiples of 3 below 1000.
We solve this by keeping a running total: we'll start with the smallest positive multiple of 3 and go up one multiple at a time keeping track of the sum, stopping once we hit 1000. We can do this with the following code:
Idle2-8.png
Notice that as you type the above code, the keywords ("while" and "print") will automatically get colored -- this makes the code easier to read. Also, after typing the line "while i < 1000:", the editor will automatically start indenting for you. When you get to the line "print(total)", you'll need to use the backspace key to remove the indentation. It is important that the code look exactly like it does in the screenshot above: in Python, proper indentation is very important!
This program basically works by incrementing $\verb=i=$ by 3 every time and adding it to the $\verb=total=$. The $\verb%+=%$ notation might be intimidating at first. However, the statement $\verb%i+=3%$ is just a shorthand for $\verb%i = i + 3%$.
Now that we've written this code, we probably want to run it and test it out. We can do so by going to the Run menu and hitting Run Module (shortcut F5). The program should execute and print out the answer to the Python Shell:
Idle2-9.png
The RESTART line just means that Python is clearing all the work you've previously done before it starts running your program. Then, the program runs and we get our answer, 166833. If instead you get an error message or a different answer, check that your program exactly matches the screenshot above, and try it again.

Python Program To Generate Diamond Pattern

Hey there! Trying your hands on Python programming? Here's a simple program to illustrate the concept of loops in python. The resulting diamond can be generated with slightly different versions of the program. Also you can alter the shape and size of the diamond according to your wish. You can do so by adding spaces in the #'s to make it more cleaner or increase or decrease the range of the loops to make the diamond shape bigger or smaller.
But the central idea will remain the same. Here's the code -

def run():
    j = 7
    k = 7
    p = 1
    for i in range(8):
        print " " * k," #" *  i
        k -=1
    while j > 1:
        j -= 1
        print " " * p," #" * j
        p +=1
run()



You can copy and paste this code into your code editor and run it. Python supports three kinds of loops namely for, while and if. In this program i have used for and while. Python doesn't includes periods or semicolons to indicate the end of the line. Also, there are no braces to initiate or end the loops. Python uses intends to indicate the beginning and end of the loop. So, unlike other programming languages like C, C++ or Java intends are important. You can use TAB to shift the code or use 2 spaces which is the standard for coding at Google. If you are using a code editor like Eclipse as i do, it will take care of the intends for you. You can look at the coding and output snapshots made in Python using Eclipse IDE below-

Code for Generating Diamond Pattern in Python Programming
Coding - Generate Diamond Pattern

Output of the Diamond Pattern Generating Program in Python
Output - Diamond Pattern using Python Code