IT ELECTIVE 1
I/O, CONDITIONAL
STATEMENT
REYNALDO G. ALVEZ
Python Input/Output
Two built-in functions can do that for us:
input()
The input() function differs slightly in Python 2 and Python 3. In Python
2, if you want to accept user input as a string, you have to use the
raw_input() function instead.
print()
The print() function is used to display information to users. It accepts
zero or more expressions as parameters, separated by commas.
Python Input/Output
Basic Input
syntax: input([“prompt”])
by default input receives a string
s = input() accepts string
name = input(“Enter your name: ”) accepts string
a = int(input(“Enter a 5 digit int: ”)) accepts int
d = float(input(‘Enter a 10 digit int: ‘)) accepts long
x = float(input(“Enter rate: ”)) accepts float
Python Input/Output
Basic Output
Syntax: print(expr1, expr2, expr3, … [,sep=“”])
by default sep=“\n” or new line making print() actually a println()
print(‘hello’) hello (cursor on next line)
print(x) value of x (cursor on next line)
print(2+3) 5 (cursor on next line)
print(1<2) True (cursor on next line)
print(a, ‘+’, b, ‘=‘, a+b) 1+2=3 (cursor on next line)
print(‘one’, sep=‘’); print(‘line’) oneline (no space)
print(‘one’, sep=‘ ‘); print(‘line’) one line (w/ space)
Python Input/Output
Output Formatting
align right '{:>10}'.format('test') test
align left '{:10}'.format('test') test
align center '{:^10}'.format('test') test
integer '{:d}'.format(42) 42
padded int '{:4d}'.format(42) 42
float '{:f}'.format(3.1415926) 3.141593
padded float '{:06.2f}'.format(3.1415926) 003.14
signed num'{:+d}'.format(42) +42
**>>>message='{:>10}'.format('test')
Triple Quotes
If you need to display a long message, you can use the triple-quote
symbol ( “””) to span your message over multiple lines.
For instance,
>>>print (“””Hello World. #you can press
Enter but still part of the line
My name is Thames and I am 21 years old.”””)
(Output)
Hello World.
My name is Thames and I am 21 years old.
This helps to increase the readability of your message.
Escape Characters
Sometimes we may need to print some special
“unprintable” characters such as a tab or a newline. In this
case, you need to use the \ (backslash) character to
escape characters that otherwise have a different
meaning.
For instance to print a tab, we type the backslash character before the
letter t, like this: \t. Without the \ character, the letter t will be printed.
With it, a tab is printed.
Hence, if you type
>>>print (“Hello\tWorld”)
Hello World
CONDITIONAL/BRANCHING/DECISION STATEMENT
Decision-making is the anticipation of conditions occurring during the execution of a
program and specified actions taken according to the conditions.
Statement & Description
if statements
An if statement consists of a boolean expression followed by one or more
statements.
if...else statements
An if statement can be followed by an optional else statement, which executes
when the boolean expression if FALSE.
nested if statements
You can use one if or else if statement inside another if or else if statement(s).
IF STATEMENT
Here is an example of a one-line if clause −
var = 100
if ( var == 100 ) : print ("Value of
expression is 100")
print ("Good bye!")
• a colon (:) appears at the start of a new block.
IF..ELSE STATEMENT
An else statement can be combined with an if statement. An else statement
contains a block of code that executes if the conditional expression in the if
statement resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at the most only
one else statement following if.
Syntax
The syntax of the if...else statement is −
if expression:
statement(s)
else:
statement(s)
Flow Diagram
Example
amount = int(input('Enter amount: '))
if amount<1000:
discount = amount*0.05
print ('Discount ',discount)
else:
discount = amount*0.10
print ('Discount',discount)
print ('Net payable: ',amount-discount)
THE ELIF STATEMENT
The elif statement allows you to check multiple
expressions for TRUE and execute a block of code as
soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional.
However, unlike else, for which there can be at the most
one statement, there can be an arbitrary number
of elif statements following an if.
Syntax (ELIF STATEMENT)
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
EXAMPLE:
amount = int(input('Enter amount: '))
if amount<1000:
discount = amount*0.05
print ('Discount',discount)
elif amount<5000:
discount = amount*0.10
print ('Discount',discount)
else:
discount = amount*0.15
print ('Discount',discount)
print ('Net payable: ',amount-discount)
NOTE:
Switch/Case Statement
Core Python does not provide switch or case
statements as in other languages.
We can use if..elif...statements to simulate switch case