Download presentation
Presentation is loading. Please wait.
1
Programming Dr. Jalal Kawash
2
Programming in Jython
3
Variables Assignments
Named memory locations Hold values = is the assignment statement Assigns a value to a variable x = 10 y = 14 y += 1 z = x+y x +=5 y = z 10 15 x 15 25 14 y 25 z Variables Assignments
4
Printing Print messages to the user (screen) print “Hello”
print “Hello World!” print 5*4 num = 16 – 4 print num Printing
5
Arithmetic operators Addition: + Subtraction: - Multiplication: *
print 1+2 Subtraction: - x = y - z Multiplication: * print y*x Division: / y = x/z Modulo print 23%2 Arithmetic operators
6
Basic Data Types Integer Float String Boolean -1, 8, 1119, -17171717
1.6672, , String “Hi”, “Hello”, “Welcome to Jython and Python” ‘Hi’, ‘Hello’, ‘Welcome to Jython and Python’ Boolean true or false 1 or 0 A = 1 < 3 Basic Data Types
7
def simpleExample(): x = 12 print x y = 2
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z Example Program
8
def simpleExample(): x = 12 print x y = 2
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z Define a function/program Called simpleExample() Executing the function Example Program
9
def simpleExample(): x = 12 print x y = 2
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z Body of the function indentation defines inclusion Example Program
10
def simpleExample(): x = 12 print x y = 2
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z 12 x Example Program
11
12 12 output x Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x Example Program
12
12 12 24 output x y Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 y Example Program
13
12 12 24 24 output x y Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 24 y Example Program
14
12 12 24 24 36 output x y Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 24 36 y Example Program
15
12 12 24 36 24 36 output x y Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 36 24 y 36 Example Program
16
12 12 24 24 36 36 output x y z Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 24 36 y 36 z Example Program
17
12 12 24 24 36 36 output x y z Example Program
def simpleExample(): x = 12 print x y = 2*x print y y += x z = y%x print z output 12 12 x 24 24 36 y 36 z Example Program
18
Reading Input (Keyboard)
19
Reading Input (Keyboard)
Convert string input to integer Reading Input (Keyboard)
20
Conditionals
21
A program is a collection of functions
Conditionals A program is a collection of functions
22
A function can return a value
Conditionals A function can return a value
23
Conditionals
24
Value returned by int_input()
Conditionals
25
If statement Conditionals
26
Output Scenarios
27
SKIP Output Scenarios
28
Flow-Chart (If) Statement before if Test If true false condition
If body Statement after if Flow-Chart (If)
29
def condEx(): a = int_input() if a == 5: print ‘a was 5
def condEx(): a = int_input() if a == 5: print ‘a was 5!’ else: print ‘a was not 5!’ Conditionals
30
If-Then-Else Statements
If logical_condition then do_something Else do_something_else Check logical_condition If true then do_something, skip else part If false, then skip if and do else part (do_something_else) If-Then-Else Statements
31
Flow-Chart (If-Else) Statement before if Test If condition true false
If body Else body Statement after if Flow-Chart (If-Else)
32
If this condition is true
def condEx(): a = int_input() if a == 5: print ‘a was 5!’ else: print ‘a was not 5!’ If this condition is true Perform if body Skip Else Conditionals
33
def condEx(): a = int_input() print ‘Hi’ if a <= 5: print ‘My’ print ‘Dear’ else: print ‘Fi’ print ‘Deer’ print ‘Got it?’ If body Else body Conditionals
34
What is the output if user enters 10?
def condEx(): a = int_input() print ‘Hi’ if a <= 5: print ‘My’ print ‘Dear’ else: print ‘Fi’ print ‘Deer’ print ‘Got it?’ What is the output if user enters 10?
35
def condEx(): a = 10 print ‘Hi’ if a <= 5: print ‘My’ print ‘Dear’ else: print ‘Fi’ print ‘Deer’ print ‘Got it?’ False: 10 is not <= 5 Skip If Perform Else What is the output?
36
Hi Fi DEER output What is the output? Got it?
def condEx(): a = 10 print ‘Hi’ if a <= 5: print ‘My’ print ‘Dear’ else: print ‘Fi’ print ‘Deer’ print ‘Got it?’ output Hi Fi DEER Got it? What is the output?
37
What is the output if User Enters 3?
def condEx(): a = int_input() print ‘Hi’ if a <= 5: print ‘My’ print ‘Dear’ else: print ‘Fi’ print ‘Deer’ print ‘Got it?’ What is the output if User Enters 3?
38
Write a Jython routine that prints the letter mark, based on the value stored in a numericMark variable. A if numericMark >= 90 B if numericMark >= 80, but < 90 C if numericMark >=70, but <80 D if numericMark >=60, but <70 F otherwise (numericMark <60) Exercise
39
Read user input Convert it to integer
40
If grade >= 90 Print A
41
Else grade <90 If grade >=80 Print B
42
Else grade <80 If grade >=70 Print C
43
Else grade <70 If grade >=60 Print D
44
Else grade < 60 Print F
47
AND OR NOT Logical Operators
48
What is the output?
49
What is the output?
50
What is the output?
51
Exercise How would you write the following condition in Jython?
(x < 12) XOR (y >12.5) In general, how would you write A XOR B Exercise
52
Arrays and Lists
53
Arrays and Lists An array is a list of numbered (indexed) elements
Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’] John Frank Alicia Jenn James 1 2 3 4 Arrays and Lists
54
Arrays and Lists
55
Loops
56
For Loop for i in [0,1,2,3,4,5,6,7,8,9]: print i print 2*i 1 2 3 4 5 6
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 For Loop
57
For Loop for i in [9,2,7]: print i+1 for i in range(0,9): print i 10 3
8 1 2 3 4 5 6 7 8 For Loop
58
for i in range(0,9,2): print i for i in range(0,9,10): for i in range(5,0,-1):
2 4 6 8 5 4 3 2 1 For Loop
59
for i in range(L,H,Inc) i = L i < H i += Inc Inc is positive true
false For body Statement after for i += Inc for i in range(L,H,Inc) Inc is positive
60
for i in range(0,9,3): print i
i 3 i < 9 true Output print i i += 3 for i in range(0,9,3): print i
61
for i in range(0,9,3): print i
i 3 6 i < 9 true Output print i 3 i += 3 for i in range(0,9,3): print i
62
for i in range(0,9,3): print i
i 3 6 9 i < 9 true false Output print i 3 i += 3 6 done for i in range(0,9,3): print i
63
for i in range(H,L,Dec) i = H i > L i += Dec Dec is Negative true
false For body Statement after for i += Dec for i in range(H,L,Dec) Dec is Negative
64
for i in range(4,1,-1): print i
3 i > 1 true Output 4 print i i -= 1 for i in range(4,1,-1): print i
65
for i in range(4,1,-1): print i
3 2 i > 1 true Output 4 print i 3 i -= 1 for i in range(4,1,-1): print i
66
for i in range(4,1,-1): print i
i 3 2 1 i > 1 true false Output 4 print i 3 i -= 1 2 done for i in range(4,1,-1): print i
67
while tank-is-not-full fill-in-gas while house-temperature < set-temperature blow-heat while hungry east While loop
68
while boolean-condition: body-of-while body-of-while is repeated as long as boolean condition is true While loop
69
Statement before while
Test While condition true false While body Statement after while While Flow-Chart
70
i = 0 while i < 10: print i i += 2 i = 5 while i > 0: i -= 1
2 4 6 8 5 4 3 2 1 While Loop Examples
71
Input: a list of n elements [a0, a1, a2, … , an-1] and one additional element x
Output: YES, if x is in the list [a0, a1, a2, … , an-1] NO, otherwise Example Input: [12, 5, 6, 100, 3, 1], 12 Output: YES Input: [12, 5, 6, 100, 3, 1], 2 Output: No The Search Problem
72
Sequential Search Algorithm (version 1)
Found = NO i = 0 Compare x with ai If x == ai Found = YES i += 1 Repeat until all elements are examined Print Found Input: [a0, a1, a2, … , an-1] , x Sequential Search Algorithm (version 1)
73
Sequential Search Algorithm
Input: Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’], Alicia Alicia? Alicia? Alicia? John Frank Alicia Jenn James 1 2 3 4 NO Found Sequential Search Algorithm
74
Sequential Search Algorithm
Input: Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’], Alicia Alicia? Alicia? Alicia? Alicia? Alicia? John Frank Alicia Jenn James 1 2 3 4 YES Found Sequential Search Algorithm
75
Sequential Search Algorithm
Input: Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’], Wong Wong? Wong? Wong? Wong? Wong? John Frank Alicia Jenn James 1 2 3 4 NO Found Sequential Search Algorithm
76
Sequential Search Algorithm
77
Sequential Search Algorithm
Once found stop the loop Sequential Search Algorithm
78
A generic cleaner version
79
Sequential Search Algorithm
Input: Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’], Alicia Alicia? Alicia? Alicia? John Frank Alicia Jenn James 1 2 3 4 NO Found Sequential Search Algorithm
80
Sequential Search Algorithm
Input: Names = [‘John’, ’Frank’, ‘Alicia’, ‘Jenn’, ‘James’], Alicia Alicia? Alicia? Alicia? John Frank Alicia Jenn James 1 2 3 4 No need to examine YES Found Sequential Search Algorithm
81
Magic of Python
82
Algorithm for finding minimum of a list
Given a list L If L is empty return Undefined Min-so-far = L[0] For i in range(1, len(L)): If L[i] < Min-so-far: Min-so-far = L[i] Return Min-so-far Algorithm for finding minimum of a list
83
Min of a list John James James < John 1 Min-so-far Marie 2 John
James James < John 1 Min-so-far Marie John 2 Frank 3 Alicia 4 Min of a list
84
Min of a list John James 1 Min-so-far Marie James 2 Marie > James
James 1 Min-so-far Marie James 2 Marie > James Frank 3 Alicia 4 Min of a list
85
Min of a list John James 1 Min-so-far Marie James 2 Frank 3
James 1 Min-so-far Marie James 2 Frank 3 Frank < James Alicia 4 Min of a list
86
Min of a list John James 1 Min-so-far Marie Frank Alicia 2 Frank 3
James 1 Min-so-far Marie Frank Alicia 2 Frank 3 Alicia 4 Alicia < Frank Min of a list
87
Code for Algorithm Min
88
The Sorting Problem Input: a list of n elements [a0, a1, a2, … , an-1]
Output: The same list sorted in ascending order Example Input: [12, 5, 6, 100, 3, 1] Output: [1, 3, 5, 6, 12, 100] The Sorting Problem
89
Selection Sort Algorithm
Given a list L SortedL = [] For i in range(0,len(L)): minElement = Min(L) SortedL.append(minElement) L.remove(minElement) Return SortedL Selection Sort Algorithm
90
Selection Sort List L List SortedL John Alicia James 1 Marie 2 Frank 3
James 1 Marie 2 Frank 3 Alicia 4 Selection Sort
91
Selection Sort List L List SortedL John Alicia James Frank 1 1 Marie 2
James Frank 1 1 Marie 2 Frank 3 Selection Sort
92
Selection Sort List L List SortedL John Alicia James Frank 1 1 Marie
James Frank 1 1 Marie James 2 2 Selection Sort
93
List L List SortedL John Alicia Frank 1 Marie James 1 2 Selection Sort
94
List L List SortedL John Alicia Marie Frank 1 1 James 2 Selection Sort
95
Selection Sort List L List SortedL John Alicia Marie Frank 1 1 James 2
Marie Frank 1 1 James 2 John 3 Selection Sort
96
Selection Sort List L List SortedL Alicia Marie Frank 1 James 2 John 3
Marie Frank 1 James 2 John 3 Marie 4 Selection Sort
97
Selection Sort List L List SortedL Alicia Frank 1 James 2 John 3 Marie
Frank 1 James 2 John 3 Marie 4 Selection Sort
98
Selection Sort Program
99
There is a ready min(L) function in Jython
There is a ready sort, L.sort() as well Does not use selection sort, though
100
Nested For Loops
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.