0% found this document useful (0 votes)
17 views70 pages

Module Prog 2

computer programming

Uploaded by

Shella Gilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views70 pages

Module Prog 2

computer programming

Uploaded by

Shella Gilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 70

Introduction to

STRUCTURED PROGRAMMING
Structured Programming
• is a method of computer programming

• where in programmers create GUI


programs from scratch without using
click and drag.
Benefits
• Complexity can be reduced using the
concepts of divide and conquer.

• Logical structures ensure clear flow of


control.

• Increase in productivity by allowing


multiple programmers to work on
different parts of the project
independently at the same time.
Benefits
• Modules can be re-used many times,
thus it saves time, reduces complexity
and increase reliability.

• Easier to update/fix the program by


replacing individual modules rather
than larger amount of code.
OOP vs. STP
- There are both methods of computer
programming.

- The only difference is their approach.

- OOP is use to make codes very short and


reusable.

- STP is use to create GUI programs.

- They can combine.


• Welcome to PYTHON Programming
Language
History
• Python was created by Guido Von
Rossum in 1990.
Today
• Python was been developed by a large
team of Volunteers.

• Python is freely available from the


Internet.
Logo
Characteristics
Python is Open Source

Free Software
Characteristics
Python is Cross-Platform

Run in various OS
Characteristics
Python is High Level

Easy to learn & use


Characteristics
Python is Multi-Purpose

Many Uses
Advantages
• Extensive Libraries

Python downloads with an


extensive library and contains code
for various purposes.
So, we don’t have to write the
complete code for that manually.
Advantages
• Extensible

Python can be extended to other


languages. You can write some of
your code in languages like C++ or C.

This comes in handy, especially in


projects.
Advantages
• Embeddable

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

The language’s simplicity and


extensive libraries render
programmers more productive than
languages like Java and C++ do.
Also, the fact that you need to
write less and get more things
done.
Advantages
• IOT Opportunities

Since Python forms the basis of


new platforms like Raspberry Pi, it
finds the future bright for the
Internet Of Things.
This is a way to connect the
language with the real world.
Advantages
• Simple and Easy

When working with Java, you may


have to create a class to print ‘Hello
World’. But in Python, just a print
statement will do.
It is also quite easy to
learn, understand, and code.
Advantages
• Readable

Because it is not such a verbose


language, reading Python is much
like reading English.
It also does not need curly braces
to define blocks, and indentation is
mandatory. This further aids the
readability of the code.
Advantages
• Object-Oriented

This language help us with code


reusability, classes and objects let us
model the real world.
A class allows the encapsulation of
data and functions into one.
Advantages
• Free and Open-Source

Python is freely available. But not


only can you download Python for
free, but you can also download its
source code, make changes to it, and
even distribute it.
It downloads with an extensive
collection of libraries to help you with
your tasks.
Advantages
• Portable

In Python, you need to code only


once, and you can run it anywhere.
This is called Write Once Run
Anywhere (WORA).
Advantages
• Interpreted

The statements are executed one


by one, debugging is easier than in
compiled languages.
Advantages over other Languages
• Less Coding

Almost all of the tasks done in


Python requires less coding when the
same task is done in other languages.

This is the reason that many people


suggest learning Python to beginners.
Advantages over other Languages
• Affordable

Python is free therefore


individuals, small companies or big
organizations can leverage the free
available resources to build
applications.
Advantages over other Languages
• Python is for Everyone

Python code can run on any


machine whether it is Linux, Mac or
Windows.
Programmers need to learn different
languages for different jobs but with
Python, you can professionally build
many applications.
Disadvantages of Python
• Speed Limitations

We have seen that Python code is


executed line by line. But since
Python is interpreted, it often results
in slow execution.
This, however, isn’t a problem
unless speed is a focal point for the
project.
Disadvantages of Python
• Weak in Mobile Computing and
Browsers

While it serves as an excellent


server-side language, Python is much
rarely seen on the client-side.

The reason it is not so famous


despite the existence of Brython is
that it isn’t that secure.
Disadvantages of Python
• Design Restrictions

As you know, Python


is dynamically-typed. This means
that you don’t need to declare the type
of variable while writing the code.
It uses duck-typing. But wait,
what’s that? Well, it just means that if
it looks like a duck, it must be a duck.
.
Disadvantages of Python
• Underdeveloped Database Access
Layers

Compared to more widely used


technologies like JDBC (Java
DataBase Connectivity) and ODBC
(Open DataBase Connectivity),
Python’s database access layers are a
bit underdeveloped.
Uses
Python is a very flexible language.

It is widely used for many different


purposes:
– Mobile Application
– Web Application
– Windows Application
Programs written in Python
• YouTube
• DropBox
• Google
• Quora
• BitTorrent
• Spotify
• Yahoo Maps
Philosophy
• Python emphasizes READABILITY &
SIMPLICITY.
Readability
This is because of having indentation
as part of the codes.

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.

• Because of indention, reading the


codes will be easier.
Readability

Python doesn't use {curly} braces to


delimit blocks of code.
C++ Python
if (grade>=75)
{ if x>=75:
output=“PASSED”; output=“PASSED”
}
else:
else
{ output=“FAILED”
output=“FAILED”;
}
Simplicity
In Python, creating program is faster
due to more Compact codes.

• 3-5 times shorter than Java codes.

• 5-10 times shorter than C++ codes.


the Output of 2 C++ Programmers,
in One Year

can be finish by only 1 Python programmer,


in Two Months.
Software Tools
Text Editor
• Notepad or Notepad++

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.

• Limit lines to 79 characters.


Code Style - Best Practices
• Python is case-sensitive:

- All commands are lower case.


input()
print()
if
for
break
Code Style - Best Practices
- Class names should be written in CamelCase

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.

rectangle_area = 100 #variable

def get_message(): #function


print (“mabuhay ang PINOY”)
Fundamentals of Python
Comments
• C++ • PYTHON

//This is a comment #This is a comment

/*Multiple “ “ “ Multiple
Comment*/ Comment” “ “
Displaying Outputs
• C++ • PYTHON

#include<iostream> print ("Hello world")

cout<<“Hello world”;
Displaying Output
• C++ • PYTHON

cout<<“Hello world”; print ("Hello world " * 10)


cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
cout<<“Hello world”;
Accepting Input
• C++ • PYTHON

#include<iostream> x = input(“Enter input”)

int x=0; //declaration

cout<<“Enter input”;
cin>>x;
Variable Declaration
• C++ • PYTHON

int x=1; x=1


float y=1.5; y=1.5
char *z=“hello world”; z="hello world"
Decision Making
• 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

int x=1; x=1


while (x<=6) while x<=6:
{ print(x)
cout<<x; x=x+1
x=x+1;
}
for Loop (incrementation)
• C++ • PYTHON

for (int x=1; x<=6; x=x+1) for x in range(1,7,1):


{ print (x)
cout<<x;
}

#output #output
123456 123456
for Loop (decrementation)
• C++ • PYTHON

for (int x=6; x>=1; x=x-1) for x in range(6,0,-1):


{ print(x)
cout<<x;
}

#output #output
654321 654321
Array Variable
• C++ • PYTHON

char *box = { “a”, ”b”, ”c” } box = (“a”, “b”, ”c”)

for (int x=1; x<=3; x++) for item in box


{ print(item)
cout<<box[x];
}

#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)

if grade >= 75: OK CANCEL


textbox2.insert(0, “PASSED”)
Programmed by: Your Name here
else:
textbox2.insert(0, “FAILED”)
HANDS ON
• Program5
Create a Simple Calculator Program

quit()
End of the Slide

You might also like