Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Welcome!
Python SIG – Students with
programming experience (PYA) –
Class 1 – 12-9-15
Ask doubts and give feedback
Let’s learn Python
hello world!
• The traditional first program
• In Python 2:
–print ‘hello world!’
• In Python 3:
–print(‘hello world!’)
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]
2 vs 3
• Difference in print – statement,
NOT function
• Libraries, libraries, libraries
Why 2 then?
• Tools & support – 2to3, future
module
• Libraries, libraries, libraries. Example:
Web development
• Easy to learn the other
What is code?
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]
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)
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
Python implementations
• CPython – reference implementation
• Others:
PyPy – Python in Python - compiled
IronPython - .NET comaptible
Jython – JVM compatible
• Further reading:
http://www.toptal.com/python/why-are-
there-so-many-pythons
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__
Obligatory xkcd reference [5]
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
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))
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
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)
Assignment statements
• Assignment is =
• ( and equality is ==, but you know this right?)
• Variable names should be sensible
• varname = computation / input
• Depends on frame
(Important) Built-in types
• boolean
• int
• float
• string
• list
• tuple
• dictionary
General idea
• Flexibility in Python
• Lists >> Arrays
• Methods – making things simpler
Mutability
• What is it?
• Why is it important?
Immutable
• int
• float
• string
• tuple
Mutable
• list
• dictionary
Operations
• + - * / % and ** for exponentiation
• Also +=, -=, *=, /=
• Floating point arithmetic
– [int].0
– Or float()
Boolean
• True / False
• >
• <
• ==
• !=
• >=
• <=
• is , is not (same as id())
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
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
In-class assignment
• Start coding!
• Use help() and dir()
• Use the offline docs
Thanks!
Pranav S Bijapur,
ECE, BMSCE, Bangalore
b.pranavshankar@gmail.com
Telegram - @pranavsb
References
1 -
https://wiki.python.org/moin/Python2orPyth
on3
2 -
https://en.wikiquote.org/wiki/Martin_Fowler
3 -
http://www.diveintopython.net/getting_to_kn
ow_python/everything_is_an_object.html
References
4 -
https://wiki.python.org/moin/PythonImpleme
ntations
5 -
https://www.xkcd.com/353/

More Related Content

What is Python?

  • 1. Welcome! Python SIG – Students with programming experience (PYA) – Class 1 – 12-9-15
  • 2. Ask doubts and give feedback
  • 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
  • 12. Python implementations • CPython – reference implementation • Others: PyPy – Python in Python - compiled IronPython - .NET comaptible Jython – JVM compatible • Further reading: http://www.toptal.com/python/why-are- there-so-many-pythons
  • 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
  • 20. (Important) Built-in types • boolean • int • float • string • list • tuple • dictionary
  • 21. General idea • Flexibility in Python • Lists >> Arrays • Methods – making things simpler
  • 22. Mutability • What is it? • Why is it important?
  • 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
  • 29. In-class assignment • Start coding! • Use help() and dir() • Use the offline docs
  • 30. Thanks! Pranav S Bijapur, ECE, BMSCE, Bangalore b.pranavshankar@gmail.com Telegram - @pranavsb
  • 31. References 1 - https://wiki.python.org/moin/Python2orPyth on3 2 - https://en.wikiquote.org/wiki/Martin_Fowler 3 - http://www.diveintopython.net/getting_to_kn ow_python/everything_is_an_object.html