Programming with Python: Week 1

Ahmet Bulut
Ahmet BulutChair, Department of Computer Science. um Istanbul Sehir University
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.
1 von 39

Recomendados

Python - the basics von
Python - the basicsPython - the basics
Python - the basicsUniversity of Technology
5.1K views130 Folien
Python quick guide1 von
Python quick guide1Python quick guide1
Python quick guide1Kanchilug
3.2K views70 Folien
Introduction about Python by JanBask Training von
Introduction about Python by JanBask TrainingIntroduction about Python by JanBask Training
Introduction about Python by JanBask TrainingJanBask Training
776 views74 Folien
Python: the Project, the Language and the Style von
Python: the Project, the Language and the StylePython: the Project, the Language and the Style
Python: the Project, the Language and the StyleJuan-Manuel Gimeno
3K views206 Folien
Python for All von
Python for All Python for All
Python for All Pragya Goyal
1.1K views17 Folien
Python Tutorial Part 2 von
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2Haitham El-Ghareeb
1.8K views50 Folien

Más contenido relacionado

Was ist angesagt?

Python Programming Language von
Python Programming LanguagePython Programming Language
Python Programming LanguageLaxman Puri
15.6K views44 Folien
Python-00 | Introduction and installing von
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installingMohd Sajjad
306 views11 Folien
An Introduction to Python Programming von
An Introduction to Python ProgrammingAn Introduction to Python Programming
An Introduction to Python ProgrammingMorteza Zakeri
2.5K views75 Folien
summer training report on python von
summer training report on pythonsummer training report on python
summer training report on pythonShubham Yadav
71.4K views20 Folien
Python Summer Internship von
Python Summer InternshipPython Summer Internship
Python Summer InternshipAtul Kumar
6.2K views18 Folien
Python presentation by Monu Sharma von
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu SharmaMayank Sharma
903 views14 Folien

Was ist angesagt?(19)

Python Programming Language von Laxman Puri
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri15.6K views
Python-00 | Introduction and installing von Mohd Sajjad
Python-00 | Introduction and installingPython-00 | Introduction and installing
Python-00 | Introduction and installing
Mohd Sajjad306 views
An Introduction to Python Programming von Morteza Zakeri
An Introduction to Python ProgrammingAn Introduction to Python Programming
An Introduction to Python Programming
Morteza Zakeri2.5K views
summer training report on python von Shubham Yadav
summer training report on pythonsummer training report on python
summer training report on python
Shubham Yadav71.4K views
Python Summer Internship von Atul Kumar
Python Summer InternshipPython Summer Internship
Python Summer Internship
Atul Kumar6.2K views
Python presentation by Monu Sharma von Mayank Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
Mayank Sharma903 views
Python indroduction von FEG
Python indroductionPython indroduction
Python indroduction
FEG96 views
Python Seminar PPT von Shivam Gupta
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
Shivam Gupta218.6K views
Seminar report On Python von Shivam Gupta
Seminar report On PythonSeminar report On Python
Seminar report On Python
Shivam Gupta37.3K views
Getting started with Linux and Python by Caffe von Lihang Li
Getting started with Linux and Python by CaffeGetting started with Linux and Python by Caffe
Getting started with Linux and Python by Caffe
Lihang Li15.4K views
Python Crash Course von Haim Michael
Python Crash CoursePython Crash Course
Python Crash Course
Haim Michael2.2K views
Introduction to python for Beginners von Sujith Kumar
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
Sujith Kumar130.2K views
Introduction to Python Programing von sameer patil
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programing
sameer patil218 views

Destacado

What is open source? von
What is open source?What is open source?
What is open source?Ahmet Bulut
408 views32 Folien
Programming with Python - Week 3 von
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3Ahmet Bulut
648 views47 Folien
Centro1807 marpegan tecnologia von
Centro1807 marpegan tecnologiaCentro1807 marpegan tecnologia
Centro1807 marpegan tecnologiaGuillermo Barrionuevo
275 views140 Folien
Programming with Python - Week 2 von
Programming with Python - Week 2Programming with Python - Week 2
Programming with Python - Week 2Ahmet Bulut
977 views48 Folien
Upstreamed von
UpstreamedUpstreamed
Upstreamedjfetch01
309 views16 Folien

Similar a Programming with Python: Week 1

4_Introduction to Python Programming.pptx von
4_Introduction to Python Programming.pptx4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptxGnanesh12
24 views85 Folien
Introduction to Python – Learn Python Programming.pptx von
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptxHassanShah396906
10 views10 Folien
Python intro von
Python introPython intro
Python introPiyush rai
509 views20 Folien
python presntation 2.pptx von
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptxArpittripathi45
199 views38 Folien
Python Programming 1.pptx von
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptxFrancis Densil Raj
27 views258 Folien
Python 01.pptx von
Python 01.pptxPython 01.pptx
Python 01.pptxAliMohammadAmiri
10 views60 Folien

Similar a Programming with Python: Week 1(20)

4_Introduction to Python Programming.pptx von Gnanesh12
4_Introduction to Python Programming.pptx4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx
Gnanesh1224 views
Introduction to Python – Learn Python Programming.pptx von HassanShah396906
Introduction to Python – Learn Python Programming.pptxIntroduction to Python – Learn Python Programming.pptx
Introduction to Python – Learn Python Programming.pptx
HassanShah39690610 views
Python for katana von kedar nath
Python for katanaPython for katana
Python for katana
kedar nath11.5K views
Python final presentation kirti ppt1 von Kirti Verma
Python final presentation kirti ppt1Python final presentation kirti ppt1
Python final presentation kirti ppt1
Kirti Verma589 views

Más de Ahmet Bulut

Nose Dive into Apache Spark ML von
Nose Dive into Apache Spark MLNose Dive into Apache Spark ML
Nose Dive into Apache Spark MLAhmet Bulut
110 views29 Folien
Data Economy: Lessons learned and the Road ahead! von
Data Economy: Lessons learned and the Road ahead!Data Economy: Lessons learned and the Road ahead!
Data Economy: Lessons learned and the Road ahead!Ahmet Bulut
116 views18 Folien
Apache Spark Tutorial von
Apache Spark TutorialApache Spark Tutorial
Apache Spark TutorialAhmet Bulut
1.8K views121 Folien
A Few Tips for the CS Freshmen von
A Few Tips for the CS FreshmenA Few Tips for the CS Freshmen
A Few Tips for the CS FreshmenAhmet Bulut
309 views22 Folien
Agile Data Science von
Agile Data ScienceAgile Data Science
Agile Data ScienceAhmet Bulut
525 views15 Folien
Data Science von
Data ScienceData Science
Data ScienceAhmet Bulut
945 views56 Folien

Más de Ahmet Bulut(12)

Nose Dive into Apache Spark ML von Ahmet Bulut
Nose Dive into Apache Spark MLNose Dive into Apache Spark ML
Nose Dive into Apache Spark ML
Ahmet Bulut110 views
Data Economy: Lessons learned and the Road ahead! von Ahmet Bulut
Data Economy: Lessons learned and the Road ahead!Data Economy: Lessons learned and the Road ahead!
Data Economy: Lessons learned and the Road ahead!
Ahmet Bulut116 views
Apache Spark Tutorial von Ahmet Bulut
Apache Spark TutorialApache Spark Tutorial
Apache Spark Tutorial
Ahmet Bulut1.8K views
A Few Tips for the CS Freshmen von Ahmet Bulut
A Few Tips for the CS FreshmenA Few Tips for the CS Freshmen
A Few Tips for the CS Freshmen
Ahmet Bulut309 views
Agile Data Science von Ahmet Bulut
Agile Data ScienceAgile Data Science
Agile Data Science
Ahmet Bulut525 views
Agile Software Development von Ahmet Bulut
Agile Software DevelopmentAgile Software Development
Agile Software Development
Ahmet Bulut387 views
Liselerde tanıtım sunumu von Ahmet Bulut
Liselerde tanıtım sunumuLiselerde tanıtım sunumu
Liselerde tanıtım sunumu
Ahmet Bulut894 views
Ecosystem for Scholarly Work von Ahmet Bulut
Ecosystem for Scholarly WorkEcosystem for Scholarly Work
Ecosystem for Scholarly Work
Ahmet Bulut388 views
Bilisim 2010 @ bura von Ahmet Bulut
Bilisim 2010 @ buraBilisim 2010 @ bura
Bilisim 2010 @ bura
Ahmet Bulut335 views
ESX Server from VMware von Ahmet Bulut
ESX Server from VMwareESX Server from VMware
ESX Server from VMware
Ahmet Bulut1.7K views

Último

Education of marginalized and socially disadvantages segments.pptx von
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptxGarimaBhati5
52 views36 Folien
The Picture Of A Photograph von
The Picture Of A PhotographThe Picture Of A Photograph
The Picture Of A PhotographEvelyn Donaldson
38 views81 Folien
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... von
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...Nguyen Thanh Tu Collection
106 views91 Folien
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023 von
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023A Guide to Applying for the Wells Mountain Initiative Scholarship 2023
A Guide to Applying for the Wells Mountain Initiative Scholarship 2023Excellence Foundation for South Sudan
89 views26 Folien
JQUERY.pdf von
JQUERY.pdfJQUERY.pdf
JQUERY.pdfArthyR3
114 views22 Folien
Gross Anatomy of the Liver von
Gross Anatomy of the LiverGross Anatomy of the Liver
Gross Anatomy of the Liverobaje godwin sunday
100 views12 Folien

Último(20)

Education of marginalized and socially disadvantages segments.pptx von GarimaBhati5
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptx
GarimaBhati552 views
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... von Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
JQUERY.pdf von ArthyR3
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
ArthyR3114 views
Interaction of microorganisms with vascular plants.pptx von MicrobiologyMicro
Interaction of microorganisms with vascular plants.pptxInteraction of microorganisms with vascular plants.pptx
Interaction of microorganisms with vascular plants.pptx
Artificial Intelligence and The Sustainable Development Goals (SDGs) Adoption... von BC Chew
Artificial Intelligence and The Sustainable Development Goals (SDGs) Adoption...Artificial Intelligence and The Sustainable Development Goals (SDGs) Adoption...
Artificial Intelligence and The Sustainable Development Goals (SDGs) Adoption...
BC Chew40 views
Introduction to AERO Supply Chain - #BEAERO Trainning program von Guennoun Wajih
Introduction to AERO Supply Chain  - #BEAERO Trainning programIntroduction to AERO Supply Chain  - #BEAERO Trainning program
Introduction to AERO Supply Chain - #BEAERO Trainning program
Guennoun Wajih135 views
UNIT NO 13 ORGANISMS AND POPULATION.pptx von Madhuri Bhande
UNIT NO 13 ORGANISMS AND POPULATION.pptxUNIT NO 13 ORGANISMS AND POPULATION.pptx
UNIT NO 13 ORGANISMS AND POPULATION.pptx
Madhuri Bhande48 views
Creative Restart 2023: Christophe Wechsler - From the Inside Out: Cultivating... von Taste
Creative Restart 2023: Christophe Wechsler - From the Inside Out: Cultivating...Creative Restart 2023: Christophe Wechsler - From the Inside Out: Cultivating...
Creative Restart 2023: Christophe Wechsler - From the Inside Out: Cultivating...
Taste39 views
OOPs - JAVA Quick Reference.pdf von ArthyR3
OOPs - JAVA Quick Reference.pdfOOPs - JAVA Quick Reference.pdf
OOPs - JAVA Quick Reference.pdf
ArthyR376 views
INT-244 Topic 6b Confucianism von S Meyer
INT-244 Topic 6b ConfucianismINT-244 Topic 6b Confucianism
INT-244 Topic 6b Confucianism
S Meyer51 views
Career Building in AI - Technologies, Trends and Opportunities von WebStackAcademy
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy51 views
Research Methodology (M. Pharm, IIIrd Sem.)_UNIT_IV_CPCSEA Guidelines for Lab... von RAHUL PAL
Research Methodology (M. Pharm, IIIrd Sem.)_UNIT_IV_CPCSEA Guidelines for Lab...Research Methodology (M. Pharm, IIIrd Sem.)_UNIT_IV_CPCSEA Guidelines for Lab...
Research Methodology (M. Pharm, IIIrd Sem.)_UNIT_IV_CPCSEA Guidelines for Lab...
RAHUL PAL45 views

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.

Hinweis der Redaktion

  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