Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.[20]
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.
-- [Wikipedia on Python](https://en.wikipedia.org/wiki/Python_(programming_language)
Scripting language created by Guido van Rossum
Published in 1991 (23 years ago)
Most places these days we know about use it for web programming.
Python is great at getting an application created quickly and cleanly
http://www.reddit.com/r/Python/comments/1dsv7m/what_is_python_not_a_good_language_for/
Everyone needs to go to http://python.org/download
C:\Python27
cd \py<tab>
apt-get
sudo apt-get update
sudo apt-get install python
python
Lets try the following in the REPL (read, evaluate, print loop)
print "hello world"
hello world
1+1
2
1/2
0
Notice for later: Not true division
1//2
0
print "hello " * 4
hello hello hello hello
item = [1,2,3,4,5,6]
print item
print type(item)
[1, 2, 3, 4, 5, 6] <type 'list'>
print item[3:4]
print item[:4]
print item[3:]
[4] [1, 2, 3, 4] [4, 5, 6]
myDict = {"key":"value", "name":"chris"}
print myDict
{'name': 'chris', 'key': 'value'}
print "My name is %s" % myDict['name']
My name is chris
if
Statements¶item = "5"
if item == 5:
print "It's 5"
elif item == "5":
print "It's %s" % item
else:
print "I don't know what it is."
It's 5
item = "5"
if item is not None:
print "item isn't None"
else:
print "item is None"
print "---\nSetting item to None\n---"
item = None
if item is None:
print "item is None"
item isn't None --- Setting item to None --- item is None
for
loops¶my_list = [1, 2, 3]
my_strings = ["things", "stuff", "abc"]
for item in my_list:
print "Item %d" % item
Item 1 Item 2 Item 3
for (index, string) in enumerate(my_strings):
print "Index of %d and value of %s" % (index, string)
Index of 0 and value of things Index of 1 and value of stuff Index of 2 and value of abc
print "Type of my_list is %s and the first element is %s" % (type(my_list), type(my_list[0]))
Type of my_list is <type 'list'> and the first element is <type 'int'>
print dir(my_list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
class
es¶class A_old():
pass
class A_new(object):
pass
print "Old type %s and New type %s" % (type(A_old), type(A_new))
Old type <type 'classobj'> and New type <type 'type'>
my_old_a = A_old()
my_new_a = A_new()
print "Old object %s and New object %s" % (type(my_old_a), type(my_new_a))
Old object <type 'instance'> and New object <class '__main__.A_new'>
In Python variables are more like "tags" than "boxes"
Normal ![](http://python.net/~goodger/projects/pycon/2007/idiomatic/a2box.png) ![](http://python.net/~goodger/projects/pycon/2007/idiomatic/b2box.png) | Python ![](http://python.net/~goodger/projects/pycon/2007/idiomatic/a2tag.png) ![](http://python.net/~goodger/projects/pycon/2007/idiomatic/ab2tag.png) |
item = [1, 2, 3]
item is item
True
item is item[:]
False
item == item[:]
True
Question and answer
import re
user = {}
user['name'] = raw_input("What is your name? ")
user['quest'] = raw_input("What is your quest? ")
user['will-get-shrubbery'] = raw_input("We want.... ONE SHRUBBERY. ")
user['favorite-color'] = raw_input('What is your favorite colour? ')
print '-'*60
accepted = re.compile(r"^((sure|SURE)|(y|Y).*)")
accepted_status = "will acquire"
if accepted.search(user['will-get-shrubbery']) is None:
accepted_status = "will not aquire"
print "%s is on a quest %s and %s a shrubbery. His favorite color is %s" % (user['name'].title(),
user['quest'].lower(), accepted_status , user['favorite-color'].upper())
What is your name?King Arthur What is your quest?To find the Holy Grail We want.... ONE SHRUBBERY.sure What is your favorite colour?blue ------------------------------------------------------------ King Arthur is on a quest to find the holy grail and will acquire a shrubbery. His favorite color is BLUE