Skip to main content

Posts

Python - Output Methods

 print() function is used to output any value or message to the console or any device. Syntax: print(value(s), sep=' ', end='\n', file=file, flush=flush) Parameters: values: Any values.  sep: (optional) Separator. Default:' ' end: (optional) Specifies what to print at the end. Default: '\n' file: (optional)An object with write method. Default: sys.stdout flush: (optional) A bool to specify if the out is flushed (if True) or buffered (if False). Default: False Returns: It returns output to the screen end=" " It specifies what to print at the end of execution of print() statement. Default: "\n" Example: Program1: print("I love to code") print("I'm a programmer", end=". ") print("I'm an expert in python") Output: I love to code I'm a programmer. I'm an expert in python Program2: #print a list values without new line a = [1, 2, 3, 4] for i in range(4):     print(a[i], end =" ...

Python - Input Methods

In this blog we will see how to get input value from console, typecasting input to integer/float/string, different ways to get multiple inputs from console and the common errors we make in using these input methods in python.   How to get the input from console? input() function is used to get input from console. When the input() is used in program, program execution flow will be stopped until user gives the input Whatever user enters as input, will be converted to string.  If we get any integer or float value, typecasting should be done. Syntax:  input(prompt) Example: name = input("Enter your name") How to typecast input to integer/float/string? Syntax: int(input()) float(input()) str(input()) Example: a = int(input()) b = int(input()) print(a+b) How to get multiple input from user in Python?  There are two methods to achieve this. Using split() method Using List comprehension Using split() method: split() method used to get multiple inputs from user and split the ...