Page 249 - COPA VOL II of II - TP -Punjabi
P. 249
# taking two inputs at a time Number of boys: 10
a, b = input(“Enter two values: “).split() Number of girls: 15
print(“First number is {} and second number is {}”. Enter three values: 10 15 20
format(a, b)) Total number of students: 10
print()
Number of boys is : 15
# taking multiple inputs at a time
Number of girls is : 20
# and type casting using list() function Enter two values: 10 15
x = list(map(int, input(“Enter multiple values: “).split()))
First number is 10 and second number is 15
print(“List of students: “, x)
Enter multiple values: 10 15 12 13 15
output: List of students: [10, 15, 12, 13, 15]
Enter two values: 10 15
TASK 5: Output formatting
# Python program showing use of format() method # using format() method and referring a position of the
using format() method object
print(‘Welcome {} to “{}!”’.format(‘NIMI’, ‘NIMI’)) print(f”{‘NImi’} and {‘Portal’}”)
# using format() method and referring a position of the Output:
object
Welcome NIMI to “NIMI!”
print(‘{0} and {1}’.format(‘NIMI’, ‘Portal’))
NIMI and Portal
print(‘{1} and {0}’.format(‘NIMI’, ‘Portal’))
Portal and NIMI
# the above formatting can also be done by using Welcome nimi for “NIMI!”
f-Strings Although, this features work only with python
NImi and Portal
print(f”Welcome {‘nimi’} for \”{‘NIMI’}!\””)
TASK 6: Operators in output formatting
# Python program showing how to use # print exponential value
# string modulo operator(%) to print print(“Exponential: %10.3E” % (356.08977))
# fancier output Output:
# print integer and float value Integer : 1, Float : 2.00
print(“Integer : %2d, Float : %5.2f” % (1, 2.0)) Integer: 240, Boys : 120
# print integer value Octal: 031
print(“Integer: %3d, Boys : %2d” % (240, 120)) Exponential: 3.561E+02
# print octal value
print(“Octal: %7.3o” % (25))
IT & ITES - COPA - (NSQF ਸੋਧੇ - 2022) - ਅਿਭਆਸ 1.39.04 235