Midterm
Midterm
Intellectual Property of: Engr. James Francis B. Aguilar and the University of Nueva Caceres
Intended for the use in the UNC Curriculum of COMPUTER FUND & PROGRAMMING
Any unauthorized production of these materials is prohibited and punishable by
Intellectual Property Act of the Philippines
Introduction:
INTRODUCTORY APPROACH
Example 1.1
OUTPUT:
Welcome to Python!
• Lines 1-3 begin with the pound symbol (#), which indicates that the remainder of the
line is a comment
• The Python print command (line 5) instructs the computer to display the string of
characters contained between the quotation marks
• A string is a sequence of characters inside the double quotes. The entire line is called a
statement.
• In some programming languages, like C/C++ and Java, statements must end with a
semicolon.
UNIVERSITY OF NUEVA CACERES COMP. FUND & PROGRAMMING
\\ Backslash. Insert a
backslash character in a
string
\” Double quote. Insert a
double quote character in a
string
OUTPUT:
Welcome to Python!
• Line 4 displays the string "Welcome". Normally, after the print statement displays its
string, Python begins a new line.
• However, the comma (,) and end= at the end of line 4 tells Python not to begin a new
line but instead to add a space after the string
• Thus, the next string the program displays (line 5) appears on the same line as the string
Welcome
Procedure(s):
A.
Output:
B.
Question:
Differentiate the given code using print syntax in program
A & B.
___________________________________________________________
___________________________________________________________
_______________________________________________________.
Use:
name = "write your complete name in lowercase"
EX. name = “juan dela cruz”
name.upper()
name.lower()
name.title()
name.capitalize()
len(name)
III. Using the techniques you learned, write a program that displays these message
1. #THIS IS A COMMENT
2. PRINTING A LINE OF A TEXT
BINARY OCT
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
Questions:
# pound symbol
______________________________________________
______________________________________________
______________________________________________
end=" "
______________________________________________
______________________________________________
______________________________________________
print
______________________________________________
______________________________________________
______________________________________________
Introduction:
In programming, a variable is a storage that hold data. But unlike C and C++, Python is a type-
inferred language, so you don’t have to explicitly define the variable type. It automatically knows that a
given variables is a string, an integer or a floating number.
The Arithmetic operators are some of the Python programming operator, which are used to
perform arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division and
Modulus. All these operators are binary operators which means they operate on two operands
PYTHON VARIABLES
Example 2.1
OUTPUT: 10
Example 2.2
DESTRUCTIVE READ-IN
▪ The process of reading information into a memory wherein the value replaces the previous
value in that location
▪ An attempt to divide by zero is normally undefined on computer systems and generally results in
a fatal error
▪ Fatal error are errors that causes the program to terminate immediately without having
successfully performed its job
▪ Non-fatal errors allow programs to run to completion, often producing incorrect results
Example 2.3
OUTPUT: 5
3.2
hello
Example 2.4
print(int(x) + int(y))
OPERATORS
Operators: These are special symbols used to perform operations on data. Some
common operators include:
• Arithmetic Operators: (+, -, *, /) for calculations.
• Comparison Operators: (==, !=, <, >, <=, >=) for comparing values.
• Logical Operators: (and, or, not) for combining conditions.
lower()
converts a string into lower case
string.lower()
capitalize()
converts a string into upper case
string.capitalize()
split()
splits the string at the specified separator and returns a list
string.split(separator, maxsplit)
title()
converts the first character of each word to uppercase
string.title()
len()
count characters in a string
len(string)
replace()
returns a string where a specified value is replace with a specified value
string.replace(oldvalue, newvalue, count)
Procedure(s):
A. (Digits of an Integer) 40 pts. Write a program that inputs a five-digit integer password,
separates the integer into its digits and prints them separated by three spaces each. [Hint:
Use the integer division and modulus operators.] For example, if the user types in 12345, the
program should print:
1 2 3 4 5
B. (Diameter, Circumference and Area of a Circle)30pts. Write a program that reads in the cross-
sectional radius of the wire as an integer and prints the wire’s diameter, circumference and
area. Use the constant value 3.14159 for π. Do all calculations in output statements.
C. (Power, Voltage and Current) 30pts. Write a program that reads in the voltage and current of
the given system and computes power rating of it.
D. Please write a Python script that assigns values to four variables: str(student_name),
str(course),int(age), and str(gender). Once assigned, kindly print the data in the specified
format, without errors. Ensure that you follow the instructions precisely, using the correct
syntax and format for the code. Please note that the output must be in uppercase, title-case,
string format, and capitalized format for name, course, age, and gender, respectively. Thank
you for your attention to detail.
Introduction:
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the
else block will be executed. Python programming language assumes any non-zero and non-null values as
true, and if it is either zero or null, then it is assumed as false value.
Python uses indentation to delimit (distinguish) sections of code. Other programming languages
often use braces to delimit sections of code. A suite is a section of code that corresponds to the body of
a control structure.
• The if… else selection statements allow the programmer to specify that the different actions are
to be performed when the condition is true than when the condition is false
• The if...else statement is used to execute a block of code among two alternatives.
• However, if we need to make a choice between more than two alternatives, then we use the
if...elif...else statement.
Example 3.1
Example 3.2
1. # Example 3.2
2. # A grade program by Engr. JF. Aguilar
3.
4. print ("This program will tell you if you passed or failed")
5.
6. # read first entry and convert the string to float
7. grade = float(input ("\nEnter your grade from 0 to 100:\t"))
8.
9. # the if... elif and else condition
10. if grade > 100:
11. print ("\n\nNOT A VALID GRADE!!!\n\n\n")
12. elif grade == 100:
13. print ("\n\nP E R F E C T !!!.", end=" ")
14. print ("Your grade is A.\n\n")
15. elif grade>=89:
16. print ("Passed: Your grade is A\n\n")
17. elif grade>=79:
18. print("\n\nPassed: Your grade is B\n\n")
19. elif grade>=69:
20. print("Passed: Your grade is C\n\n")
21. elif grade>=59:
22. print("\n\nPassed: Your grade is D\n\n")
23. else:
24. print("\n\nFAILED ! ! ! \n\n\n")
25.
Procedure(s):
A. (Arithmetic, Smallest and Largest) 40 pts. Write a program that inputs three integers from the
keyboard and prints the sum, average, product, smallest, and largest of these numbers. The
screen dialog should appear as follows:
B. (Odd or Even)30 pts. Write a program that reads an integer and determines and prints whether
it’s odd or even. [Hint: Use the modulus operator. An even number is a multiple of two. Any
multiple of two leaves a remainder of zero when divided by 2.]
C. (If…else statement)30 pts. Write your own Python code that will utilize if…else statements, in
which it differentiates the state of a certain object. Using any of these, <, >, >=, <=, == or! =
Introduction:
Before writing a program to solve a particular problem, it is essential to have a thorough
understanding of the problem and a carefully planned approach to solving the problem. When writing a
program, it is equally essential to understand the types of building blocks that are available and to use
proven program-construction principles.
Repetition executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable. It is more like a while statement, except that it tests the condition at the end
of the loop body.
FORMULATING ALGORITHM
Algorithms
Control Structures
1. Sequential execution
• Statements in a program are executed one after the other in the order in which they are
written
2. Transfer of control
• Enable the programmer to specify that the next statement to be executed may be other
than the next one in sequence
Pseudocode
• The pseudocode is useful for developing algorithms that will be converted to structured
C programs
Example 1:
Pseudocode
1. If student’s grade is greater than or equal to 60
2. Print “Passed”
Example 2:
Pseudocode
1. If student’s grade is greater than or equal to 60
2. Print “Passed”
3. Else
4. Print “Failed”
Procedure(s):
I. Compile the following source code and write the output in the box
A.
1. # A counter from 1 to 10
2. # using while repetition statement
3.
4. counter = 1
5.
6. #using while to count from 1 to 10
7. while counter<=10:
8. print (counter, end="\n")
9. counter=counter+1
Output
B.
1. # A countdown from 10 to 1
2. # using while repetition statement
3.
4. countdown = 10
5.
6. #using while to reverse count from 10 to 1
7. while countdown >= 1:
8. print (countdown, end="\n")
9. countdown=countdown-1
Output
OPERATORS DESCRIPTION
==
>
<
>=
<=
A.
Write a pseudocode
B.
Write a pseudocode
Questions:
While
______________________________________________
______________________________________________
______________________________________________
for
______________________________________________
______________________________________________
______________________________________________
counter=counter+1
______________________________________________
______________________________________________
______________________________________________
range() function
______________________________________________
______________________________________________
______________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
___________________________________________________________
_______________
1
4
9
16
25
36
49
64
81
Introduction:
initialization
while loopContinuationTest:
statement(s)
increment
FORMULATING ALGORITHM
1. CASE STUDY 1 (Counter-Controlled Repetition)
• This technique uses a variable called a counter to specify the number of times a set of
statements should execute
• Is often called definite repetition because the number of repetitions is known before the
loop begins executing
ASSIGNMENT OPERATORS
c=c+3;
c +=3;
The += operator adds the value of the expression on the right of the operator to the value of
variable on the left of the operator and stores the result in the variable on the left of the
operator
variable operator=expression;
PSEUDOCODE
FLOW CHART
1. Create an Algorithm
2. Create a Pseudocode
3. Convert Pseudocode into a Python Equivalent Program
1. CREATE AN ALGORITHM
• STEP 1 -START
• STEP 2 -declare 2 float sales and salary
• STEP 3 -define values of initial sales and salary
• STEP 4 -obtain new values for sales
• STEP 5 -compute for the salary using the equation given
• STEP 6 -store and print the salary
• STEP 7 -repeat STEP 1 if the sentinel is not pressed
otherwise STOP
2. CREATE A PSEUDOCODE
• Input sales in dollars
• While the sales is not equal to -1
• Compute for the salary = 0.09*sales + 200
• Print the sales in 2 decimal places
• Repeat procedure #2 otherwise print PROGRAM ENDED
3. PYTHON Program
Intellectual Property of: Engr. James Francis B. Aguilar and the University of Nueva Caceres
Intended for the use in the UNC Curriculum of COMPUTER FUND & PROGRAMMING
Any unauthorize production of these materials is prohibited and punishable by Intellectual Property Act
of the Philippines