Standard Input Output Functions in python
Standard functions are the part of standard python libraries and can be used out of the box without need to import external python library.Some of the functions are:
o
print()
o
input()
o
len()
o
range()
o
min()
o
max()
o
sum()
o
etc
print():
In
Python, the print() function is used to display result on the
screen.
>>> print("Welcome to
Python Programming")
>>> x = 5
>>> y = 6
>>> z = x + y
>>> print(z)
input():
Ø It is used to gather input from the user and store it in the variable. It prompts for the user input and reads a line. After reading data, it converts it into a string and returns that.
Syntax: input ([prompt]) #where prompts is a string message which prompts for the user input.
>>> name = input("What is your
name?")
>>> print(name)
The program will now wait for the user to enter some value and user will be prompted with the text which is supplied inside input function. Once the user type the value, that value will get stored into the variable name
Ø This input function convert the whatever
the value entered into a string
# Python input() function example
# Calling function
val = input("Enter a value: ")
# Displaying result
print("You entered:",val)
len():
Ø The python len() function is used to return the
length (the number of items) of an object.
Ø
Syntax:
len(object)
Ø Where object: An object that must be a
sequence(string, tuple, list etc.) or a collection(dictionary, set etc.)
# The
below example shows the length of string.
>>> strA = 'OnlineSmartTrainer'
>>> print(len(strA))
>>> strA = 'Online Smart Trainer'
>>> print(len(strA))
#The
below example shows the length of list.
>>>
listA = ['Python', 'C', 'C++', 'Java']
>>> print(len(listA))
#The below example shows the length of
dictionary.
>>>
dictA = {
'name': 'Phill', 'university': 'GTU', 'ID':
1107
}
>>>
print(len(dictA))
range():
Ø Python range() function returns an immutable
sequence of numbers starting from 0, increments by 1 and ends at a specified
number.
Ø
Syntax:
range(start, stop, step)
where
start (optional) : It is an integer number that specifies the starting position. The Default value is 0.stop (optional) : It is an integer that specifies the ending position. step (optional) : It is an integer that specifies the increment of a number. The Default value is 1.
#
Empty range
>>>
print(list(range(0)))
#
using the range(stop)
>>>
print(list(range(4)))
# using the range(start, stop)
>>>
print(list(range(1,7)))
sum():
Ø As the name says, python sum() function is used to get the sum of
numbers of an iterable like list, tuples, and dictionaries, but an iterable
object must contain numbers.
>>>
s = sum([1,2,2])
>>>
print(s)
>>>
s = sum([1, 2, 2], 10)
>>>
print(s)
# This
example shows sum of floating point numbers:
>>>
s = sum([2.5, 2.5, 3])
>>>
print(s)
# This
example shows sum of complex numbers:
>>>
s = sum([1 + 2j, 3 + 4j])
>>>
print(s)
>>>
s = sum([1 + 2j, 3 + 4j], 2 + 2j)
>>>
print(s)
>>>
s = sum([1 + 2j, 2, 1.5 - 2j])
>>>
print(s)
min() and max():
Ø These
functions are used to find minimum and maximum values
Comments
Post a Comment