SlideShare une entreprise Scribd logo
1  sur  39
Programming with
Python
                    Week 1
Dept. of Electrical Engineering and Computer Science
Academic Year 2010-2011
Why is it called Python?
Python’s Creator
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.
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.
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.
Python Gossip


• Python plays well with others.
• Python runs everywhere.
• Python is friendly, and easy to learn.
• Python is open.
“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.
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.
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.
The name of our game
            is
learn & practice & dream
     with Python
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.
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!
1.8 Interactive shell

• >>> 1+1
• >>> print ‘look mom, I am programming’
• >>> x=3
• >>> y=7
• >>> x+y
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.)
2 First Python program




                     odbchelper.py
Run the program


• On UNIX-compatible systems (including Mac OS X),
  you can run a Python program from the command line:

  python odbchelper.py
Output of the program
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.
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.
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.
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.
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.
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.
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.
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
Importing modules

• >>> sys.path.append(‘~/Dropbox/EECS Lisans/
  Programming with Python/Week 1/Code’)
• >>> import odbchelper
• >>> print odbchelper.__doc__
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.
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.
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.
code blocks
code blocks




   one
argument
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
code blocks

 code
 block




 code
 block
code blocks

 code
 block
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.
  :
            ...
Back to your first program



                  code
                  block
Back to your first program



                            code
                            block
8hrs of sleep will make
you happier.

Contenu connexe

Tendances

Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri
 

Tendances (19)

Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python-00 | Introduction and installing
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installing
 
An Introduction to Python Programming
An Introduction to Python ProgrammingAn Introduction to Python Programming
An Introduction to Python Programming
 
summer training report on python
summer training report on pythonsummer training report on python
summer training report on python
 
Python Summer Internship
Python Summer InternshipPython Summer Internship
Python Summer Internship
 
Python presentation by Monu Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Python indroduction
Python indroductionPython indroduction
Python indroduction
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Seminar report On Python
Seminar report On PythonSeminar report On Python
Seminar report On Python
 
Getting started with Linux and Python by Caffe
Getting started with Linux and Python by CaffeGetting started with Linux and Python by Caffe
Getting started with Linux and Python by Caffe
 
Python Crash Course
Python Crash CoursePython Crash Course
Python Crash Course
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Introduction to Python Programing
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programing
 
Python
PythonPython
Python
 

En vedette (9)

What is open source?
What is open source?What is open source?
What is open source?
 
Tristans Shooting
Tristans ShootingTristans Shooting
Tristans Shooting
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Centro1807 marpegan tecnologia
Centro1807 marpegan tecnologiaCentro1807 marpegan tecnologia
Centro1807 marpegan tecnologia
 
Programming with Python - Week 2
Programming with Python - Week 2Programming with Python - Week 2
Programming with Python - Week 2
 
Upstreamed
UpstreamedUpstreamed
Upstreamed
 
Kaihl 2010
Kaihl 2010Kaihl 2010
Kaihl 2010
 
Virtualization @ Sehir
Virtualization @ SehirVirtualization @ Sehir
Virtualization @ Sehir
 
Startup Execution Models
Startup Execution ModelsStartup Execution Models
Startup Execution Models
 

Similaire à Programming with Python: Week 1

Introduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptx
HassanShah396906
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 

Similaire à Programming with Python: Week 1 (20)

4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx
 
Introduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptx
 
Python intro
Python introPython intro
Python intro
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
python presntation 2.pptx
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptx
 
Python Programming 1.pptx
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptx
 
Python 01.pptx
Python 01.pptxPython 01.pptx
Python 01.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Python for katana
Python for katanaPython for katana
Python for katana
 
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdfPy-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
1-ppt-python.ppt
1-ppt-python.ppt1-ppt-python.ppt
1-ppt-python.ppt
 
Class_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdfClass_X_PYTHON_J.pdf
Class_X_PYTHON_J.pdf
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Python final presentation kirti ppt1
Python final presentation kirti ppt1Python final presentation kirti ppt1
Python final presentation kirti ppt1
 
Python Course In Chandigarh
Python Course In ChandigarhPython Course In Chandigarh
Python Course In Chandigarh
 
Python Module-1.1.pdf
Python Module-1.1.pdfPython Module-1.1.pdf
Python Module-1.1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 
Python Programming Part 1.pdf
Python Programming Part 1.pdfPython Programming Part 1.pdf
Python Programming Part 1.pdf
 

Plus de Ahmet Bulut

Liselerde tanıtım sunumu
Liselerde tanıtım sunumuLiselerde tanıtım sunumu
Liselerde tanıtım sunumu
Ahmet Bulut
 
Bilisim 2010 @ bura
Bilisim 2010 @ buraBilisim 2010 @ bura
Bilisim 2010 @ bura
Ahmet Bulut
 

Plus de Ahmet Bulut (12)

Nose Dive into Apache Spark ML
Nose Dive into Apache Spark MLNose Dive into Apache Spark ML
Nose Dive into Apache Spark ML
 
Data Economy: Lessons learned and the Road ahead!
Data Economy: Lessons learned and the Road ahead!Data Economy: Lessons learned and the Road ahead!
Data Economy: Lessons learned and the Road ahead!
 
Apache Spark Tutorial
Apache Spark TutorialApache Spark Tutorial
Apache Spark Tutorial
 
A Few Tips for the CS Freshmen
A Few Tips for the CS FreshmenA Few Tips for the CS Freshmen
A Few Tips for the CS Freshmen
 
Agile Data Science
Agile Data ScienceAgile Data Science
Agile Data Science
 
Data Science
Data ScienceData Science
Data Science
 
Agile Software Development
Agile Software DevelopmentAgile Software Development
Agile Software Development
 
Liselerde tanıtım sunumu
Liselerde tanıtım sunumuLiselerde tanıtım sunumu
Liselerde tanıtım sunumu
 
Ecosystem for Scholarly Work
Ecosystem for Scholarly WorkEcosystem for Scholarly Work
Ecosystem for Scholarly Work
 
I feel dealsy
I feel dealsyI feel dealsy
I feel dealsy
 
Bilisim 2010 @ bura
Bilisim 2010 @ buraBilisim 2010 @ bura
Bilisim 2010 @ bura
 
ESX Server from VMware
ESX Server from VMwareESX Server from VMware
ESX Server from VMware
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Dernier (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Programming with Python: Week 1

  • 1. Programming with Python Week 1 Dept. of Electrical Engineering and Computer Science Academic Year 2010-2011
  • 2. Why is it called Python?
  • 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. 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. 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. Python Gossip • Python plays well with others. • Python runs everywhere. • Python is friendly, and easy to learn. • Python is open.
  • 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. 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. 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. The name of our game is learn & practice & dream with Python
  • 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. 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. 1.8 Interactive shell • >>> 1+1 • >>> print ‘look mom, I am programming’ • >>> x=3 • >>> y=7 • >>> x+y
  • 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. 2 First Python program odbchelper.py
  • 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. Output of the program
  • 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. 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. 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. 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. 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. 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. 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. 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. Importing modules • >>> sys.path.append(‘~/Dropbox/EECS Lisans/ Programming with Python/Week 1/Code’) • >>> import odbchelper • >>> print odbchelper.__doc__
  • 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. 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. 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.
  • 32. code blocks one argument
  • 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. code blocks code block code block
  • 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. Back to your first program code block
  • 38. Back to your first program code block
  • 39. 8hrs of sleep will make you happier.

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n