SlideShare a Scribd company logo
Introduction to Python
Why Python?
• Python is
• easy to learn,
• relatively fast,
• object-oriented,
• strongly typed,
• widely used, and
• portable.
• C is much faster but much
harder to use.
• Java is about as fast and slightly
harder to use.
• Perl is slower, is as easy to use,
but is not strongly typed.
Getting started on the Mac
• Start a terminal session.
• Type “python”
• This should start the Python interpreter
> python
Python 2.4.2 (#2, Apr 10 2006, 16:28:28)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-53)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print “hello, world!”
hello, world!
The interpreter
Try printing various things
Leave off the quotation marks.
Print numbers, letters and combinations.
Print two things, with a comma between.
Enter a mathematical formula.
Leave off the word “print”.
The interpreter allows you to try things out
interactively and quickly.
Use the interpreter to test syntax, or to try
commands that you’re not sure will work when you
run your program.
Your first program
In your terminal, Ctrl-D out of python.
Type “pwd” to find your current working directory.
Open TextWrangler.
Create a file containing one line:
print “hello, world!”
Be sure that you end the line with a carriage return.
Save the file as “hello.py” in your current working directory.
In your terminal, type “python hello.py”
> python hello.py
hello, world!
Notice that, once you
save the file with “.py”
as the extension,
WordWrangler
automatically colors
the text according to
the syntax.
Objects and types
• We use the term object to refer to any entity in a python program.
• Every object has an associated type, which determines the properties of the
object.
• Python defines six types of built-in objects:
Number 10
String “hello”
List [1, 17, 44]
Tuple (4, 5)
Dictionary {‘food’ : ‘something you eat’,
‘lobster’ : ‘an edible, undersea arthropod’}
Files
• Each type of object has its own properties, which we will learn about in the
next several weeks.
• It is also possible to define your own types, comprised of combinations of
the six base types.
Literals and variables
•A variable is simply another name for an object.
•For example, we can assign the name “pi” to the
object 3.14159, as follows:
>>> pi = 3.14159
>>> print pi
3.14159
•When we write out the object directly, it is a literal,
as opposed to when we refer to it by its variable
name.
The “import” command
•Many python functions are only available via
“packages” that must be imported.
>>> print log(10)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'log' is not defined
>>> import math
>>> print math.log(10)
2.30258509299
The command line
• To get information into a program, we will typically use the
command line.
• The command line is the text you enter after the word
“python” when you run a program.
import sys
print "hello, world!"
print sys.argv[0]
print sys.argv[1]
• The zeroth argument is the name of the program file.
• Arguments larger than zero are subsequent elements of the
command line.
Sample problem #1
•Write a program called “print-two-args.py” that
reads the first two command line arguments after
the program name, stores their values as variables,
and then prints them on the same line with a colon
between.
> python print-two-args.py hello world
hello : world
Solution #1
import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]
print arg1, ":", arg2
Sample problem #2
•Write a program called “add-two-args.py” that
reads the first two command line arguments after
the program name, stores their values as variables,
and then prints their sum.
•Hint: To read an argument as a number, use the
syntax “arg1 = float(sys.argv[1])”
> python add-two-args.py 1 2
3.0
Solution #2
import sys
arg1 = float(sys.argv[1])
arg2 = float(sys.argv[2])
print arg1 + arg2
Ad

More Related Content

What's hot (20)

Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)
Matt Harrison
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
RaginiJain21
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
File io
File io File io
File io
نورا احمد
 
Python final ppt
Python final pptPython final ppt
Python final ppt
Ripal Ranpara
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter NotebooksIntroduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter Notebooks
Eueung Mulyana
 
Best Python Online Training with Live Project by Expert
Best Python Online Training with Live Project by Expert Best Python Online Training with Live Project by Expert
Best Python Online Training with Live Project by Expert
QA TrainingHub
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
Aakashdata
 
Introduction Jupyter Notebook
Introduction Jupyter NotebookIntroduction Jupyter Notebook
Introduction Jupyter Notebook
thirumurugan133
 
Python Intro
Python IntroPython Intro
Python Intro
Tim Penhey
 
java copy file program
java copy file programjava copy file program
java copy file program
Glen Pais
 
Learn PYTHON at ASIT
Learn PYTHON at ASITLearn PYTHON at ASIT
Learn PYTHON at ASIT
ASIT
 
working with files
working with filesworking with files
working with files
SangeethaSasi1
 
Demystifying how imports work in Python
Demystifying how imports work in PythonDemystifying how imports work in Python
Demystifying how imports work in Python
prodicus
 
Groovy
GroovyGroovy
Groovy
atonse
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Edureka!
 
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
Lihang Li
 
The Ring programming language version 1.5.1 book - Part 38 of 180
The Ring programming language version 1.5.1 book - Part 38 of 180The Ring programming language version 1.5.1 book - Part 38 of 180
The Ring programming language version 1.5.1 book - Part 38 of 180
Mahmoud Samir Fayed
 
Distributed Storage with IPFS and Python!
Distributed Storage with IPFS and Python!Distributed Storage with IPFS and Python!
Distributed Storage with IPFS and Python!
Abhinav Srivastava
 
Python ppt
Python pptPython ppt
Python ppt
Rachit Bhargava
 
Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)
Matt Harrison
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
RaginiJain21
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
Introduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter NotebooksIntroduction to IPython & Jupyter Notebooks
Introduction to IPython & Jupyter Notebooks
Eueung Mulyana
 
Best Python Online Training with Live Project by Expert
Best Python Online Training with Live Project by Expert Best Python Online Training with Live Project by Expert
Best Python Online Training with Live Project by Expert
QA TrainingHub
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
Aakashdata
 
Introduction Jupyter Notebook
Introduction Jupyter NotebookIntroduction Jupyter Notebook
Introduction Jupyter Notebook
thirumurugan133
 
java copy file program
java copy file programjava copy file program
java copy file program
Glen Pais
 
Learn PYTHON at ASIT
Learn PYTHON at ASITLearn PYTHON at ASIT
Learn PYTHON at ASIT
ASIT
 
Demystifying how imports work in Python
Demystifying how imports work in PythonDemystifying how imports work in Python
Demystifying how imports work in Python
prodicus
 
Groovy
GroovyGroovy
Groovy
atonse
 
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Advanced Python Tutorial | Learn Advanced Python Concepts | Python Programmin...
Edureka!
 
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
Lihang Li
 
The Ring programming language version 1.5.1 book - Part 38 of 180
The Ring programming language version 1.5.1 book - Part 38 of 180The Ring programming language version 1.5.1 book - Part 38 of 180
The Ring programming language version 1.5.1 book - Part 38 of 180
Mahmoud Samir Fayed
 
Distributed Storage with IPFS and Python!
Distributed Storage with IPFS and Python!Distributed Storage with IPFS and Python!
Distributed Storage with IPFS and Python!
Abhinav Srivastava
 

Viewers also liked (20)

Game playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphGame playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graph
Syed Zaid Irshad
 
Cube permutation multistage
Cube permutation multistageCube permutation multistage
Cube permutation multistage
Syed Zaid Irshad
 
Interconnection mechanisms
Interconnection mechanismsInterconnection mechanisms
Interconnection mechanisms
Syed Zaid Irshad
 
IT Infrastructure
IT InfrastructureIT Infrastructure
IT Infrastructure
Syed Zaid Irshad
 
Fotografía de página completa
Fotografía de página completaFotografía de página completa
Fotografía de página completa
Ada Luz Olivares
 
Presentation #1
Presentation #1Presentation #1
Presentation #1
KMOlson12
 
Norian SRS Rotary Mix
Norian SRS Rotary MixNorian SRS Rotary Mix
Norian SRS Rotary Mix
Michelle Lapus
 
Ilirika Jugoistocna Evropa
Ilirika Jugoistocna EvropaIlirika Jugoistocna Evropa
Ilirika Jugoistocna Evropa
guest29c407b0
 
Bagini Glacier Trek
Bagini Glacier TrekBagini Glacier Trek
Bagini Glacier Trek
bohemianadventures
 
24. Kecamatan pagindar
24. Kecamatan pagindar24. Kecamatan pagindar
24. Kecamatan pagindar
kabupaten_pakpakbharat
 
Everest Base Camp
Everest Base CampEverest Base Camp
Everest Base Camp
bohemianadventures
 
Nandanvan-Tapovan
Nandanvan-TapovanNandanvan-Tapovan
Nandanvan-Tapovan
bohemianadventures
 
Proyecto final curso leer e investigar en la era digital
Proyecto final curso leer e investigar en la era digitalProyecto final curso leer e investigar en la era digital
Proyecto final curso leer e investigar en la era digital
Cristina Marchante
 
Systolic array
Systolic arraySystolic array
Systolic array
Syed Zaid Irshad
 
Multicore computers
Multicore computersMulticore computers
Multicore computers
Syed Zaid Irshad
 
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Laura Cavallaro
 
Reduced instruction set computers
Reduced instruction set computersReduced instruction set computers
Reduced instruction set computers
Syed Zaid Irshad
 
Business plan (2)
Business plan (2)Business plan (2)
Business plan (2)
Syed Zaid Irshad
 
Data model
Data modelData model
Data model
Syed Zaid Irshad
 
Butterfly network
Butterfly networkButterfly network
Butterfly network
Syed Zaid Irshad
 
Game playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphGame playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graph
Syed Zaid Irshad
 
Cube permutation multistage
Cube permutation multistageCube permutation multistage
Cube permutation multistage
Syed Zaid Irshad
 
Interconnection mechanisms
Interconnection mechanismsInterconnection mechanisms
Interconnection mechanisms
Syed Zaid Irshad
 
Fotografía de página completa
Fotografía de página completaFotografía de página completa
Fotografía de página completa
Ada Luz Olivares
 
Presentation #1
Presentation #1Presentation #1
Presentation #1
KMOlson12
 
Ilirika Jugoistocna Evropa
Ilirika Jugoistocna EvropaIlirika Jugoistocna Evropa
Ilirika Jugoistocna Evropa
guest29c407b0
 
Proyecto final curso leer e investigar en la era digital
Proyecto final curso leer e investigar en la era digitalProyecto final curso leer e investigar en la era digital
Proyecto final curso leer e investigar en la era digital
Cristina Marchante
 
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Mobile Content - Contenuti digitali: mercato, scenari e opportunità dal Canal...
Laura Cavallaro
 
Reduced instruction set computers
Reduced instruction set computersReduced instruction set computers
Reduced instruction set computers
Syed Zaid Irshad
 
Ad

Similar to Introduction to python (20)

1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt
AmritMarwaha1
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 
python presntation 2.pptx
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptx
Arpittripathi45
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
samiwaris2
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
Punithavel Ramani
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
gmadhu8
 
Python
PythonPython
Python
Shivam Gupta
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
Shivam Gupta
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
MohammadSamiuddin10
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
MohammadSamiuddin10
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
Aditya Shankar
 
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
FabMinds
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
natnaelmamuye
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
SugumarSarDurai
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1
Elaf A.Saeed
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
BijuAugustian
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
Dozie Agbo
 
1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt1B-Introduction_to_python.ppt
1B-Introduction_to_python.ppt
AmritMarwaha1
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 
python presntation 2.pptx
python presntation 2.pptxpython presntation 2.pptx
python presntation 2.pptx
Arpittripathi45
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
samiwaris2
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
gmadhu8
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
Shivam Gupta
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 
Python for Security Professionals
Python for Security ProfessionalsPython for Security Professionals
Python for Security Professionals
Aditya Shankar
 
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
FabMinds
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
Python basics_ part1
Python basics_ part1Python basics_ part1
Python basics_ part1
Elaf A.Saeed
 
Fundamentals of python
Fundamentals of pythonFundamentals of python
Fundamentals of python
BijuAugustian
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
Dozie Agbo
 
Ad

More from Syed Zaid Irshad (20)

Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
Operating System.pdf
Operating System.pdfOperating System.pdf
Operating System.pdf
Syed Zaid Irshad
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
Syed Zaid Irshad
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
Syed Zaid Irshad
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
Syed Zaid Irshad
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
Syed Zaid Irshad
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
Syed Zaid Irshad
 
C Language
C LanguageC Language
C Language
Syed Zaid Irshad
 
Flowchart
FlowchartFlowchart
Flowchart
Syed Zaid Irshad
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
Syed Zaid Irshad
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Syed Zaid Irshad
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
Syed Zaid Irshad
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
Syed Zaid Irshad
 
Computer Architecture
Computer ArchitectureComputer Architecture
Computer Architecture
Syed Zaid Irshad
 
Data Communication
Data CommunicationData Communication
Data Communication
Syed Zaid Irshad
 
Information Networks
Information NetworksInformation Networks
Information Networks
Syed Zaid Irshad
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
Syed Zaid Irshad
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
Syed Zaid Irshad
 
Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
Syed Zaid Irshad
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
Syed Zaid Irshad
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
Syed Zaid Irshad
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
Syed Zaid Irshad
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
Syed Zaid Irshad
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
Syed Zaid Irshad
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
Syed Zaid Irshad
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
Syed Zaid Irshad
 

Recently uploaded (20)

Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Artificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptxArtificial Intelligence introduction.pptx
Artificial Intelligence introduction.pptx
DrMarwaElsherif
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
LECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's usesLECTURE-16 EARTHEN DAM - II.pptx it's uses
LECTURE-16 EARTHEN DAM - II.pptx it's uses
CLokeshBehera123
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 

Introduction to python

  • 2. Why Python? • Python is • easy to learn, • relatively fast, • object-oriented, • strongly typed, • widely used, and • portable. • C is much faster but much harder to use. • Java is about as fast and slightly harder to use. • Perl is slower, is as easy to use, but is not strongly typed.
  • 3. Getting started on the Mac • Start a terminal session. • Type “python” • This should start the Python interpreter > python Python 2.4.2 (#2, Apr 10 2006, 16:28:28) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-53)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print “hello, world!” hello, world!
  • 4. The interpreter Try printing various things Leave off the quotation marks. Print numbers, letters and combinations. Print two things, with a comma between. Enter a mathematical formula. Leave off the word “print”. The interpreter allows you to try things out interactively and quickly. Use the interpreter to test syntax, or to try commands that you’re not sure will work when you run your program.
  • 5. Your first program In your terminal, Ctrl-D out of python. Type “pwd” to find your current working directory. Open TextWrangler. Create a file containing one line: print “hello, world!” Be sure that you end the line with a carriage return. Save the file as “hello.py” in your current working directory. In your terminal, type “python hello.py” > python hello.py hello, world! Notice that, once you save the file with “.py” as the extension, WordWrangler automatically colors the text according to the syntax.
  • 6. Objects and types • We use the term object to refer to any entity in a python program. • Every object has an associated type, which determines the properties of the object. • Python defines six types of built-in objects: Number 10 String “hello” List [1, 17, 44] Tuple (4, 5) Dictionary {‘food’ : ‘something you eat’, ‘lobster’ : ‘an edible, undersea arthropod’} Files • Each type of object has its own properties, which we will learn about in the next several weeks. • It is also possible to define your own types, comprised of combinations of the six base types.
  • 7. Literals and variables •A variable is simply another name for an object. •For example, we can assign the name “pi” to the object 3.14159, as follows: >>> pi = 3.14159 >>> print pi 3.14159 •When we write out the object directly, it is a literal, as opposed to when we refer to it by its variable name.
  • 8. The “import” command •Many python functions are only available via “packages” that must be imported. >>> print log(10) Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'log' is not defined >>> import math >>> print math.log(10) 2.30258509299
  • 9. The command line • To get information into a program, we will typically use the command line. • The command line is the text you enter after the word “python” when you run a program. import sys print "hello, world!" print sys.argv[0] print sys.argv[1] • The zeroth argument is the name of the program file. • Arguments larger than zero are subsequent elements of the command line.
  • 10. Sample problem #1 •Write a program called “print-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints them on the same line with a colon between. > python print-two-args.py hello world hello : world
  • 11. Solution #1 import sys arg1 = sys.argv[1] arg2 = sys.argv[2] print arg1, ":", arg2
  • 12. Sample problem #2 •Write a program called “add-two-args.py” that reads the first two command line arguments after the program name, stores their values as variables, and then prints their sum. •Hint: To read an argument as a number, use the syntax “arg1 = float(sys.argv[1])” > python add-two-args.py 1 2 3.0
  • 13. Solution #2 import sys arg1 = float(sys.argv[1]) arg2 = float(sys.argv[2]) print arg1 + arg2