Input Output & Control Flow Statements
Input Output & Control Flow Statements
Example:
For int: a=int(input('Enter value:'))
For float: a=float(input('Enter value:'))
For complex: a=complex(input('Enter value:'))
For bool: a=bool(input('Enter value:'))
For string: a=input('Enter value:')
For list, tuple, set, dict: a=eval(input('Enter value:'))
Example: Create a File -> enter input -> Run Module -> She'll output
Input Output
>>>print(10,20,30) 10 20 30
>>>print(10,20,30, sep=',') 10,20,30
>>>print(7, 5, sep='#') 7#5
>>>print(10,20,30, sep=',', end='@') 10,20,30@7#5
print(7, 5, sep='#')
1. Write a program to find product of two integer numbers.
a= int(input('Enter a:'))
b= int(input('Enter b:'))
print('Product of a and b : ', a*b)
Control statements:
These are the statements used to control the flow of execution of a program.
Control statements are of two types:
1. Conditional/decisional statement
2. Looping statement
Conditional statement
It controls the flow of execution based on some condition.
Types:
★ Simple if
★ if else
★ elif
★ nested if
Looping statements:
it controls flow of execution by repeating the same instruction execution for n number of
times.
Types:
● While loop
● for loop
Simple if:
● If is a keyword.
● it's used to check condition .
● if condition is true, then control will execute true statement block.
● if condition is False, then control will ignore executing true statement block.
● Syntax and flow diagram
if char in 'aeiouAEIOU':
print('Vowel')
5. Write a program to check whether the given data is of single value data type
n = eval(input('enter the data:'))
if type(n)==int or type(n)==float or type(n)==complex or type(n)==bool:
#or
if type(n) in [int, float, complex, bool]:
#or
if type(n) not in [str, list, tuple, set, dict]
print('single value data type')
If else :
For a single condition when we have two set of statements we use if else condition.
★ In this case if condition is true , then true statement block will be executed.
★ if condition is false , then false statement block will be executed.
★ Else is a default block and writing condition for else is not required.
★ Syntax and flow diagram
Elif Statement:
● Whenever there are multiple conditions and a set of statements for each and every
condition, then elif statement should be used.
● this will start from if and end with else block
● in this case, control will check other condition only if condition 1 is False ; and same
for all.
● Is none of the conditions are true, then else block will get executed by default .
● in this statement, writing else block is optional.
. .
Nested if:
Writing if condition inside another if condition is called as nested if.
Note: here the nested if will be executed only if the 1st condition is true.
Syntax and flow diagram:
. .
Ex: Write a program to check if the given integer is multiple of 5 only if it is an even number.
n=int(input ('Enter'))
if n%2==0:
if n%5==0:
print(n,' Is an even multiple of 5 ')
else:
print(n,' Is even , but not a multiple of 5 ')
else:
print(n, 'is not an even number ')
2. Write a program to check greater among three numbers using nested if.
a,b,c=int(input (()), int(input (()), int(input (())
if a>b:
if a>c:
print (a)
else:
print(c)
else:
if b>c:
print (b)
else:
print(c)
#Pr, check if the user can login to instagram by entering proper credentials
oun='ABC'
opwd='123'
un=input('Enter Username:')
pwd=input('Enter Password:')
if un==oun:
if pwd==opwd:
print('Logged in Successfully')
else:
print('Incorrect Password')
else:
print('User does not exist')
Looping statements
While loop:
● It is a looping statement which is used to execute the same set of instruction
repeatedly until the given condition becomes False.
● In while loop , initialisation and updation of looping variable is mandatory.
● Using while loop, efficiency of program can be increased by reducing code repetition
(or code redundancy)
● Syntax and flow diagram:
(TBS: true statement block)
. .
Example:write a program to print "Python" 5 times
i=0
while i<=5:
print ('Python')
i=i+1 #i+=1
i=1
while i<=100:
if i%2==0:
print(i)
i=i+1
Write a program to reverse the given in teacher without using slicing or Type casting.
(Here to get the last digit we can use modulus)
(Two eliminate the last decimal digit we can use floor division)
n= int(input ('enter a number:'))
rev=0
while n!=0
ld=n%10
rev=rev*10+ld
n=n//10
print (rev)
s=input('Enter')
out=''
i=0
while i<len(s):
if 'A'<=s[i]<='Z':
out=out+s[i]
i+=1
print(out)
Write a program to extract all special characters from the given string.
s=input ('Enter a string ')
out=''
i=0
while i< len(s):
if not('A'<=s[i]<='Z' or 'a'<=s[i]<='z' or '0'<=s[i]<='9' ):
out+=s[i]
i+=1
print (out)
Note :
ord():
Is a function which is used to get ASCII value for a particular character.
Syntax: ord(character)
>>>ord('A')
65
chr():
It is a function which is used to get character of the mentioned ASCII value.
Syntax: chr(value)
>>>chr(65)
'A'
abs():
This function is used to convert any negative integer to positive integer.
>>>n=-23
>>>abs(n)
23
Ex:
>>>tuple(range(10,1-1,-1))
(10,9,8,7,6,5,4,3,2,1)
>>>tuple(range(10,0,-1))
(10,9,8,7,6,5,4,3,2,1)
>>>list(range(0,5+1,1))
[0,1,2,3,4,5]
>>>list(range(0,6,1))
[0,1,2,3,4,5]
>>>list(range(6))
[0,1,2,3,4,5]
>>>list(range(2,11,2))
[2,4,6,8,10]
For loop:
● Used to execute the same set of instructions repeatedly.
● initialisation and updation of looping variable is not required
● it is possible to get the values directly from the collection using for loop.
● this can be used only if we know the number of iterations.
for i in 'python':
print (i )
for i in [10,20,30]:
print (i )
for i in (1,2,3):
print (i )
for i in {10,20,30}:
print (i )
Write a program to find sum of all the integers present in a given list
L=eval(input('Enter List of integer '))
s=0
for i in L:
if type (i)==int:
s=s+I
print (sum)
Write a program to extract all characters from string only if its ASCII value is 3 digit
number.
s=input('Enter a string:')
out=''
for i in s:
if 99<ord(i)<1000:
out+=i
print (out)
Write a program to find the length of collection without using length function.
x=eval(input(' values:')
len=0
for i in x:
len+=1
print(len)
Split function:
It is an inbuilt function used to convert the given string into list of words.
This works only on string collection.
Syntax: split()
● var.split()
Here the default split happens for every space.
● var.split(char)
Here it's splits at the specified character.
Example:
>>>s='Python is very easy'
>>>s.split()
['Python', 'is', 'very', 'easy']
>>>s.split('y')
['P', 'thon', 'is', 'very', 'eas', ' ']
>>>s.split(' ',2)
['Python', 'is', 'very easy ']
>>>'_'.join(a)
'Python_is_very_easy'
s=input('Enter a String')
out={}
a=s.split()
for i in a:
out[i]=len(i)
print(out)
#or
s = input('Enter the Value : ')
print(s.split()[::-1])
print(' '.join(s.split()[::-1]))
#or
print(input('Enter:').split()[::-1])
print(' '.join(input('Enter:').split()[::-1]))
#Pr, Find length of a collection without using len()
Break:
It is a keyword which is used to stop the loop in between.
As soon as a control findals a keyword break, it stops a loop immediately and it will not go
back to the same loop for further instruction execution.
Example:
for i in range(1, 11):
print( i)
break
output: 1
Write a program to check if the given list contains only integer in it.
l=eval( input ('enter list :'))
for i in l:
if type(I)!= int:
print(" other than integer")
break
else:
print ('only integer ')
Note: if we write if condition inside for loop, then else condition should be written outside
the for loop
num=678
while True:
n=int(input('Enter the Number:'))
if n==num:
print('Correct')
break
elif n<num:
print('Please enter greater number')
elif n>num:
print('Please enter lesser number')
#Pr, check if the str contains only lower case char in it.
s=input('Enter a string')
for i in s:
if not('a'<=i<='z'):
print('Other than lower case char is present')
break
else:
print('String has only lower case char')
Continue:
It is a keyword which is used to skip the current execution and will make the control to go for
next iteration execution.
continue can be used in both while and for loop.
for i in range(1, 11):
if i==5 or i==7:
continue
print(i)
l=eval(input('List please:'))
out=[]
for i in l:
if type(i)!=int: #if not(type(i)==int):
continue
out+=[i]
print(out)
Pass:
pass is a keyword which is used to keep an empty statement block .
it is used whenever it is required to keep an empty statement block, because if the statement
block is not present then the program will throw error , so to avoid that we use pass
statement.
a=10
b=20
if a<b:
pass
pass
#Assignment:
#Simple if
#if else:
#Pr, print 'True' if str has >5 char, else print 'False'
# elif:
# * print all char present in even index, if str has even no. of char
#Nested if:
#while loop
#Pr, Print all natural nos, between 1 to n only if it is multiple of 3 and 5
#Pr, extract all str from a list only if the str has 3 char in it.
#Pr, Extract all special char, only if its ASCII value is 2digit numbers