You are on page 1of 28

Input/Output

 used to output stuff to console


 keyword is print

x = 1
print(x)

x_str = str(x)

print("my fav num is", x, ".", "x =", x)

print("my fav num is " + x_str + ". " + "x = " + x_str)

2
• print() - throws cursur to next line
• print(“string”) - prints string on screen
• We use repetition operator (*) to repeat the strings in
the output as
• print(3 * 'Hi') - prints HiHiHi
• print(variables list)
• a,b = 2,4
• print(a,b) 24
• print(a,b, sep=':') 2:4
print(object) statement
• We can pass objects like lists,tuples or dictinaries to the print function
to display the elements of these objects
• lst = [ 10,'d','hi']
• print(lst) prints [ 10,'d','hi']

• print(“string”, variables list) statement


print(formatted string) statement
• The output can be formatted using the special symbol
%
• print(“formatted string” % (variables list))
• In the formatted string we can use
• %i or %d for decimal integers,
• %f for float numbers,
• %s for string and
• %c for characters
Example
>>> x,y = 10, 15
>>> print('x = %i y = % d' % (x,y)) #for single variable we don't require ()
>>> x = 10 y = 15

>>> name = 'Dhruv'


>>>print('Hi %c, %c %s' % (name[0], name[1], name))
>>>Hi D,h Dhruv
input
input : Reads a string from the user's keyboard.
• reads and returns an entire line of input *

>>> name = input("Hi. What's your name?")


Hi. What's your name? Paras Hilton

>>> name
'Paras Hilton'
INPUT/OUTPUT: input("")
 prints whatever is in the quotes
 user types in something and hits enter
 binds that value to a variable
text = input("Type anything... ")
print(5*text)

input gives you a string so must cast if working


with numbers
num = int(input("Type a number... "))
print(5*num)

8
input
• to read numbers, cast input result to an int or float
• If the user does not type a number, an error occurs.
• Example:
age = int(input("How old are you? "))
print("Your age is", age)
print(65 - age, "years to retirement")

Output:
How old are you? 53
Your age is 53
12 years to retirement
• x=input("Enter First number:")
• y=input("Enter Second Number:")
• i=int(x)
• j=int(y)
• print("The Sum:", i+j)

• Or else even we can simplify the above program:


• x=int(input("Enter Fisrt number:"))
• y=int(input("Enter Second Number:"))
• print("The Sum:", i+j)
eno=int(input("Enter the Emplyoyee number:"))
ename=input("Enter the Employee name:")
esal=float(input("Enter the employee salary:"))
eaddr=input("Enter the employee address:")
married=bool(input("Is employee married?[True|False]:"))

print("Please confimr your provided information")


print("Emplyoyee number:", eno)
print("Employee name:", ename)
print("Employee salary:", esal)
print(" Employee address:", eaddr)
print("Employee married?:", married)
Converting numbers from other systems into
decimal systems
OUTPUT
str = input('Enter Hexadecimal number...') Enter Hexadecimal number... a
n = int(str,16) #inform number is base 16
print('Hexadecimal to decimal is = ',n)
Hexadecimal to decimal is = 10

str = input('Enter Octal number...') Enter octal number... 10


n = int(str,8) #inform number is base 8 octal to decimal is = 8
print('Hexadecimal to octal is = ',n)
Enter binary number... 1101
str = input('Enter binary number...')
n = int(str,2) #inform number is base 2 binary to decimal is = 13
print('binary to decimal is = ',n)
To accept more than one input in the same line we can use a loop along
with input() function. e.g.

a,b = [int(x) for x in input(“Enter two numbers: “). split()]

split function by default splits the values where a space is found


square brackets indicate that input is accepted as elements of a list
#Accepting three numbers
a,b,c = [int(x) for x in input(“Enter three numbers: “).split()]
print('sum = ', a+b+c)
Enter three numbers: 23 50 80
sum = 153

a,b,c = [int(x) for x in input(“Enter three numbers: “).split(',')]


print('sum = ', a+b+c)
Enter three numbers: 23,50,80
sum = 153
Accepting a list of strings
lst = [x for x in input(“Enter strings: “).split(',')]
print('You Entered: \n', lst)

Enter Strings: World, globe, circle


You Entered:
[‘World’, ‘globe’, ‘circle’]
eval() Function
• takes a string and evaluates the result of the string by taking it as
python expression. e.g.

a,b = 10,20
result = eval(“a+b-10”)
print(result)
20
x = eval(input(“Enter an Expression...”)
print(“result = %d” % x)

Enter an Expression... 20 + 30 * 2
result = 80
Part A: Decision Control Statements

○ If
● If-else
● Nested if
● if-elif-else
Part B: Basic loop:
a. while loop
b. for loop
c. Nested loops
d. The break, continue , pass , else statement
used with loop
Decision Control Statements
Decision control statements usually jumps from one part of the code to another depending
on whether a particular condition is satisfied or not .
That is they allow to execute statements selectively based on certain decisions .
If statement
If-else statement
Nested if else statement
If-elif-else statement
If Statement
Syntax:
If test_Expression :
Statment1
……………
Statement n
Statment x
If Statement
x=5
if(x<10):
print(“x is smaller”)
if(x>20):
print(“x is greater”)
print(“Finish”)
Output : X is Smaller
Finish
Indentation importance
x=5
Print (“before 5”)
if(x==5):
print(“is 5”)
print(“is still 5”)
print(Third 5”)
print(“Afterword 5”)
print(“Before 6”)
if (x==6):
print(“is 6”)
print(“is Still in 6”)
print(“Third 6”)
print(“Afterword 6”)
If-else syntax

if(test condition ):
Statement block 1
else:
Statement block 2
Statement x
If-else statement

x=4
if(x>2):
print(“Bigger”)
else:
print(“Smaller”)
print(“All Done”)
Nested if statement

If statement can be nested that is, can be placed one inside the other .
If statement is the statement part of outer one.
if-elif-else
Note that it is not necessary that every if statement should have else block as
python supports simple if statement also.
After their first test expression or the first if branch , the programmer can
have as many elif branches as he wants depending on the expression that
have to be tested.
A series of if-elif statements can have a final else block.
if-elif-else statement
if(x<2):
print(“small”)
elif(x<10):
print(“Medium”)
else:
print(“Large”)
print(“done”)

You might also like