Python can be used for data manipulation, analyze data, graphic & visualization, build website, maintain servers etc.
Python useful links:
Setup instructions - http://bil.ly/intro-setup
Python documentation - https://docs.python.org/2/tutorial/
Frameworks for Python:
Web framework - Django (www.djangoproject.com)Graphing - Matplotlib (www.matplotlib.org)
Games - pygame (www.pygame.org)
Key differences from Java
- Interpreted vs compiled
- No data type assignment to variables.
- 4 white spaces vs paranthesis
Code Samples
Containment check
If you want to check if 'hello' contains letter 'h' you can write below code
>>> 'h' in 'hello'
True
>>>
If / Else:
Sample 1:
>>> sister_age = 15
>>> brother_age = 12
>>> if sister_age > brother_age:
... print "sister is older than brother"
... else:
... print "brother is older than sister"
...
sister is older than brother
>>> brother_age = 12
>>> if sister_age > brother_age:
... print "sister is older than brother"
... else:
... print "brother is older than sister"
...
sister is older than brother
>>>
Last line in above sample code is the output of the code execution of the code block above it.
Sample 2:
>>> temperature = 70
>>> if temperature > 60 and temperature < 75:
... print "Just right!"
... else:
... print "EXTREME!!"
...
Just right!
>>> if temperature > 60 and temperature < 75:
... print "Just right!"
... else:
... print "EXTREME!!"
...
Just right!
>>>
Sample 3:
>>> sister_age = 15
>>> brother_age = 15
>>> if sister_age > brother_age:
... print "sister is older than brother"
... elif sister_age == brother_age:
... print "same age!"
... else:
... print "brother is older than sister"
...
same age!
>>> brother_age = 15
>>> if sister_age > brother_age:
... print "sister is older than brother"
... elif sister_age == brother_age:
... print "same age!"
... else:
... print "brother is older than sister"
...
same age!
>>>
Notes:
Pass keyword - pass is a null operation — when it is executed, nothing happens.
It is useful as a placeholder when a statement is required syntactically, but no
code needs to be executed, for example:
>>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
def f(arg): pass # a function that does nothing (yet)
class C: pass # a class with no methods (yet)
For online interactive learning sign up to - http://www.codecademy.comThen go to http://bit.ly/py-practice to do some assignments.
Lists
Python documentation - https://docs.python.org/2/tutorial/introduction.html#lists
Python knows a number of compound data types, used to group together other
values. The most versatile is the list, which can be written as a list of
comma-separated values (items) between square brackets. Lists might contain
items of different types, but usually the items all have the same type.
Sample 1:
>>> my_list = ['a', 'b', 'c', 'd']
>>> print my_list
['a', 'b', 'c', 'd']
>>> len(my_list)
4
>>> my_list[0]
'a'
>>> my_list[-1]
'd'
>>> my_list[-2]
'c'
>>>
>>> print my_list
['a', 'b', 'c', 'd']
>>> len(my_list)
4
>>> my_list[0]
'a'
>>> my_list[-1]
'd'
>>> my_list[-2]
'c'
>>>
Negative index means counting from end of the list. So, my_list[-1] indicates last element of the list and my_list[-2] indicates second last element of the list.
Sample 2: Append to list:
>>> my_list.append("e")
>>> my_list
['a', 'b', 'c', 'd', 'e']
>>> my_list
['a', 'b', 'c', 'd', 'e']
Sample 3: Get index of an element
>>> my_list.index('c')
2
>>>
2
>>>
Sample 4: Containment
>>> 'a' in my_list
True
>>> 'z' in my_list
False
>>>
True
>>> 'z' in my_list
False
>>>
Sample 5: Slicing list
>>> my_list
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
>>> my_list[0:2]
['a', 'b']
>>> my_list[1:3]
['b', 'c']
>>> my_list[1:4]
['b', 'c', 'd']
>>> my_list[:3]
['a', 'b', 'c']
>>> my_list[2:]
['c', 'd', 'e']
>>> my_list[:]
['a', 'b', 'c', 'd', 'e']
>>>
['a', 'b']
>>> my_list[1:3]
['b', 'c']
>>> my_list[1:4]
['b', 'c', 'd']
>>> my_list[:3]
['a', 'b', 'c']
>>> my_list[2:]
['c', 'd', 'e']
>>> my_list[:]
['a', 'b', 'c', 'd', 'e']
>>>
Sample 6: Sort
>>> names = ["Deepak", "Aditya", "Jasica", "Garima"]
>>> names
['Deepak', 'Aditya', 'Jasica', 'Garima']
>>> names.sort()
>>> names
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>>
>>> names
['Deepak', 'Aditya', 'Jasica', 'Garima']
>>> names.sort()
>>> names
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>>
Sample 7: Max, min
>>> numbers = [2, 0.5, -1, 89, 44, 1000]
>>> max(numbers)
1000
>>> min(numbers)
-1
>>>
>>> max(numbers)
1000
>>> min(numbers)
-1
>>>
Loops
Sample 1: For Loops
>>> names
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>> for name in names:
... print "-" + name
...
-Aditya
-Deepak
-Garima
-Jasica
>>>
>>> names
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>> for name in names:
... print "-" + name
...
-Aditya
-Deepak
-Garima
-Jasica
>>>
>>> names
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>> for name in names:
... if name[0] in "AEIOU":
... print name + " starts with vowel"
... else:
... print name + " does not start with vowel"
...
Aditya starts with vowel
Deepak does not start with vowel
Garima does not start with vowel
Jasica does not start with vowel
>>
['Aditya', 'Deepak', 'Garima', 'Jasica']
>>> for name in names:
... if name[0] in "AEIOU":
... print name + " starts with vowel"
... else:
... print name + " does not start with vowel"
...
Aditya starts with vowel
Deepak does not start with vowel
Garima does not start with vowel
Jasica does not start with vowel
>>
Sample 2: building list using loops
>>> vowel_names = []
>>> for name in names:
... if name[0] in 'AEIOU':
... vowel_names.append(name)
...
>>> vowel_names
['Aditya']
>>>
>>> for name in names:
... if name[0] in 'AEIOU':
... vowel_names.append(name)
...
>>> vowel_names
['Aditya']
>>>