SlideShare a Scribd company logo
1 of 27
Python Programming
(BCC302)
Introduction to Python
Brief History of Python
 Invented in the Netherlands, early 90s by Guido
van Rossum(February 20, 1991)
 Named after Monty Python
 Open sourced from the beginning
 Considered a scripting language, but is much more
 Scalable, object oriented and functional from the
beginning
 Used by Google from the beginning
 Increasingly popular
Python
Python is an open
source, object -
oriented, high-
level powerful
programming
language.
Developed by
Guido Van
Rossum in early
1990.
Python is
platform
independent
Introduction To Python Interpreter
A python program is read by a parser.
Python was designed to be highly readable language.
Python Program
Python program are composed of modules.
Modules contain statements.
Statements contain expressions.
Expressions create and process objects.
Features Of Python
Open source
Easy to learn
Major Uses Of Python
System
utilities
Web
development
Graphical
user
interface
Internet
scripting
Embedded
scripting
Major Uses Of Python
Database
access
and
programm
ing.
Game
programming
Rapid
prototyping
and
development
Distributed
programmin
g
Python Is Easy To Use
C program C ++ program Java
program
Python
program
#include <stdio.h>
int main (int
argc,char ** argv)
{
Printf (“welcome”);
}
#include<iostream >
int main()
{
Std:: cout
<<“welcome”<<std::e
ndl;
return0;
}
Public class
hello
{
Public static
voidmain(string
argv[])
{
System .out.
println(“welcom
e”);
}
}
Print (“welcome”)
Python Definition
Python is a high level programming language designed to be easy
to read and simple to implement.
Python is considered a scripting language.
Scripts written in python (.py files ) can be parsed and run
immediately.
Python Coding Style
Use 4 space per
indentation and
no tabs.
Do not mix tabs
and spaces.
Maximum line
length.
Python Coding Style
Use blank lines to
separate top level
function and class
definitions.
Inline comments
(should be
complete
sentences).
Use spaces around
expression and
statements.
Python Is Interactive
Python has an interactive console where you get a python prompt
and interact with interpreter directly.
• Interpreted
• Extendable
• Libraries
• supports
http://docs.python.org/
Running Python
The Python Interpreter
 Typical Python implementations offer both an interpreter
and compiler
 Interactive interface to Python with a read-eval-print loop
[finin@linux2 ~]$ python
Python 2.4.3 (#1, Jan 14 2008, 18:32:40)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def square(x):
... return x * x
...
>>> map(square, [1, 2, 3, 4])
[1, 4, 9, 16]
>>>
Installing
 Python is pre-installed on most Unix systems, including Linux
and MAC OS X
 The pre-installed version may not be the most recent one (2.6.2
and 3.1.1 as of Sept 09)
 Download from http://python.org/download/
 Latest version is 3.11.5
 Python comes with a large library of standard modules
 There are several options for an IDE
 IDLE – works well with Windows
 Emacs with python-mode or your favorite text editor
 Eclipse with Pydev (http://pydev.sourceforge.net/)
PyCharm
IDLE Development Environment
IDLE is an Integrated DeveLopment Environ-ment
for Python, typically used on Windows
Multi-window text editor with syntax highlighting,
auto-completion, smart indent and other.
Python shell with syntax highlighting.
Integrated debugger
with stepping, persis-
tent breakpoints,
and call stack visi-
bility
Running Interactively on UNIX
On Unix…
% python
>>> 3+3
6
 Python prompts with ‘>>>’.
 To exit Python (not Idle):
In Unix, type CONTROL-D
In Windows, type CONTROL-Z + <Enter>
Evaluate exit()
Running Programs on UNIX
 Call python program via the python interpreter
% python fact.py
 Make a python file directly executable by
 Adding the appropriate path to your python interpreter as
the first line of your file
#!/usr/bin/python
 Making the file executable
% chmod a+x fact.py
Invoking file from Unix command line
% fact.py
A Code Sample (in IDLE)
x = 34 - 23 # A comment.
y = “Hello” # Another one.
z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print (x)
print (y)
Enough to Understand the Code
 Indentation matters to code meaning
Block structure indicated by indentation
 First assignment to a variable creates it
Variable types don’t need to be declared.
Python figures out the variable types on its own.
 Assignment is = and comparison is ==
 For numbers + - * / % are as expected
Special use of + for string concatenation and % for string formatting (as in
C’s printf)
 Logical operators are words (and, or, not) not symbols
 The basic printing command is print
Basic Datatypes
 Integers (default for numbers)
z = 5 / 2 # Answer 2, integer division
 Floats
x = 3.456
 Strings
 Can use “” or ‘’ to specify with “abc” == ‘abc’
 Unmatched can occur within the string: “matt’s”
 Use triple double-quotes for multi-line strings or
strings than contain both ‘ and “ inside of them:
“““a‘b“c”””
Whitespace
Whitespace is meaningful in Python: especially indentation and
placement of newlines
Use a newline to end a line of code
Use  when must go to next line prematurely
No braces {} to mark blocks of code, use consistent indentation
instead
• First line with less indentation is outside of the block
• First line with more indentation starts a nested block
Colons start of a new block in many constructs, e.g. function
definitions, then clauses
Comments
Start comments with #, rest of line is ignored
Can include a “documentation string” as the first
line of a new function or class you define
Development environments, debugger, and other
tools use it: it’s good style to include one
def fact(n):
“““fact(n) assumes n is a positive
integer and returns facorial of n.”””
assert(n>0)
return 1 if n==1 else n*fact(n-1)

More Related Content

Similar to Introduction to python.pptx

intro.pptx (1).pdf
intro.pptx (1).pdfintro.pptx (1).pdf
intro.pptx (1).pdfANIKULSAIKH
 
Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3FabMinds
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners Sujith Kumar
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughgabriellekuruvilla
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfVisionAcademyProfSac
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course BasicNaiyan Noor
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxusvirat1805
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of pythonBijuAugustian
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfKosmikTech1
 
Lecture1_introduction to python.pptx
Lecture1_introduction to python.pptxLecture1_introduction to python.pptx
Lecture1_introduction to python.pptxMohammedAlYemeni1
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on pythonwilliam john
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Pythondidip
 
Web Programming UNIT VIII notes
Web Programming UNIT VIII notesWeb Programming UNIT VIII notes
Web Programming UNIT VIII notesBhavsingh Maloth
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptxsangeeta borde
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
 

Similar to Introduction to python.pptx (20)

intro.pptx (1).pdf
intro.pptx (1).pdfintro.pptx (1).pdf
intro.pptx (1).pdf
 
Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3Python Programming | JNTUK | UNIT 1 | Lecture 3
Python Programming | JNTUK | UNIT 1 | Lecture 3
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
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_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course Basic
 
Cmpe202 01 Research
Cmpe202 01 ResearchCmpe202 01 Research
Cmpe202 01 Research
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
 
ENGLISH PYTHON.ppt
ENGLISH PYTHON.pptENGLISH PYTHON.ppt
ENGLISH PYTHON.ppt
 
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
Lecture1_introduction to python.pptx
Lecture1_introduction to python.pptxLecture1_introduction to python.pptx
Lecture1_introduction to python.pptx
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python1
Python1Python1
Python1
 
Socket Programming In Python
Socket Programming In PythonSocket Programming In Python
Socket Programming In Python
 
Web Programming UNIT VIII notes
Web Programming UNIT VIII notesWeb Programming UNIT VIII notes
Web Programming UNIT VIII notes
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

Introduction to python.pptx

  • 3. Brief History of Python  Invented in the Netherlands, early 90s by Guido van Rossum(February 20, 1991)  Named after Monty Python  Open sourced from the beginning  Considered a scripting language, but is much more  Scalable, object oriented and functional from the beginning  Used by Google from the beginning  Increasingly popular
  • 4. Python Python is an open source, object - oriented, high- level powerful programming language. Developed by Guido Van Rossum in early 1990. Python is platform independent
  • 5. Introduction To Python Interpreter A python program is read by a parser. Python was designed to be highly readable language.
  • 6. Python Program Python program are composed of modules. Modules contain statements. Statements contain expressions. Expressions create and process objects.
  • 7. Features Of Python Open source Easy to learn
  • 8. Major Uses Of Python System utilities Web development Graphical user interface Internet scripting Embedded scripting
  • 9. Major Uses Of Python Database access and programm ing. Game programming Rapid prototyping and development Distributed programmin g
  • 10. Python Is Easy To Use C program C ++ program Java program Python program #include <stdio.h> int main (int argc,char ** argv) { Printf (“welcome”); } #include<iostream > int main() { Std:: cout <<“welcome”<<std::e ndl; return0; } Public class hello { Public static voidmain(string argv[]) { System .out. println(“welcom e”); } } Print (“welcome”)
  • 11. Python Definition Python is a high level programming language designed to be easy to read and simple to implement. Python is considered a scripting language. Scripts written in python (.py files ) can be parsed and run immediately.
  • 12. Python Coding Style Use 4 space per indentation and no tabs. Do not mix tabs and spaces. Maximum line length.
  • 13. Python Coding Style Use blank lines to separate top level function and class definitions. Inline comments (should be complete sentences). Use spaces around expression and statements.
  • 14. Python Is Interactive Python has an interactive console where you get a python prompt and interact with interpreter directly. • Interpreted • Extendable • Libraries • supports
  • 16.
  • 18. The Python Interpreter  Typical Python implementations offer both an interpreter and compiler  Interactive interface to Python with a read-eval-print loop [finin@linux2 ~]$ python Python 2.4.3 (#1, Jan 14 2008, 18:32:40) [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def square(x): ... return x * x ... >>> map(square, [1, 2, 3, 4]) [1, 4, 9, 16] >>>
  • 19. Installing  Python is pre-installed on most Unix systems, including Linux and MAC OS X  The pre-installed version may not be the most recent one (2.6.2 and 3.1.1 as of Sept 09)  Download from http://python.org/download/  Latest version is 3.11.5  Python comes with a large library of standard modules  There are several options for an IDE  IDLE – works well with Windows  Emacs with python-mode or your favorite text editor  Eclipse with Pydev (http://pydev.sourceforge.net/) PyCharm
  • 20. IDLE Development Environment IDLE is an Integrated DeveLopment Environ-ment for Python, typically used on Windows Multi-window text editor with syntax highlighting, auto-completion, smart indent and other. Python shell with syntax highlighting. Integrated debugger with stepping, persis- tent breakpoints, and call stack visi- bility
  • 21. Running Interactively on UNIX On Unix… % python >>> 3+3 6  Python prompts with ‘>>>’.  To exit Python (not Idle): In Unix, type CONTROL-D In Windows, type CONTROL-Z + <Enter> Evaluate exit()
  • 22. Running Programs on UNIX  Call python program via the python interpreter % python fact.py  Make a python file directly executable by  Adding the appropriate path to your python interpreter as the first line of your file #!/usr/bin/python  Making the file executable % chmod a+x fact.py Invoking file from Unix command line % fact.py
  • 23. A Code Sample (in IDLE) x = 34 - 23 # A comment. y = “Hello” # Another one. z = 3.45 if z == 3.45 or y == “Hello”: x = x + 1 y = y + “ World” # String concat. print (x) print (y)
  • 24. Enough to Understand the Code  Indentation matters to code meaning Block structure indicated by indentation  First assignment to a variable creates it Variable types don’t need to be declared. Python figures out the variable types on its own.  Assignment is = and comparison is ==  For numbers + - * / % are as expected Special use of + for string concatenation and % for string formatting (as in C’s printf)  Logical operators are words (and, or, not) not symbols  The basic printing command is print
  • 25. Basic Datatypes  Integers (default for numbers) z = 5 / 2 # Answer 2, integer division  Floats x = 3.456  Strings  Can use “” or ‘’ to specify with “abc” == ‘abc’  Unmatched can occur within the string: “matt’s”  Use triple double-quotes for multi-line strings or strings than contain both ‘ and “ inside of them: “““a‘b“c”””
  • 26. Whitespace Whitespace is meaningful in Python: especially indentation and placement of newlines Use a newline to end a line of code Use when must go to next line prematurely No braces {} to mark blocks of code, use consistent indentation instead • First line with less indentation is outside of the block • First line with more indentation starts a nested block Colons start of a new block in many constructs, e.g. function definitions, then clauses
  • 27. Comments Start comments with #, rest of line is ignored Can include a “documentation string” as the first line of a new function or class you define Development environments, debugger, and other tools use it: it’s good style to include one def fact(n): “““fact(n) assumes n is a positive integer and returns facorial of n.””” assert(n>0) return 1 if n==1 else n*fact(n-1)