Programming Element
Prepared By Dr Goh Wan Inn
Example of Simple Calculator Program
using Python Language
# This program is to make a simple calculator comment
import os
print (“Additional Calculator of Two Input Number")
print ("")
Display
prompt
num1 = float (input("Enter first number: ")) Gathering input
num2 = float (input("Enter second number: ")) prompt
sum=num1+num2 Process statement
print (num1, "+", num2, "=", sum) Displaying output
os.system ("PAUSE")
Computer Programming 2
The print Object
• This produces multiple line of output:
print (“I am ”)
print (“Iron Man”)
Computer Programming 5
The print Object
• Another way of producing multiple-line text using
\n
print (“I am\nIron Man ”)
Computer Programming 6
The output Object
Computer Programming 8
Computer Programming 9
Computer Programming 10
Variables and Literals
Variables
• Variable: a storage location in memory
– Has a name and a type of data it can hold
– Python allow programmer use variable without
declaring it.
– Can directly assigned/initialized any value into those
variables.
– Automatically select appropriate data type according to
the value.
Variable automatically assigned as int data type
num1 = 10
11
Variables and
Literals Variables
Way of declaring variable
while asking for input.
Variable num1 assigned to
hold float data type
Computer Programming 12
Variables and Literals
• Literal: a value that is written into a program’s code.
"hello, there" (string literal)
12 (integer literal)
Computer Programming 13
Variables and Literals
15 is an integer literal
This is a string literal
Output Display
Total number of green apple is 15
Computer Programmng
14
Identifiers
• An identifier is a programmer-defined name for
some part of a program: variables, functions, etc.
• You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
15
Variable Names
• A variable name should represent the purpose of
the variable. For example:
itemsOrdered
The purpose of this variable is to hold the number of
items ordered.
16
Identifier Rules
• The first character of an identifier must be an
alphabetic character or and underscore ( _ ),
• After the first character you may use
alphabetic characters, numbers, or
underscore characters.
• Upper- and lowercase characters are distinct
17
Rules of naming variables
Explanation Example
Variable name should START
either with letter of underscore. score, _number
Cannot start with number.
The reminding character CAN
consist of letters, numbers and total_sales, marks1
underscore.
Should NOT made of reserved and (reserved word),
words and contain any symbols. password& (contain symbol)
May NOT contain spaces of total sales (not valid since has
naming variable names. space between total & sales)
Names are CASE SENSITIVE with totalsales is not same with
uppercase and lowercase. Totalsales
Computer Programming 18
Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
total.Sales No Cannot contain .
4thQtrSales No Cannot begin with digit
totalSale$ No Cannot contain $
Computer Programming 19
Data Types
Numeric Data Type Character Data Type
❑int ❑string (str)
❑float
❑long Bool Data Types
❑complex ❑True
❑False
Computer Programming 20
Numeric Data Type - int
• Plain integers of positive or negative whole
numbers
• E.g. : 10, -10
• Assign for suitable variable application, such
as number of student (num_student),
number of car (num_car) and etc.
num_student = int (input(“Enter number of student =“)
Computer Programming 21
Numeric Data Type - long
• Long integer with infinite size
• Similar with int, except the are followed by
letter “L”
• E.g. : 10L, -10L
Computer Programming 22
Numeric Data Type - float
• Represent real numbers.
• E.g.
12.45 -3.8
• Stored in a form similar to scientific notation (6.022e23)
• All floating-point numbers are signed
Computer Programming 23
Numeric Data Type - complex
• Use to represent complex number
• Represent by formula a+bi, where a and b are floats, while
i is the
−1
• E.g. : 10+28i
Computer Programming 24
Character Data Type - str
state = str (input("Enter State: "))
print (“State is: ", state)
state = “Singapore”
print (state)
Computer Programming 25
bool Data Type
• Represents values that are true or false
• bool variables are stored as small integers
• false is represented by 0, true by 1:
bool allDone = true;
bool finished = false;allDone finished
1 0
Computer Programming 26
Variable Assignments
• An assignment statement uses the = operator to
store a value in a variable.
item = 12;
• This statement assigns the value 12 to the item
variable.
• The variable receiving the value must appear on
the left side of the = operator.
• This will NOT work:
// ERROR!
12 = item;
Computer Programming 27
Variable Initialization
• To initialize a variable means to assign it a
value and automatically defined with
appropriate data type
length = 12; #Integer data types
Computer Programming 28
Multiple Variable Assignment in the Same Line
Computer Programming 29
Arithmetic Operators
• Used for performing numeric calculations
• C++ has unary, binary, and ternary
operators:
– unary (1 operand) -5
– binary (2 operands) 13 - 7
– ternary (3 operands) exp1 ? exp2 : exp3
Computer Programming 30
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF
ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Computer Programming 31
A Closer Look at the / Operator
Computer Programming 32
Program Documentation / Comments
Computer Programming 33
Computer Programming 34
Computer Programming 35
Computer Programming 36
Program to calculate area of rectangular
37
And now??
• Add on the program to
calculate perimeter.
Computer Programming 38
Computer Programming 39
Let’s go for extra miles!
Change to calculate the width required
for an area of rectangular
Hint: ask the area and length required
from user.
Area = length*width
Computer Programming 40