O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Programming with Python: Week 1

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Python - the basics
Python - the basics
Carregando em…3
×

Confira estes a seguir

1 de 39 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Anúncio

Semelhante a Programming with Python: Week 1 (20)

Anúncio

Mais recentes (20)

Programming with Python: Week 1

  1. 1. Programming with Python Week 1 Dept. of Electrical Engineering and Computer Science Academic Year 2010-2011
  2. 2. Why is it called Python?
  3. 3. Python’s Creator
  4. 4. Guido van Rossum • Guido van Rossum, born in 31 January 1956, is a Dutch computer programmer, who invented the Python Programming Language. He is currently employed by Google, where he spends half of his time working on Python development.
  5. 5. It is called Python, because • Guido was reading the published scripts from a BBC comedy series Monthy Python from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.
  6. 6. So what is Python? • Python is an i. interpreted, ii. interactive, iii. object-oriented (OO) iv. programming language (PL). • Python is a dynamically typed language. • Python combines remarkable power with very clear syntax. • Python is portable: it runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, and OS/2.
  7. 7. Python Gossip • Python plays well with others. • Python runs everywhere. • Python is friendly, and easy to learn. • Python is open.
  8. 8. “Python is ...” • “I love Python! Once you learn and use it, you don't want to go back to anything else. It allows fast development, is easy to test and debug, and comes with an extensive set of powerful features and libraries. For any skeptics out there, YouTube is developed entirely on Python and works beautifully!” said Özgür D. Şahin, Senior Software Engineer at YouTube. • “Python has been important part of Google since the beginning, and remains so as the system grows and evolves. Today, dozens of Google engineers use Python, and we are looking for more people with skills in this language,” said Peter Norvig, Director of Search Quality at Google, Inc. • “Python makes us extremely productive, and makes maintaining a large and rapidly evolving codebase relatively simple,” said Mark Shuttleworth at Thawte Consulting and Canonical Ltd., Ubuntu OS sponsor.
  9. 9. Is Python a good language for beginner programmers? • For a student, who has never programmed before, using a statically typed language seems unnatural. It slows the pace. • Python has a consistent syntax and a large standard library. Students can focus on more important programming skills, such as problem decomposition and data type design, code reuse. • Very early in the course, students can be assigned to programming projects that do something.
  10. 10. Is Python a good language for beginner programmers? • Python’s interactive interpreter enables students to test language features while they are programming. • They can keep a window with the interpreter running, while they enter their program’s source in another window. • Python is extremely practical to teach basic programming skills to students.
  11. 11. The name of our game is learn & practice & dream with Python
  12. 12. 1 Installing Python • Open the /Applications folder. • Open the Utilities folder. • Double-click Terminal to open a terminal window and get to a command line. • Type python at the command prompt. • Make sure that you are running Python version 2.6.5.
  13. 13. 1.8 Interactive shell • Python leads a double life. • It's an interpreter for scripts that you can run from the command line or by double-clicking the scripts. But • It's also an interactive shell that can evaluate arbitrary statements and expressions. • This is useful for debugging, quick hacking, and testing. • Some people use the Python interactive shell as a calculator!
  14. 14. 1.8 Interactive shell • >>> 1+1 • >>> print ‘look mom, I am programming’ • >>> x=3 • >>> y=7 • >>> x+y
  15. 15. 1.8 Interactive shell • The Python interactive shell can evaluate arbitrary Python expressions, including any basic arithmetic expression (e.g., 1+1). • The interactive shell can execute arbitrary Python statements (e.g., print). • You can also assign values to variables, and the values will be remembered as long as the shell is open (but not any longer than that.)
  16. 16. 2 First Python program odbchelper.py
  17. 17. Run the program • On UNIX-compatible systems (including Mac OS X), you can run a Python program from the command line: python odbchelper.py
  18. 18. Output of the program
  19. 19. 2.2 Declaring Functions • Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation section like Pascal. Function def buildConnectionString(params): declaration • Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments (not shown here) are separated with commas.
  20. 20. Python Functions • Python functions do not specify the datatype of their return value; they don't even specify whether or not they return a value. • Every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, (Python null value). • The argument, params, doesn't specify a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.
  21. 21. Python --“dynamically typed” • In Python, you never explicitly specify the datatype of anything. • Based on what value you assign, Python keeps track of the datatype internally.
  22. 22. How Python datatypes compare to other PLs • statically typed language: types are fixed at compile time. You are required to declare all variables with their datatypes before using them. Java and C are statically typed languages. • dynamically typed language: types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value. • strongly typed language: types are always enforced. Java and Python are strongly typed. If you have an integer, you can't treat it like a string without explicitly converting it. • weakly typed language: types may be ignored; In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.
  23. 23. Dynamic and Strong • So Python is both dynamically typed, because it doesn't use explicit datatype declarations, and it is strongly typed, because once a variable has a datatype, it actually matters.
  24. 24. 2.3 Documenting functions doc • You can document a Python function by giving it a doc string. string • Triple quotes “1 ”2 ”3 signify a multiline string. Everything between the start and end quotes is part of a single string, including carriage returns and other quote characters. • Everything between the triple quotes is the functions’s doc string, which documents what the function does. • You should always define a doc string for your function.
  25. 25. 2.4 Everything is an object • Python functions have attributes and they are available at run time. A doc string is an attribute that is available at run time. • A function is also an object.
  26. 26. Importing modules • Import search path: when you try to import a module (a chunk of code that does something), Python uses the search path. Specifically, it searches in all directories defined in sys.path. This is a list and it is modifiable with standard list methods. • >>> import sys (hint: sys is a module itself) • >>> sys.path
  27. 27. Importing modules • >>> sys.path.append(‘~/Dropbox/EECS Lisans/ Programming with Python/Week 1/Code’) • >>> import odbchelper • >>> print odbchelper.__doc__
  28. 28. 2.4 Everything is an object two underscores • Everything in Python is an object, and almost everything has attributes and methods. • All functions have a built-in __doc__, which returns the doc string defined in the function’s source code. • Everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function. • Strings are objects, Lists are objects, Functions are objects, Modules are objects, You are an object.
  29. 29. 2.5 Indenting code • Python functions have i. NO explicit begin or end, and ii. NO curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.
  30. 30. 2.5 Indenting code • Code blocks are defined by their indentation. • By “code block”, I mean functions, if statements, for loops, while loops, and so forth. • Indenting starts a code block. Un-indenting ends that code block. • There are NO explicit braces, brackets, keywords used for marking a code block.
  31. 31. code blocks
  32. 32. code blocks one argument
  33. 33. code blocks print statements can take any data type, including strings, integers, and other native datatypes. you can print several things on one line by using a comma-separated list of values
  34. 34. code blocks code block code block
  35. 35. code blocks code block
  36. 36. code blocks • just indent and get on with your life. • indentation is a language requirement. not a matter of style. • Python uses carriage return to separate statements. • Python uses a colon and indentation to separate code blocks. : ...
  37. 37. Back to your first program code block
  38. 38. Back to your first program code block
  39. 39. 8hrs of sleep will make you happier.

Notas do Editor

  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

×