KEMBAR78
Python4HPC.pptx
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

Python4HPC.pptx

  • 1.
  • 2.
    Python! • Created in1991 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 usedeverywhere!
  • 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: • DownloadPython from http://www.python.or g • Install Python. • Run Idle from the Start Menu.
  • 6.
  • 7.
  • 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 runPython Windows • Run IDLE to use the interpret • Open a new window in IDLE to write and save programs
  • 10.
    Feb-24 Programming 10 Filename, preferredextension is py User Program
  • 11.
    Feb-24 Programming 11 •Standard PythonIDE (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 isInteractive Feb-24 Programming 12 IN[1]: IN[2]: IN[4]: IN[3]: Python Shell Prompt User Commands (Statements) Outputs ( )
  • 13.
    Interacting with PythonPrograms • 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 asargument 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
  • 16.
  • 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 Operatorsand Expressions 2/1/2024 18 Programming
  • 19.
    Binary Operations 2/1/2024 Programming 19 OpMeaning 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 • Indaily 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 • Comparetwo 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 (noelse!) • 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 • Generalform 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-24Programming 24 if a <= b: if a <= c: … else: … else: if b <= c) : … else: …
  • 25.
    Elif • A specialkind 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: …
  • 26.
  • 27.
    Printing Multiplication Table 5X 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 Xi = 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-24Python 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. Evaluateexpression 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 PythonProgramming 32 • Print the hi for 100 times. # the for loop for i in range(1,101): print(“hi”)
  • 33.
    For loop inPython • General form Feb-24 Python Programming 33 for variable in sequence: stmt