SlideShare a Scribd company logo
Python Programming
© copyright : spiraltrain@gmail.com
Course Schedule
• Python Intro
• Variables and Data Types
• Data Structures
• Control Flow and Operators
• Functions
• Modules
• Comprehensions
• Exception Handling
• Python IO
• Python Database Access
• Python Classes
• Optional : Python Libraries
2
• What is Python?
• Python Features
• History of Python
• Getting Started
• Setting up PATH
• Running Python
• Python in Interactive Mode
• Python in Script Mode
• Python Identifiers
• Python Reserved Words
• Comments in Python
• Lines and Indentation
• Multi Line Statements
• Quotes in Python
www.spiraltrain.nl
What is Python?
• Python is a general purpose high-level scripting language :
• Supports many applications from text processing to WWW browsers and games
• Python is Beginner's Language :
• Great language for the beginner programmers
• Python is interpreted :
• Processed at runtime by the interpreter like Perl and PHP code
• No need to compile your program before executing it
• Python is interactive :
• Prompt allows you to interact with the interpreter directly to write your programs
• Python is Object Oriented :
• Supports Object-Oriented style programming that encapsulates code within objects
• Python is easy to learn and easy to read :
• Python has relatively few keywords, simple structure, and a clearly defined syntax
• Python code is much more clearly defined and visible to the eye
3Python Intro
www.spiraltrain.nl
Python Features
• Broad standard library :
• Bulk of the library is cross portable on UNIX, Windows, and Macintosh
• Portable :
• Python runs on a wide variety of platforms and has same interface on all platforms
• Extendable :
• Python allows the addition of low-level modules to the Python interpreter
• Databases :
• Python provides interfaces to all major commercial databases
• GUI Programming :
• Python supports GUI applications that can be ported to many system calls
• Web Applications :
• Django and Flask Framework allow building of Python Web Applications
• Scalable :
• Python provides better structure and support for large programs than shell scripting
• Supports automatic garbage collection :
• Can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java
4Python Intro
www.spiraltrain.nl
History of Python
• Python was developed by Guido van Rossum around 1990 at :
• National Research Institute for Mathematics and Computer Science in Netherlands
• Python is derived from many other languages :
• Including Modula-3, C, C++,SmallTalk, Unix shell and other scripting languages
• Python is usable as :
• Scripting language or compiled to byte-code for building large applications
• Python is copyrighted and Python source code available :
• Like Perl under the GNU General Public License (GPL)
• Python is maintained by a development team at the institute :
• Guido van Rossum still holds a vital role in directing it's progress
• Python supports :
• Functional and structured programming methods as well as OOP
• Very high-level dynamic data types and dynamic type checking
5Python Intro
www.spiraltrain.nl
Getting Started
• Python Official Website :
• http://www.python.org/
• Has most up-to-date and current source code and binaries
• Both Python 2 and Python 3 are available which differ significantly
• Linux Installation :
• Download the zipped source code available for Unix/Linux
• Extract files and enter make install
• This will install Python in a standard location /usr/local/bin
• Windows Installation :
• Download the Windows installer python-XYZ.msi file
• Double-click the msi file and run the Python installation wizard
• Accept the default settings or choose an installation directory
• Python Documentation Website :
• www.python.org/doc/
• Documentation available in HTML, PDF and PostScript format
6Python Intro
www.spiraltrain.nl
Setting up PATH
• To invoke the Python interpreter from any particular directory :
• Add the Python directory to your path
• PATH is an environment variable :
• Provides a search path that lists directories that OS searches for executables
• Named PATH in Unix or Path in Windows
• Unix is case-sensitive, Windows is not
• Setting PATH in Linux :
• In the bash shell type :
export PATH="$PATH:/usr/local/bin/python" and press Enter
• Setting PATH in Windows :
• At a command prompt type :
PATH=%PATH%;C:Python and press Enter
• Or use the environment variables tab under System Properties
• PYTHONPATH :
• Tells Python interpreter where to locate module files you import into a program
7Python Intro
www.spiraltrain.nl
Running Python
• Invoke Python interpreter from any particular directory :
• Add the Python directory to your path
• Interactive Interpreter :
• Start the Python interactive interpreter from command line by entering python :
• You will get $python in Linux and >>> in Windows or Anaconda prompt with IPython
• Script can be parameterized with command line arguments C: python -h :
• Script from Command Line :
• A Python script can be executed at command line by invoking the interpreter
$ python script.py in Linux
C:> python script.py in Windows
• Integrated Development Environment :
• Run Python from a graphical user interface (GUI) environment
• GUI IDE for Python on system that supports Python is IDLE
• Jupyter Notebook on a web server :
• Give command jupyter notebook from Anaconda prompt
8Language Syntax
www.spiraltrain.nl
Python in Interactive Mode
• Python language has many similarities to Perl, C, and Java :
• There are some definite differences between the languages
• Start with Hello World Python program in interactive mode :
• Invoking interpreter by typing python in a command prompt :
• Type following right of the python prompt and press Enter :
>>> print ("Hello, Python!")
• This will produce following result :
Hello, Python!
• Separate multiple statements with a semicolon ;
9Python Intro
www.spiraltrain.nl
Python in Script Mode
• When the interpreter is invoked with a script parameter :
• The script begins execution and continues until it is finished
• When the script is finished, the interpreter is no longer active
• All Python files will have extension .py :
• Put the following source code in a test.py file
print ("Hello, Python!")
• Run the script as follows :
C: python test.py
• In a Unix environment make file executable with :
$ chmod +x test.py # set execute permission
• This will produce following result :
Hello, Python!
• print() default prints a new line
10Python Intro
Demo
01_hellopython
www.spiraltrain.nl
Python Identifiers
• Names used to identify things in Python :
• Variable, function, class, module, or other object
• Identifier Rules :
• Starts with a letter A to Z or a to z or an underscore (_)
• Followed by zero or more letters, underscores, and digits (0 to 9)
• Punctuation characters such as @, $, and % not allowed within identifiers
• Python is a case sensitive programming language :
• myVar and Myvar are different
• Naming conventions for Python :
• Class names start with uppercase letters, all other identifiers with lowercase letters
• Leading underscore is a weak private indicator :
— from M import * does not import objects with name starting with an underscore
• Two leading underscores indicates a strongly private identifier :
— Name mangling is applied to such identifiers by putting class name in front
• If identifier also ends with two trailing underscores :
— identifier is a language-defined special name 11Python Intro
www.spiraltrain.nl
Python Reserved Words
• May not be used as constant or variable or other identifier name
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
12Python Intro
www.spiraltrain.nl
Comments in Python
• Comments starts with :
• A hash sign (#) that is not inside a string literal
• All characters after the # and up to the physical line end are part of the comment
# First comment
print ("Hello, Python!") # second comment
• Will produce following result :
Hello, Python!
• Comment may be on same line after statement or expression :
name = "Einstein" # This is again comment
• Comment multiple lines as follows :
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.
13Python Intro
www.spiraltrain.nl
Lines and Indentation
• Blocks of code are denoted by line indentation :
• No braces for blocks of code for class and function definitions or flow control
• Number of spaces in the indentation is variable :
• All statements within the block must be indented the same amount
if True: # Both blocks in first example are fine
print ("True")
else:
print ("False")
• Other example :
if True: # Second line in second block will generate an error
print ("Answer")
print ("True")
else:
print ("Answer")
print ("False")
• print() without new line is written as follows :
print ('hello python', end='')
• Semicolon ; allows multiple statements on single line 14Language Syntax
Demo
02_lines_and_identation
www.spiraltrain.nl
Multi Line Statements
• Statements in Python typically end with a newline
• Python allows the use of the line continuation character () :
• Denotes that the line should continue
total = item_one + 
item_two + 
item_three
• Statements contained within the [], {}, or () brackets :
• No need to use the line continuation character
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
• On a single line :
• Semicolon ( ; ) allows multiple statements on single line
• Neither statement should start a new code block
15Python Intro
Demo
03_multilinestatements
www.spiraltrain.nl
Quotes in Python
• Python uses quotes to denote string literals :
• single ('), double (") and triple (''' or """)
• Same type of quote should start and end the string
• Triple quotes can be used to span the string across multiple lines
print ('hello' + " " + '''world''')
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is made up of
multiple lines and sentences."""
• Single quote in a single quoted string :
• Displayed with an escape character 
'What 's your name'
16Python Intro
Demo
04_quotesandescapes
© copyright : spiraltrain@gmail.com
Summary : Python Intro
• Python is a general purpose high-level scripting language :
• It is interpreted, interactive and object oriented
• Python was developed by Guido van Rossum around 1990 at :
• National Research Institute for Mathematics and Computer Science in Netherlands
• Python can be started in three different ways :
• Interactive Interpreter, Script from Command Line, from inside an IDE, Jupyter Notebook
• Python is a case sensitive programming language :
• myVar and Myvar are different
• Python uses line indentation to denote blocks of code :
• No braces for blocks of code for class and function definitions or flow control
• Python allows the use of the line continuation character () :
• Denote that the line should continue
• Python uses quotes to denote string literals :
• single ('), double (") and triple (''' or """)
Python Intro 17
Exercise 1
Environment Configuration

More Related Content

What's hot (19)

PPTX
Python Tutorial Part 2
Haitham El-Ghareeb
 
PDF
Introduction to python
Mohammed Rafi
 
PPTX
About Python Programming Language | Benefit of Python
Information Technology
 
KEY
Programming with Python: Week 1
Ahmet Bulut
 
PDF
Python quick guide1
Kanchilug
 
PPTX
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
PDF
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
 
PPTX
01 python introduction
Tamer Ahmed Farrag, PhD
 
PDF
Introduction of Python
ZENUS INFOTECH INDIA PVT. LTD.
 
PPTX
Plc part 1
Taymoor Nazmy
 
PDF
Programming with \'C\'
bdmsts
 
PPTX
Introduction to-python
Aakashdata
 
PPTX
Introduction to Python Programing
sameer patil
 
PPTX
Getting Started with Python
Sankhya_Analytics
 
PPT
Introduction to python
Ranjith kumar
 
PDF
Why learn python in 2017?
Karolis Ramanauskas
 
PPTX
Python presentation by Monu Sharma
Mayank Sharma
 
PPTX
Python Programming
RenieMathews
 
PPTX
2018 20 best id es for python programming
SyedBrothersRealEsta
 
Python Tutorial Part 2
Haitham El-Ghareeb
 
Introduction to python
Mohammed Rafi
 
About Python Programming Language | Benefit of Python
Information Technology
 
Programming with Python: Week 1
Ahmet Bulut
 
Python quick guide1
Kanchilug
 
Python | What is Python | History of Python | Python Tutorial
QA TrainingHub
 
Web programming UNIT II by Bhavsingh Maloth
Bhavsingh Maloth
 
01 python introduction
Tamer Ahmed Farrag, PhD
 
Introduction of Python
ZENUS INFOTECH INDIA PVT. LTD.
 
Plc part 1
Taymoor Nazmy
 
Programming with \'C\'
bdmsts
 
Introduction to-python
Aakashdata
 
Introduction to Python Programing
sameer patil
 
Getting Started with Python
Sankhya_Analytics
 
Introduction to python
Ranjith kumar
 
Why learn python in 2017?
Karolis Ramanauskas
 
Python presentation by Monu Sharma
Mayank Sharma
 
Python Programming
RenieMathews
 
2018 20 best id es for python programming
SyedBrothersRealEsta
 

Similar to Python Intro (20)

PDF
Unit 1-Part-1-Introduction to Python.pdf
Harsha Patil
 
PPTX
Session-1_Introduction to Python.pptx
WajidAliHashmi2
 
PPT
Py-Slides-1.pptPy-Slides-1.pptPy-Slides-1.pptPy-Slides-1.ppt
v65176016
 
PDF
Python Programing Bio computing,basic concepts lab,,
smohana4
 
PPT
Python slides for the beginners to learn
krishna43511
 
PPT
program on python what is python where it was started by whom started
rajkumarmandal9391
 
PPT
Py-Slides-1.ppt1234444444444444444444444444444444444444444
divijareddy0502
 
PPT
Python Over View (Python for mobile app Devt)1.ppt
AbdurehmanDawud
 
PPT
notwa dfdfvs gf fdgfgh s thgfgh frg reggg
Godwin585235
 
PDF
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
shetoooelshitany74
 
PDF
Python Programming.pdf
ssuser9a6ca1
 
PDF
Python Programming Part 1.pdf
percivalfernandez2
 
PDF
Python Programming Part 1.pdf
percivalfernandez2
 
PDF
Python Programming Part 1.pdf
percivalfernandez2
 
PPTX
ITC 110 Week 10 Introdution to Python .pptx
aaaaaannnnn6
 
PDF
Python_Programming_PPT Basics of python programming language
earningmoney9595
 
PPTX
Python programming language introduction unit
michaelaaron25322
 
PPTX
4_Introduction to Python Programming.pptx
Gnanesh12
 
PPTX
Introduction to Python Basics for PSSE Integration
FarhanKhan978284
 
PPTX
Lecture1_introduction to python.pptx
MohammedAlYemeni1
 
Unit 1-Part-1-Introduction to Python.pdf
Harsha Patil
 
Session-1_Introduction to Python.pptx
WajidAliHashmi2
 
Py-Slides-1.pptPy-Slides-1.pptPy-Slides-1.pptPy-Slides-1.ppt
v65176016
 
Python Programing Bio computing,basic concepts lab,,
smohana4
 
Python slides for the beginners to learn
krishna43511
 
program on python what is python where it was started by whom started
rajkumarmandal9391
 
Py-Slides-1.ppt1234444444444444444444444444444444444444444
divijareddy0502
 
Python Over View (Python for mobile app Devt)1.ppt
AbdurehmanDawud
 
notwa dfdfvs gf fdgfgh s thgfgh frg reggg
Godwin585235
 
Py-Slides- easuajsjsjejejjwlqpqpqpp1.pdf
shetoooelshitany74
 
Python Programming.pdf
ssuser9a6ca1
 
Python Programming Part 1.pdf
percivalfernandez2
 
Python Programming Part 1.pdf
percivalfernandez2
 
Python Programming Part 1.pdf
percivalfernandez2
 
ITC 110 Week 10 Introdution to Python .pptx
aaaaaannnnn6
 
Python_Programming_PPT Basics of python programming language
earningmoney9595
 
Python programming language introduction unit
michaelaaron25322
 
4_Introduction to Python Programming.pptx
Gnanesh12
 
Introduction to Python Basics for PSSE Integration
FarhanKhan978284
 
Lecture1_introduction to python.pptx
MohammedAlYemeni1
 
Ad

More from koppenolski (8)

PDF
Intro JavaScript
koppenolski
 
PPT
HTML Intro
koppenolski
 
PDF
Spring Boot
koppenolski
 
PDF
Wicket Intro
koppenolski
 
PDF
R Intro
koppenolski
 
PDF
SQL Intro
koppenolski
 
PDF
UML Intro
koppenolski
 
PDF
Intro apache
koppenolski
 
Intro JavaScript
koppenolski
 
HTML Intro
koppenolski
 
Spring Boot
koppenolski
 
Wicket Intro
koppenolski
 
R Intro
koppenolski
 
SQL Intro
koppenolski
 
UML Intro
koppenolski
 
Intro apache
koppenolski
 
Ad

Recently uploaded (20)

PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Tally software_Introduction_Presentation
AditiBansal54083
 

Python Intro

  • 2. © copyright : [email protected] Course Schedule • Python Intro • Variables and Data Types • Data Structures • Control Flow and Operators • Functions • Modules • Comprehensions • Exception Handling • Python IO • Python Database Access • Python Classes • Optional : Python Libraries 2 • What is Python? • Python Features • History of Python • Getting Started • Setting up PATH • Running Python • Python in Interactive Mode • Python in Script Mode • Python Identifiers • Python Reserved Words • Comments in Python • Lines and Indentation • Multi Line Statements • Quotes in Python
  • 3. www.spiraltrain.nl What is Python? • Python is a general purpose high-level scripting language : • Supports many applications from text processing to WWW browsers and games • Python is Beginner's Language : • Great language for the beginner programmers • Python is interpreted : • Processed at runtime by the interpreter like Perl and PHP code • No need to compile your program before executing it • Python is interactive : • Prompt allows you to interact with the interpreter directly to write your programs • Python is Object Oriented : • Supports Object-Oriented style programming that encapsulates code within objects • Python is easy to learn and easy to read : • Python has relatively few keywords, simple structure, and a clearly defined syntax • Python code is much more clearly defined and visible to the eye 3Python Intro
  • 4. www.spiraltrain.nl Python Features • Broad standard library : • Bulk of the library is cross portable on UNIX, Windows, and Macintosh • Portable : • Python runs on a wide variety of platforms and has same interface on all platforms • Extendable : • Python allows the addition of low-level modules to the Python interpreter • Databases : • Python provides interfaces to all major commercial databases • GUI Programming : • Python supports GUI applications that can be ported to many system calls • Web Applications : • Django and Flask Framework allow building of Python Web Applications • Scalable : • Python provides better structure and support for large programs than shell scripting • Supports automatic garbage collection : • Can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java 4Python Intro
  • 5. www.spiraltrain.nl History of Python • Python was developed by Guido van Rossum around 1990 at : • National Research Institute for Mathematics and Computer Science in Netherlands • Python is derived from many other languages : • Including Modula-3, C, C++,SmallTalk, Unix shell and other scripting languages • Python is usable as : • Scripting language or compiled to byte-code for building large applications • Python is copyrighted and Python source code available : • Like Perl under the GNU General Public License (GPL) • Python is maintained by a development team at the institute : • Guido van Rossum still holds a vital role in directing it's progress • Python supports : • Functional and structured programming methods as well as OOP • Very high-level dynamic data types and dynamic type checking 5Python Intro
  • 6. www.spiraltrain.nl Getting Started • Python Official Website : • http://www.python.org/ • Has most up-to-date and current source code and binaries • Both Python 2 and Python 3 are available which differ significantly • Linux Installation : • Download the zipped source code available for Unix/Linux • Extract files and enter make install • This will install Python in a standard location /usr/local/bin • Windows Installation : • Download the Windows installer python-XYZ.msi file • Double-click the msi file and run the Python installation wizard • Accept the default settings or choose an installation directory • Python Documentation Website : • www.python.org/doc/ • Documentation available in HTML, PDF and PostScript format 6Python Intro
  • 7. www.spiraltrain.nl Setting up PATH • To invoke the Python interpreter from any particular directory : • Add the Python directory to your path • PATH is an environment variable : • Provides a search path that lists directories that OS searches for executables • Named PATH in Unix or Path in Windows • Unix is case-sensitive, Windows is not • Setting PATH in Linux : • In the bash shell type : export PATH="$PATH:/usr/local/bin/python" and press Enter • Setting PATH in Windows : • At a command prompt type : PATH=%PATH%;C:Python and press Enter • Or use the environment variables tab under System Properties • PYTHONPATH : • Tells Python interpreter where to locate module files you import into a program 7Python Intro
  • 8. www.spiraltrain.nl Running Python • Invoke Python interpreter from any particular directory : • Add the Python directory to your path • Interactive Interpreter : • Start the Python interactive interpreter from command line by entering python : • You will get $python in Linux and >>> in Windows or Anaconda prompt with IPython • Script can be parameterized with command line arguments C: python -h : • Script from Command Line : • A Python script can be executed at command line by invoking the interpreter $ python script.py in Linux C:> python script.py in Windows • Integrated Development Environment : • Run Python from a graphical user interface (GUI) environment • GUI IDE for Python on system that supports Python is IDLE • Jupyter Notebook on a web server : • Give command jupyter notebook from Anaconda prompt 8Language Syntax
  • 9. www.spiraltrain.nl Python in Interactive Mode • Python language has many similarities to Perl, C, and Java : • There are some definite differences between the languages • Start with Hello World Python program in interactive mode : • Invoking interpreter by typing python in a command prompt : • Type following right of the python prompt and press Enter : >>> print ("Hello, Python!") • This will produce following result : Hello, Python! • Separate multiple statements with a semicolon ; 9Python Intro
  • 10. www.spiraltrain.nl Python in Script Mode • When the interpreter is invoked with a script parameter : • The script begins execution and continues until it is finished • When the script is finished, the interpreter is no longer active • All Python files will have extension .py : • Put the following source code in a test.py file print ("Hello, Python!") • Run the script as follows : C: python test.py • In a Unix environment make file executable with : $ chmod +x test.py # set execute permission • This will produce following result : Hello, Python! • print() default prints a new line 10Python Intro Demo 01_hellopython
  • 11. www.spiraltrain.nl Python Identifiers • Names used to identify things in Python : • Variable, function, class, module, or other object • Identifier Rules : • Starts with a letter A to Z or a to z or an underscore (_) • Followed by zero or more letters, underscores, and digits (0 to 9) • Punctuation characters such as @, $, and % not allowed within identifiers • Python is a case sensitive programming language : • myVar and Myvar are different • Naming conventions for Python : • Class names start with uppercase letters, all other identifiers with lowercase letters • Leading underscore is a weak private indicator : — from M import * does not import objects with name starting with an underscore • Two leading underscores indicates a strongly private identifier : — Name mangling is applied to such identifiers by putting class name in front • If identifier also ends with two trailing underscores : — identifier is a language-defined special name 11Python Intro
  • 12. www.spiraltrain.nl Python Reserved Words • May not be used as constant or variable or other identifier name and exec not assert finally or break for pass class from print continue global raise def if return del import try elif in while else is with except lambda yield 12Python Intro
  • 13. www.spiraltrain.nl Comments in Python • Comments starts with : • A hash sign (#) that is not inside a string literal • All characters after the # and up to the physical line end are part of the comment # First comment print ("Hello, Python!") # second comment • Will produce following result : Hello, Python! • Comment may be on same line after statement or expression : name = "Einstein" # This is again comment • Comment multiple lines as follows : # This is a comment. # This is a comment, too. # This is a comment, too. # I said that already. 13Python Intro
  • 14. www.spiraltrain.nl Lines and Indentation • Blocks of code are denoted by line indentation : • No braces for blocks of code for class and function definitions or flow control • Number of spaces in the indentation is variable : • All statements within the block must be indented the same amount if True: # Both blocks in first example are fine print ("True") else: print ("False") • Other example : if True: # Second line in second block will generate an error print ("Answer") print ("True") else: print ("Answer") print ("False") • print() without new line is written as follows : print ('hello python', end='') • Semicolon ; allows multiple statements on single line 14Language Syntax Demo 02_lines_and_identation
  • 15. www.spiraltrain.nl Multi Line Statements • Statements in Python typically end with a newline • Python allows the use of the line continuation character () : • Denotes that the line should continue total = item_one + item_two + item_three • Statements contained within the [], {}, or () brackets : • No need to use the line continuation character days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] • On a single line : • Semicolon ( ; ) allows multiple statements on single line • Neither statement should start a new code block 15Python Intro Demo 03_multilinestatements
  • 16. www.spiraltrain.nl Quotes in Python • Python uses quotes to denote string literals : • single ('), double (") and triple (''' or """) • Same type of quote should start and end the string • Triple quotes can be used to span the string across multiple lines print ('hello' + " " + '''world''') word = 'word' sentence = "This is a sentence." paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" • Single quote in a single quoted string : • Displayed with an escape character 'What 's your name' 16Python Intro Demo 04_quotesandescapes
  • 17. © copyright : [email protected] Summary : Python Intro • Python is a general purpose high-level scripting language : • It is interpreted, interactive and object oriented • Python was developed by Guido van Rossum around 1990 at : • National Research Institute for Mathematics and Computer Science in Netherlands • Python can be started in three different ways : • Interactive Interpreter, Script from Command Line, from inside an IDE, Jupyter Notebook • Python is a case sensitive programming language : • myVar and Myvar are different • Python uses line indentation to denote blocks of code : • No braces for blocks of code for class and function definitions or flow control • Python allows the use of the line continuation character () : • Denote that the line should continue • Python uses quotes to denote string literals : • single ('), double (") and triple (''' or """) Python Intro 17 Exercise 1 Environment Configuration