SlideShare a Scribd company logo
Introduction to Python
Priyanka
Welcome Python Programming
1
Python!
• Created in 1991 by Guido van Rossum (now at Google)
– Named for Monty Python
• Useful as a scripting language
– script: A small program meant for one-time use
– Targeted towards small to medium sized projects
• Used by:
– Google, Yahoo!, Youtube
– Many Linux distributions
– Games and apps (e.g. Eve Online)
Python is used everywhere!
Interpreted Languages
• Interpreted
– Not compiled like Java
– Code is written and then directly executed by an interpreter
– Type commands into interpreter and see immediate results
Computer
Runtime
Environment
Compiler
Code
Java:
Computer
Interpreter
Code
Python:
Installing
Python
• Windows:
• Download Python from
http://www.python.or
g
• Install Python.
• Run Idle from the Start
Menu.
Feb-24 Programming
6
Feb-24 Programming
7
The Python Interpreter
• Allows you to type commands one-at-a-time and see results
• A great way to explore Python's syntax
– Repeat previous command: Alt+P
How to run Python Windows
• Run IDLE to use the interpret
• Open a new window in IDLE to write and save programs
Feb-24 Programming
10
Filename, preferred extension is py
User Program
Feb-24 Programming
11
•Standard Python IDE (Integrated Development
Environment)
•Main components: Editor, Shell, and Debugger
•Editor: Write and edit Python scripts
•Shell: Execute Python commands interactively
•Debugger: Helps in finding and fixing errors in
code
Python Shell is Interactive
Feb-24 Programming
12
IN[1]:
IN[2]:
IN[4]:
IN[3]:
Python Shell Prompt
User Commands
(Statements)
Outputs
( )
Interacting with Python Programs
• Python program communicates its results to
user using print
• Most useful programs require information
from users
– Name and age for a travel reservation system
• Python 3 uses input to read user input as a
string (str)
Feb-24 Programming
13
input
• Take as argument a string to print as a prompt
• Returns the user typed value as a string
– details of how to process user string later
Feb-24 Programming 14
IN[1]:
IN[2]:
IN[3]:
( )
Types in Python
• int
– Bounded integers, e.g. 732 or -5
• float
– Real numbers, e.g. 3.14 or 2.0
• long
– Long integers with unlimited precision
• str
– Strings, e.g. ‘hello’ or ‘C’
Feb-24 Programming
15
Strings
Programming
16
Operators
• Arithmetic
• Comparison
• Assignment
• Logical
• Bitwise
• Membership
• Identity
Feb-24 Programming
17
+ - * // / % **
== != > < >= <=
= += -= *= //= /= %= **=
and or not
in not in
is is not
& | ^ ~ >> <<
Programming using Python
Operators and Expressions
2/1/2024
18
Programming
Binary Operations
2/1/2024 Programming
19
Op Meaning Example Remarks
+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.25 In Python3
9.1/2.0 is 4.55 Real div.
// Integer Division 9//2 is 4
% Remainder 9%2 is 1
Conditional Statements
• In daily routine
–If it is very hot, I will skip
exercise.
–If there is a quiz tomorrow, I will
first study and then sleep.
Otherwise I will sleep now.
–If I have to buy coffee, I will
go left. Else I will go
straight.
Feb-24 Programming
20
if-else statement
• Compare two integers and print the min.
Feb-24 Programming
21
1. Check if x is less
than y.
2. If so, print x
3. Otherwise, print y.
if x < y:
print (x)
else:
print (y)
print (‘is the minimum’)
if statement (no else!)
• General form of the if statement
• Execution of if statement
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is
executed and then control moves to the S2.
– If expression evaluates to false, then control
moves to the S2 directly.
Feb-24 Programming
22
if boolean-expr :
S1
S2
S1
S2
if-else statement
• General form of the if-else statement
• Execution of if-else statement
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is executed and
then control moves to S3.
– If expression evaluates to false, then S2 is executed
and then control moves to S3.
– S1/S2 can be blocks of statements!
Feb-24 Programming
23
if boolean-expr :
S1
else:
S2
S3
S2
S1
S3
Nested if, if-else
Feb-24 Programming
24
if a <= b:
if a <= c:
…
else:
…
else:
if b <= c) :
…
else:
…
Elif
• A special kind of nesting is the chain of if-
else-if-else-… statements
• Can be written elegantly using if-elif-..-else
Feb-24 Programming
25
if cond1:
s1
elif cond2:
s2
elif cond3:
s3
elif …
else
last-block-of-stmt
if cond1:
s1
else:
if cond2:
s2
else:
if cond3:
s3
else:
…
Programming using Python
Loops
Feb-24
26
Python Programming
Printing Multiplication Table
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Feb-24 Python Programming
27
Program…
Feb-24 Python Programming
28
n = int(input('Enter a number: '))
print (n, 'X', 1, '=', n*1)
print (n, 'X', 2, '=', n*2)
print (n, 'X', 3, '=', n*3)
print (n, 'X', 4, '=', n*4)
print (n, 'X', 5, '=', n*5)
print (n, 'X', 6, '=', n*6)
….
Too much
repetition!
Can I avoid
it?
Print n X i = n*i
i = i+1
Input n
i = 1
i <=10
TRUE FALSE
Printing Multiplication Table
Feb-24 Python Programming
29
Stop
Loop
Loop Entry
Loop Exit
Printing Multiplication Table
Feb-24 Python Programming
30
n = int(input('n=? '))
i = 1
while (i <= 10) :
print (n ,'X', i, '=', n*i)
i = i + 1
print ('done‘)
Print n x i = ni
i = i+1
Input n
i = 1
TRUE
i <=10
FALSE
Stop
While Statement
1. Evaluate expression
2. If TRUE then
a) execute statement1
b) goto step 1.
3. If FALSE then execute statement2.
Feb-24 Python Programming
31
while (expression):
S1
S2
FALSE
TRUE
S1
expression
S2
For Loop
Feb-24 Python Programming
32
• Print the hi for 100 times.
# the for loop
for i in range(1,101):
print(“hi”)
For loop in Python
• General form
Feb-24 Python Programming
33
for variable in sequence:
stmt

More Related Content

PPTX
Python details for beginners and for students
PPTX
Introduction to vrevr rv4 rvr r r r u r a Python.pptx
PPTX
Python4HPC.pptx
PPTX
Python programming language presentation
PPTX
python ppt
PPTX
Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx
PPTX
Python4HPC.pptx
PPTX
Jenkins Pipelines [Autosaved].pptx
Python details for beginners and for students
Introduction to vrevr rv4 rvr r r r u r a Python.pptx
Python4HPC.pptx
Python programming language presentation
python ppt
Updatedpython.pptxUpdatedpython.pptxUpdatedpython.pptx
Python4HPC.pptx
Jenkins Pipelines [Autosaved].pptx

Similar to Python4HPC.pptx (20)

PDF
Blueprints: Introduction to Python programming
PDF
Cs4hs2008 track a-programming
PPTX
Basic Programming concepts - Programming with C++
PPTX
Learning C++ - Introduction to c++ programming 1
PPTX
ESCM303 Introduction to Python Programming.pptx
PDF
Python and Pytorch tutorial and walkthrough
PPTX
asic computer is an electronic device that can receive, store, process, and o...
PPTX
python introduction initial lecture unit1.pptx
PPTX
Kuliah07 TBK-Compiler 111111111111111111
PPTX
INTRODUCTION TO C++, Chapter 1
PPT
Chapter_7_tucker_noonan_programmng_lang.ppt
PPTX
ForLoops.pptx
PPTX
C# 101: Intro to Programming with C#
PPTX
OptView2 MUC meetup slides
PDF
if statements in Python -A lecture class
PPTX
Mastering Python lesson3b_for_loops
PDF
علم البيانات - Data Sience
PPTX
Module-1.pptx
PPTX
W1-Intro to python.pptx
PPT
Programing Fundamental
Blueprints: Introduction to Python programming
Cs4hs2008 track a-programming
Basic Programming concepts - Programming with C++
Learning C++ - Introduction to c++ programming 1
ESCM303 Introduction to Python Programming.pptx
Python and Pytorch tutorial and walkthrough
asic computer is an electronic device that can receive, store, process, and o...
python introduction initial lecture unit1.pptx
Kuliah07 TBK-Compiler 111111111111111111
INTRODUCTION TO C++, Chapter 1
Chapter_7_tucker_noonan_programmng_lang.ppt
ForLoops.pptx
C# 101: Intro to Programming with C#
OptView2 MUC meetup slides
if statements in Python -A lecture class
Mastering Python lesson3b_for_loops
علم البيانات - Data Sience
Module-1.pptx
W1-Intro to python.pptx
Programing Fundamental
Ad

Recently uploaded (20)

PPTX
Computer Architecture Input Output Memory.pptx
PPTX
20th Century Theater, Methods, History.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Trump Administration's workforce development strategy
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
1_English_Language_Set_2.pdf probationary
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Empowerment Technology for Senior High School Guide
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
Computer Architecture Input Output Memory.pptx
20th Century Theater, Methods, History.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
Indian roads congress 037 - 2012 Flexible pavement
Trump Administration's workforce development strategy
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
1_English_Language_Set_2.pdf probationary
History, Philosophy and sociology of education (1).pptx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Empowerment Technology for Senior High School Guide
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Weekly quiz Compilation Jan -July 25.pdf
Ad

Python4HPC.pptx

  • 2. Python! • Created in 1991 by Guido van Rossum (now at Google) – Named for Monty Python • Useful as a scripting language – script: A small program meant for one-time use – Targeted towards small to medium sized projects • Used by: – Google, Yahoo!, Youtube – Many Linux distributions – Games and apps (e.g. Eve Online)
  • 3. Python is used everywhere!
  • 4. Interpreted Languages • Interpreted – Not compiled like Java – Code is written and then directly executed by an interpreter – Type commands into interpreter and see immediate results Computer Runtime Environment Compiler Code Java: Computer Interpreter Code Python:
  • 5. Installing Python • Windows: • Download Python from http://www.python.or g • Install Python. • Run Idle from the Start Menu.
  • 8. The Python Interpreter • Allows you to type commands one-at-a-time and see results • A great way to explore Python's syntax – Repeat previous command: Alt+P
  • 9. How to run Python Windows • Run IDLE to use the interpret • Open a new window in IDLE to write and save programs
  • 10. Feb-24 Programming 10 Filename, preferred extension is py User Program
  • 11. Feb-24 Programming 11 •Standard Python IDE (Integrated Development Environment) •Main components: Editor, Shell, and Debugger •Editor: Write and edit Python scripts •Shell: Execute Python commands interactively •Debugger: Helps in finding and fixing errors in code
  • 12. Python Shell is Interactive Feb-24 Programming 12 IN[1]: IN[2]: IN[4]: IN[3]: Python Shell Prompt User Commands (Statements) Outputs ( )
  • 13. Interacting with Python Programs • Python program communicates its results to user using print • Most useful programs require information from users – Name and age for a travel reservation system • Python 3 uses input to read user input as a string (str) Feb-24 Programming 13
  • 14. input • Take as argument a string to print as a prompt • Returns the user typed value as a string – details of how to process user string later Feb-24 Programming 14 IN[1]: IN[2]: IN[3]: ( )
  • 15. Types in Python • int – Bounded integers, e.g. 732 or -5 • float – Real numbers, e.g. 3.14 or 2.0 • long – Long integers with unlimited precision • str – Strings, e.g. ‘hello’ or ‘C’ Feb-24 Programming 15
  • 17. Operators • Arithmetic • Comparison • Assignment • Logical • Bitwise • Membership • Identity Feb-24 Programming 17 + - * // / % ** == != > < >= <= = += -= *= //= /= %= **= and or not in not in is is not & | ^ ~ >> <<
  • 18. Programming using Python Operators and Expressions 2/1/2024 18 Programming
  • 19. Binary Operations 2/1/2024 Programming 19 Op Meaning Example Remarks + Addition 9+2 is 11 9.1+2.0 is 11.1 - Subtraction 9-2 is 7 9.1-2.0 is 7.1 * Multiplication 9*2 is 18 9.1*2.0 is 18.2 / Division 9/2 is 4.25 In Python3 9.1/2.0 is 4.55 Real div. // Integer Division 9//2 is 4 % Remainder 9%2 is 1
  • 20. Conditional Statements • In daily routine –If it is very hot, I will skip exercise. –If there is a quiz tomorrow, I will first study and then sleep. Otherwise I will sleep now. –If I have to buy coffee, I will go left. Else I will go straight. Feb-24 Programming 20
  • 21. if-else statement • Compare two integers and print the min. Feb-24 Programming 21 1. Check if x is less than y. 2. If so, print x 3. Otherwise, print y. if x < y: print (x) else: print (y) print (‘is the minimum’)
  • 22. if statement (no else!) • General form of the if statement • Execution of if statement – First the expression is evaluated. – If it evaluates to a true value, then S1 is executed and then control moves to the S2. – If expression evaluates to false, then control moves to the S2 directly. Feb-24 Programming 22 if boolean-expr : S1 S2 S1 S2
  • 23. if-else statement • General form of the if-else statement • Execution of if-else statement – First the expression is evaluated. – If it evaluates to a true value, then S1 is executed and then control moves to S3. – If expression evaluates to false, then S2 is executed and then control moves to S3. – S1/S2 can be blocks of statements! Feb-24 Programming 23 if boolean-expr : S1 else: S2 S3 S2 S1 S3
  • 24. Nested if, if-else Feb-24 Programming 24 if a <= b: if a <= c: … else: … else: if b <= c) : … else: …
  • 25. Elif • A special kind of nesting is the chain of if- else-if-else-… statements • Can be written elegantly using if-elif-..-else Feb-24 Programming 25 if cond1: s1 elif cond2: s2 elif cond3: s3 elif … else last-block-of-stmt if cond1: s1 else: if cond2: s2 else: if cond3: s3 else: …
  • 27. Printing Multiplication Table 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 5 X 10 = 50 Feb-24 Python Programming 27
  • 28. Program… Feb-24 Python Programming 28 n = int(input('Enter a number: ')) print (n, 'X', 1, '=', n*1) print (n, 'X', 2, '=', n*2) print (n, 'X', 3, '=', n*3) print (n, 'X', 4, '=', n*4) print (n, 'X', 5, '=', n*5) print (n, 'X', 6, '=', n*6) …. Too much repetition! Can I avoid it?
  • 29. Print n X i = n*i i = i+1 Input n i = 1 i <=10 TRUE FALSE Printing Multiplication Table Feb-24 Python Programming 29 Stop Loop Loop Entry Loop Exit
  • 30. Printing Multiplication Table Feb-24 Python Programming 30 n = int(input('n=? ')) i = 1 while (i <= 10) : print (n ,'X', i, '=', n*i) i = i + 1 print ('done‘) Print n x i = ni i = i+1 Input n i = 1 TRUE i <=10 FALSE Stop
  • 31. While Statement 1. Evaluate expression 2. If TRUE then a) execute statement1 b) goto step 1. 3. If FALSE then execute statement2. Feb-24 Python Programming 31 while (expression): S1 S2 FALSE TRUE S1 expression S2
  • 32. For Loop Feb-24 Python Programming 32 • Print the hi for 100 times. # the for loop for i in range(1,101): print(“hi”)
  • 33. For loop in Python • General form Feb-24 Python Programming 33 for variable in sequence: stmt