Module Prog 2
Module Prog 2
STRUCTURED PROGRAMMING
Structured Programming
• is a method of computer programming
Free Software
Characteristics
Python is Cross-Platform
Run in various OS
Characteristics
Python is High Level
Many Uses
Advantages
• Extensive Libraries
Complimentary to extensibility,
Python is embeddable as well. You
can put your Python code in your
source code of a different language,
like C++.
Advantages
• Improved Productivity
C++ Python
if (grade>=75)
{ if x>=75:
output=“PASSED”; output=“PASSED”
}
else:
else
{ output=“FAILED”
output=“FAILED”;
}
Readability
• So, programmers have to write well-
indented codes for their programs to
work.
Python Editors
• PyDroid we will use this
• IDLE
• Eclipse
• Netbeans
• Komodo
• Emacs
• Vim
• Jedit
Code Style - Best Practices
• Do not use semicolons ; they are legal
but unnecessary.
class MyProgramClass:
#constructor
def __init__(self, val):
print (val)
#method
def display_message(self):
print ("Mabuhay Pinoy Programmers")
Code Style - Best Practices
• Everything else (variables, function,
modules...) should be lowercase with
underscore.
/*Multiple “ “ “ Multiple
Comment*/ Comment” “ “
Displaying Outputs
• C++ • PYTHON
cout<<“Hello world”;
Displaying Output
• C++ • PYTHON
cout<<“Enter input”;
cin>>x;
Variable Declaration
• C++ • PYTHON
if (grade>=75) if grade>=75:
{ print(“passed”)
cout<<“passed”;
}
if else Condition
• C++ • PYTHON
if (grade>=75) if grade>=75:
{ print(“passed”) #true
cout<<“passed”; else:
} print(“failed”) #false
else
{
cout<<“failed”;
}
if else if Condition
• C++ • PYTHON
if (grade>=75) if grade>=75:
{ print(“passed”)
cout<<“passed”; elif grade<75:
} print(“failed”)
else if(grade<75)
{
cout<<“failed”;
}
nested if Condition
• C++ • PYTHON
if (grade>=75) if grade>=75:
{ if level==1:
if(level==1) print(“ok lang”)
{
cout<<“ok lang”;
}
}
while Loop
• C++ • PYTHON
#output #output
123456 123456
for Loop (decrementation)
• C++ • PYTHON
#output #output
654321 654321
Array Variable
• C++ • PYTHON
#output #output
abc abc
Example
• Program1
Create a c++ program
That accepts two input integers
Then compute for the sum
and display the result.
Python
• Program1
Create a Python program
That accepts two input integers
Then compute for the sum
and display the result.
Source Code:
• Program2
Create a Python program
That accepts a Grade of College Student
Check the grade if Passed or Failed
and display the result.
Source Code:
Graphical User Interface
Program x
Input 1 :
Input 2 :
Sum:
OK CANCEL
Python Today : New PROGRAM
Program x
Grade :
Remarks :
grade = int(textbox1.get())
textbox2.remove(0, END)
quit()
End of the Slide