You are on page 1of 26

CPROG 1: PROCEDURAL

PROGRAMMING
MARY JEZZA M. SABALLA-MARIN
CHAPTER TOPICS

• Introduction to Procedural Programming


• Logical Operators
• Comments and Documentation in Python
OBJECTIVES

By the end of this chapter, student will be able to:


• Understand procedural programming and how it differs from other paradigms of programming;
• Describe procedural programming in the context of Python, as well as read and write comments;
• Evaluate a turtle program from the perspective of procedural programming.
FUNCTIONAL PROGRAMMING

• Much of programming is based around the idea of “functions”, in some circumstances referred to
as “methods.”
• A function is like a little machine: you drop something in referred to as “input”, and it spits
something out referred to as “output”.
• Functional programming is a fundamental part of most modern programming languages.
• The function name is given, followed by a set of parentheses. Inside the parentheses are given
any input, if needed.
OBJECT-ORIENTED PROGRAMMING

• It is the paradigm that lets us create new concepts and teach them to the computer.
• It means is that there is some code that isn’t executed in order when we execute our code;
instead, it’s sort of “pre-loaded” and used as needed.
• It breaks the imperative nature of procedural programming.
EVENT-DRIVEN PROGRAMMING

• Most programs you use are like this, waiting for a command to execute. When you’re sitting
looking at an empty word processor or an Internet browser, the program is waiting for you to do
something.
• When you do something, it triggers an event, and the event sets off some sequence of code.
• The code that the event sets off is just like the code we write procedurally, but it’s triggered by
events rather than happening automatically when we run our program.
PROCEDURAL PROGRAMMING IN PYTHON

• A sample code above is a single command.

• A series of these lines would execute the


command a series of times. The computer
executes each line one by one.
DATA TYPES AND VARIABLES

Data Type – other type of data that we print or process in our program.

print(5) -> prints an integer which is a whole number (not decimals)


print(5.5) -> prints another kind of data type. A decimal number, in Python called as float
(stands for floating point number)
print(“Hello”) -> prints a string of characters Hello.
print(True) -> prints a Boolean value (either TRUE or FALSE)
DATA TYPES AND VARIABLES CONT’D

Variable
• Are containers for storing data values.
• A variable is created the moment you first assign a value to it.
• The usefulness of this is that we can have a single variable name that changes its value over time.

Ex.
name = “John Doe”
age = 30
LOGICAL OPERATORS

• There are two kinds of operators: : logical operators and mathematical operators.
• The logical operators has two subtypes: relational operators and boolean operators.
RELATIONAL OPERATORS

• It check for the relationships between values, whether they’re contained in variables or not.
• Answerable by a boolean value (TRUE or FALSE)
BOOLEAN OPERATORS

• These are operators that themselves only act on True and False values (called boolean values).
• It operates on the results of the two relational operators on either side.
MATHEMATICAL OPERATORS

• Like boolean operators, mathematical operators operate on variables. Specifically, they perform
mathematical operations.

Ex.
num1=4
num2=7
print(5+3) -> prints 8
print(num1 + num2) -> prints 11
COMMENTS AND DOCUMENTATION

• Comments
• Represented by writing a # (number sign or pound or hashtag symbol before the comment)
• Are a way of explaining how our code works alongside the code itself.
• It is essentially some text written into the code with a certain character or label instructing the
computer to ignore it.
• It’s only there for people to read, so the computer pays no attention to it.
• Comments are like notes to future developers
COMMENTS AND DOCUMENTATION CONT’D

• Comments
• There are two kinds of comments: In-Line Comments and Code Block Comments

• In-Line Comments
• These are comments that are placed right alongside the code that we write to explain what it does at a very
detailed level.
• Code Block Comments
• Before big segments of code, at the beginning of files, or before longer functions, we might also put larger
explanations for what the code that follows tries to do.
• This is especially useful in bigger programs, where it’s important to understand not just how individual lines of
code work, but how the entire program fits together
SELF-DOCUMENTING CODE

• We have enough leeway in how we write code that we can write it in ways that make it more
clear what the code does.
• We can title our variables, our functions, our files any way we want.
• If you had a variable that represents how many words are in a file, why call it a when you can
call it numWords?
• That’s the notion of self-documenting code: writing code that shows what it means.
COMMENTS AND DOCUMENTATION IN
PYTHON
• In-Line Comments
• Give line-by-line descriptions of what a segment of code does.
• The presence of commented lines doesn’t change the operation of the code.
• Anything that appears after the pound sign is ignored by the computer.
• We usually put comments on their own lines before the code that they describe, but you can also put a
comment after the end of a line of code
COMMENTS AND DOCUMENTATION IN
PYTHON CONT’D
• Code Block Comments
• Comments that explain what the function does.
• This way, a person coming along and viewing our code later could easily figure out what the code
segment was intended to do and more easily revise it if necessary.
• In large programs with lots of functions, lots of different files, and lots of people working together, this
kind of documentation is essential.
SELF-DOCUMENTING CODE IN PYTHON

• Finally, wherever possible, we should write code that documents itself based on the names we
choose for things.
FUN ACTIVITY

First to type the answer on the chat box gets 5 points in recitation.
PRACTICE ACTIVITY 1

Supply the missing part of the code below to output “Hello World”

_____(“Hello World”)
PRACTICE ACTIVITY 2

What special character used in Python for in-line comments?


PRACTICE ACTIVITY 3

Create a variable name carname and assign the value Volvo to it.
Type the complete line of code.
PRACTICE ACTIVITY 4

Create a variable named x and assign the value 50 to it.


Type the complete line of code.
PRACTICE ACTIVITY 5

What is the symbol used to assign a values to a variable?

You might also like