SAGUING EXAMPLES:
CS112 – COMPUTER PROGRAMMING 1 Good: spam eggs spam23 _speed
SEPTEMBER 21, 2022
Bad: 23 spam #sign var.12
TOPIC 2: ELEMENTARY PROGRAMMING
Diff: spam Spam SPAM
WRITING A SIMPLE PROGRAM
I. VARIABLE = EXPRESSION
Involves designing a strategy for solving the problem and then using II. We assign a value to a variable using assignment statement(=)
a programming language to implement that strategy. III. Assignment statement consists of an expression on the right hand
side and a variable to store the result
ALGORITHM
X = 3.9 * x * (1 – x)
Describes how a problem is solved by listing the actions that need to be
taken and the order of their execution. EXPRESSIONS
Translating into program or instruction or code.
Represents a computation involving values, variables, and operators
CODE that, taken together, evaluate to a value.
A variable can be used in both sides of the = operator.
When you write a program x=x+1 1=x is wrong
Radius = float(input(“enter a value for radius: “))
VARIABLE SIMULTANEOUS ASSIGNMENTS
Can evaluate all expressions simultaneously.
A name that references a value stored in the computer’s memory # Prompt the user to enter three numbers
It can be x, y, z or etc. Number1 = int(input(Enter the first number: "y
INPUT = FUNCTION TO ASK A USER TO INPUT A VALUE number2 = intlinput/"Enter the second number: ")
number3 = int(input(Enter the third number: "y)
IDENTIFIERS # Compute average
average = (numberl + number2 + number3) / 3
A sequence of characters that consists of letters, digits, and # Display result
underscores(_) print('The average of, number1. number2, number3, "is', average)
Must start with a letter or an underscore. Cannot start with a digit.
Cannot be a keyword. Ex. Import = tells a python to import a
NAMED CONSTANTS
module or program.
An identifier that represents a permanent value.
Can be of any length
There are three benefits of using constants:
Tip 1: Avoid using abbreviations for identifiers
o 1. You don't have to repeatedly type the same value if it is
Tip 2: Use lowercase letters for variable names. If a name consists of
used multiple times.
several words, concatenate them into one. Ex numberOfStudents = o 2. If you have to change the constant's value (e.g., from 3.14
camelCase
to 3.14159 for PI), you need to change it only in a single
Note: area, Area, and AREA are all different identifiers location in the source code.
o 3. Descriptive names make the program easy to read.
NUMERIC DATA TYPES AND OPERATORS
Integer(int) = representing whole numbers.
Real(float) = for representing numbers with a fractional part.