This is the presentation I used to teach the first class of Python SIG (Special Interest Group) which is a class for interested students taught by students. This is not meant to be used as standalone material, rather, it is meant to point you in a useful direction. If you are new to Python, and know another programming language, I hope this will be helpful to you.
4. hello world!
• The traditional first program
• In Python 2:
–print ‘hello world!’
• In Python 3:
–print(‘hello world!’)
5. 2 vs 3
• 2.7 last in 2 series
• 3 under development
• Ultimately, not much difference
• “The most drastic improvement is
the better Unicode support (with all
text strings being Unicode by
default)” [1]
6. 2 vs 3
• Difference in print – statement,
NOT function
• Libraries, libraries, libraries
7. Why 2 then?
• Tools & support – 2to3, future
module
• Libraries, libraries, libraries. Example:
Web development
• Easy to learn the other
9. What is code?
• Any fool can write code that a computer can
understand. Good programmers write code
that humans can understand.
Martin Fowler, "Refactoring: Improving the
Design of Existing Code" [2]
10. Objects, objects everywhere!
• “Everything in Python is an object, and almost
everything has attributes and methods. All
functions have a built-in attribute __doc__,
which returns the doc string defined in the
function's source code.” [3]
From Dive Into Python by Mark Pilgrim
• help(int)
11. Python implementations
• Language and implementation – separate
issues
• Language == syntax
• “An "implementation" of Python should be
taken to mean a program or environment
which provides support for the execution of
programs written in the Python language” [4]
From wiki.python.org
13. CPython
• Interpreted; with “bytecode” in between (like
Java)
• C heritage
• Examples:
– Can use numbers as boolean types; though a
separate boolean type exists
– __name__ == __main__
15. Explaining the comic - why Python
is awesome
• Dynamic vs static typing
• Whitespace as indentation
• Strongly typed vs weakly typed languages
• REPLs – rapid prototyping
• Interpreted – line by line; easier to find errors
16. import - ing
• Libraries and packages
• Batteries included philosophy of Python
• import antigravity
• import this
• import lotsofotherawesomestuff
• import modulename (BTW, modules (== .py files))
• from modulename import methodname
(BTW, methods ((not exactly) == functions))
17. Python is just words
• and del from not while as elif global or with
assert else if pass yield break except import
print class exec in raise continue finally is
return def for lambda try
• + variables
• + structure that you create
18. User input
• Complex programs = input + efficient
computation + output
• raw_input, NOT input
• raw_input – reads as string, so convert when
required (strongly typed, remember?)
• Example:
– var = raw_input(prompt)
19. Assignment statements
• Assignment is =
• ( and equality is ==, but you know this right?)
• Variable names should be sensible
• varname = computation / input
• Depends on frame
25. Operations
• + - * / % and ** for exponentiation
• Also +=, -=, *=, /=
• Floating point arithmetic
– [int].0
– Or float()
26. Boolean
• True / False
• >
• <
• ==
• !=
• >=
• <=
• is , is not (same as id())
27. Strings
• Immutable
• Single or double, they don’t care
• Raw strings
• Escape sequences
• String slicing – string[start:stop:step]
• We count from 0 (why?)
• String methods
28. Read the documentation
• If you know programming, (which you say you
do) reading the documentation is the best way
to learn
• Don’t have to memorise, only know how to
find what you need