Input and Output Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Programming Element

Prepared By Dr Goh Wan Inn

Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1
Input
&
Output
Operations
The input Object
• Standard command for input prompt
• Used to call and read input from keyboard
• Input is stored in one or more variables

width = float (input("Enter width: "))

input command
Reading Strings with input
• Can be used to read and store in a string.

• Variable need to be declare to hold characters in string as


str:

country = str (input(“Enter Country”))

• country is name of variable, str is the data type


that could hold and store string characters.
Output Manipulators

• Some affect values until changed again:

– Round decimal using round ()

area = round (area,2)


Output Prompt/Display Manipulators

• Display result:

– Using method .format(value,value)

length = 10
height = 11
area = length * height

print(“The area of rectangular with the


length of {} width of {} is
{}”.format(length,height,area))
❑ Type Conversation
❑ Name Constant
Type Coercion

• Type Coercion:
automatic conversion of an operand to
another data type
• Promotion:
convert to a higher type
• Demotion:
convert to a lower type
Example

In the beginning, answer is in float.


Variable num1 automatically However, command of int force
converted into float data type the result to be promoted into int
(coercion) data type.
When You Mix Apples and Oranges:
Type Conversion
• Operations are performed between operands
of the same type.
• If not of the same type, python will convert
one to be the type of the other
• This can impact the results of calculations.
Mathematical Library Functions
• Require import math header file and used
with math extension
• Commonly used functions:
sin Sine
cos Cosine
tan Tangent
sqrt Square root
log Natural (e) log
abs Absolute value (takes and returns an int)
Library & Extension math
• Some mathematical function require to import extension math and used it in
the program.

# Use of math extension


import math
num=12
new_num = (math.sqrt (num))
print (new_num)

Output: 3.4641016151377544
Library & Extension math

• Some mathematical function require to import


extension math and used it in the program.
Method 1 Method 2
Hand Tracing a Program
Hand Tracing a Program

Hand trace a program: act as if you are Useful to locate logic or mathematical
the computer, executing a program: errors
step through and ‘execute’ each statement, one-by-
one
record the contents of variables after statement
execution, using a hand trace chart (table)

You might also like