SOFTWARE

FREEDOM

                       DAY

PYTHON WORKSHOP

IS THIS PYTHON???

FATHER OF PYTHON

Guido van Rossum

"Over six years ago, in December 1989, I was looking for a "hobby" programming project that would keep me occupied during the week around Christmas. My office ... would be closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus)."

WHAT IS PYTHON?

  • Python is a Open Source Programming Language.
  • Dynamic and high-level.
  • Unlike C/C++, it is an Interpreted Language.
  • Object Oriented.

Why Python?

  • Extremely Readable.
  • Developers prefer python, as they can then emphasis more on Logic then syntax.
  • A thriving community of developer and contributors and third party application developers.
  • Wide variety of Libraries like NumPy, SciPy, Turtle, Sklearn, PyGame, etc

 

 

 

 

Who all are using Python?

and the list goes on...

And now we will see the Magic...

No, we'll not get inside the computer!

 

We will see some cool Python Scripts.

  • pacman.py
  • send_text.py
  • open_the_url.py
  • search_your_location.py
  • email_sender.py

Now lets talk about Money...

This is the most recent data,

so, conclusion:

Python Rocks!!!

Chocolate Question

Lets Begin with Python...

Installing Python

1.  Go to http://www.python.org

2.  Click on Download

3.  Select the version, and download the setup file as according your Operating System, and Install.

"HELLO WORLD!"

How it's done in C and Java

#include<stdio.h>

int main()

{
    printf("HELLO WORLD!");
    return 0;
}

C

public class helloworld
{
    public static void main(String[] args)
    {
        System.out.println("HELLO WORLD!");
    }
}

Java

>>> print "HELLO WORLD!"

Pythonic Way

In python interpreter

In Python Script writer

print "HELLO WORLD!"

Open New File from Shell and write:

Now save the file as name.py

And execute it from command line as below:

$ python name.py

Difference between

Python Shell and Script Writer

VARIABLES AND DATATYPES

  • Identifiers
  • Keywords
  • Assignment
  • Operators & Expressions

IDENTIFIERS

So, while x_yz is a valid identifier, 9x_yz is not.

A name which can be used to identify a function, variable, module or class.

Uppercase letters: "A,B,..........,Z"

Lowercase letters: "a,b,..........,z"

Integers:"1,2,3................."

Special Character: "_"

KEYWORDS

and        del       from      not     while
as         elif      global    or      
assert     else      if        pass    
break      except    import    print
class      exec      in        with
continue   finally   is        return
def        for       lambda    try

ASSIGNMENTS

In Python, we don't declare what kind of data we want to put in a variable.

 >>> a = 20
 >>> b = 10
 >>> a + b
 >>> 30
 >>>
 >>> a**4
 >>> 160000  # a*a*a*a

Examples

MULTIPLE ASSIGNMENT

 >>> a,b = 10, 12 
 >>> print a
 10
 >>> print b 
 12 

Lets Try to Swap two numbers in Pythonic way.

DATATYPES

# Integer
>>> a = 10
# Float
>>> b = 2.35
# String
>>> c = "Hello"
# List
>>> d = [1, 2, 'blah', 3.2]
>>> d[2]
'blah'

Operators

  • Arithmetic: +, -, /, *, %, //
  • Relational: >, <, >=, <=, ==, !=
  • Logical: and, or
  • Shorthand: +=, /=, -=, *=

TYPE CONVERSION

  • float(string) -> float value
  • int(string) -> integer value
  • str(integer) or str(float) -> string representation
>>> a = 8.126768
>>> int(a)
8

If-else , the control flow

if expr1:
    do this
elif expr2:
    do that
...
...
...
else:
   do whatever 
>>> # Example
>>> x = 2
>>> if x==2:
        print 'YES'
    else:
        print 'NO'

YES
>>> # Therefore, YES is the output

FUNCTIONS

>>> def functionname(params):
        statement1
        statement2
        return something

Defining a Function

Example

>>> def addition(a,b):
        c = a + b
        return c

>>> print addition(20,30)
50 

LISTS AND STRINGS

LISTS

>>> a = [23, 45, 1, -3434, 43624356, 234] 

# Append
>>> a.append(45)
>>> a
[23, 45, 1, -3434, 43624356, 234, 45]

# Insert
>>> a.insert(0, 1) # 1 added at the 0th position of the list
>>> a
[1, 23, 45, 1, -3434, 43624356, 234, 45] 

>>> a.insert(0, 111)
>>> a
[111, 1, 23, 45, 1, -3434, 43624356, 234, 45]

# Count the length of the list
>>> a.count(45)
2  

# length
>>> print len(a)
9

# max
>>> max(a)
43624356

# slicing
>>> a[2:4]
23 45

STRING

>>> s = "I am Awesome "
>>> s
'I am Awesome '

>>> s = """ This is a
... multiline string, so you can
... write many lines"""
>>> print s
This is a
multiline string, so you can
write many lines 

>>> s = 'I am Awesome "
>>> print len(s)
12
>>>
>>> q = 'and I am Awesome'
>>> s+q
I am Awesome and I am Awesome

LOOPING

while condition:
    statement1
    statement2 



>>> n = 0
>>> while n < 11:
...     print n
...     n += 1
...
0
1
2
3
4
5
6
7
8
9
10 

WHILE LOOP

>>> a = ['Python', 'is', 'powerful'] 

>>> for x in a:
...     print x,
...
Python is powerful 

FOR LOOP

FOR ELSE

For...else...

>>> for i in range(0, 5):
...     print i
... else:
...     print "Bye bye"
...
0
1
2
3
4
Bye bye

FUN FACTS

>>> # Try This
>>> import antigravity
>>> # and this
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

NOT A CHANCE!!! :P

>>> from __future__ import braces
SyntaxError: not a chance (<pyshell#46>, line 2)

Here are some links, which you can use.

  • http://pythonpedia.com
  • http://pypi.python.org - Python Package Index
  • http://docs.python.org/2/tutorial/ - The “official” Python tutorial
  • http://wiki.python.org/moin/MostPopularPythonProjects - 
  • MostPopularPythonProjects
  • http://www.python.org/community/workshops/ - Python 
  • Conferences and Workshops
  • http://wiki.python.org/moin/ - The Python “MoinMoin” Wiki
  • http://docs.python.org/devguide/ - All about developing Python
  • http://mail.python.org/mailman/listinfo - A list of all Python

Questions???

SFD - Python

By sfd

SFD - Python

  • 1,435