Input and Output Operations in Python

Input and Output Operations in Python

A Program needs to interact with the user to accomplish the desired task; this is done using Input-Output facility. Input means the data entered by the user of the program. In python, we have input() and raw_input ( ) function available for Input.

Input Operation in Python

The Python programming language provides input() function to take input into a program from console.

input()

The simplest way to take input is using the input() function which first takes the input from the user, then evaluates the expression and finally converts the entered value into the string format (irrespective of format or type of entered value). The input() method accepts a string message which is an optional and meant to be displayed on the output console to ask a user to enter input value. Let’s have a look at the syntax

num = input ("Enter number:")   
print(num)    
# Printing type of input value   
print ("type of number", type(num))

You will need to typecast the input to the desired type because the entered value is always returned as string.

**split() **

Using split() method, we can take multiple values in one line. Split method breaks the given input by the specified separator. If separator is not provided, then any white space is a separator. Let’s have a look at the syntax and example to understand in a better way.

# taking two inputs at a time   
x, y = input("Enter a two value: ").split()   
print("Number of boys: ", x)   
print("Number of girls: ", y)   
print()  

Output Operation in Python

print()

Python programming language provides print() function to present the output of a program. The simplest way to present or display a program output to the console is using the print() function where you can pass zero or more expressions separated by commas. Let’s have a look at the example:

Example 1

Print two integers value

# printing two numbers at a time   
a=7   
b=9  
print(a, b)  

Example 2

Print string and integer value using print command

num = input ("Enter number :")   
print('number entered is : ', num)  

By default, python print() function use space ‘ ’ as separator between arguments passed in print() function but we can modify or use any character, number or string as separator. ‘sep’ (Optional) parameter in print() function can be used to specify the character or number or string as a separator to separate the arguments passed in print().

print('Male', 'Female', sep='/') will print both Male and Female separated by ‘/’  

Similarly, for formatting a date we can specify a dash ‘-’ as separator

#for formatting a date   
print('09','12','2018', sep='-')  

‘end’ (Optional) parameter in print() function can be used to specify the character or number or string to print at the end. Default is new line character ‘\n’.

# ends the output with a <space>    
print("Welcome to" , end = ' ')    
print("Python Progamming Course", end = ' '))   

‘file’ (Optional) parameter in print() function can be used to specify where the function should write a given object(s) to. If not specified explicitly, it is sys.stdout by default.

# Code for printing to a file   
welcomeFile = open("c:\\welcome.txt", 'w')     
print('python course', file = welcomeFile)   
welcomeFile.close()   

To verify the result, open the file and look for the entered content.

Till now we looked at the way to present the output of print the output using print() function but sometimes user often wants more control the formatting of output than simply printing. There are several ways to format output as mentioned below,

Formatting output using String modulo operator (%)

The String modulo operator (%) can be used for string formatting by adding a placeholder in the string which later replaced with the values in tuple. String modulo operator ( % ) can be used to replace any integer, float or string values. Let’s have a look at the example to understand better

# print integer and float value   
print("Integer : % 2d, Float : % 5.2f" %(1, 05.333))    

Use “%d” for integer value and “%f” for float value. In the above example “%2d” is used for the integer 1. The prefix ‘2” signifies that the number will be printed with 1 leading blank character as integer value 1 consists only of one digit. The second one “%5.2f” is a format description for a float number. the number following the “.” in our placeholder signifies the decimal part of the number or the precision, character “f” of our placeholder stands for “float”

Formatting output using format method

use {} to mark where a variable will be substituted and can provide detailed formatting directives. We can either use the exact position of variable to be substituted or empty {} will substitute the variables in the order mentioned. Let’s have a look at the example where we have used a position of the object

# using format() method and referring a position of the object   
print('{0} {1} {2}'.format('Python', 'Programming', 'Course'))  

In the above example, the brackets and characters within them (called format fields) are replaced with the objects passed into the format() method. A number in the brackets is used to refer to the position of the object passed into the format() method.

Conclusion

In this article we read about the different ways to take input and present output supported in Python programming language.

Thank you for reading!

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Best Way to Learn Python Programming Language | Python Tutorial

Best Way to Learn Python Programming Language | Python Tutorial

Worried that you have no experience in handling Python? Don’t! Python programming language teaching from Simpliv puts you right there to be able to write Python programs with ease. Place object-oriented programing in a Python context and use Python to perform complicated text processing.

Description
A Note on the Python versions 2 and 3: The code-alongs in this class all use Python 2.7. Source code (with copious amounts of comments) is attached as a resource with all the code-alongs. The source code has been provided for both Python 2 and Python 3 wherever possible.

What's Covered:

Introductory Python: Functional language constructs; Python syntax; Lists, dictionaries, functions and function objects; Lambda functions; iterators, exceptions and file-handling
Database operations: Just as much database knowledge as you need to do data manipulation in Python
Auto-generating spreadsheets: Kill the drudgery of reporting tasks with xlsxwriter; automated reports that combine database operations with spreadsheet auto-generation
Text processing and NLP: Python’s powerful tools for text processing - nltk and others.
Website scraping using Beautiful Soup: Scrapers for the New York Times and Washington Post
Machine Learning : Use sk-learn to apply machine learning techniques like KMeans clustering
Hundreds of lines of code with hundreds of lines of comments
Drill #1: Download a zip file from the National Stock Exchange of India; unzip and process to find the 3 most actively traded securities for the day
Drill #2: Store stock-exchange time-series data for 3 years in a database. On-demand, generate a report with a time-series for a given stock ticker
Drill #3: Scrape a news article URL and auto-summarize into 3 sentences
Drill #4: Scrape newspapers and a blog and apply several machine learning techniques - classification and clustering to these
Using discussion forums

Please use the discussion forums on this course to engage with other students and to help each other out. Unfortunately, much as we would like to, it is not possible for us at Loonycorn to respond to individual questions from students:-(

We're super small and self-funded with only 2 people developing technical video content. Our mission is to make high-quality courses available at super low prices.

The only way to keep our prices this low is to NOT offer additional technical support over email or in-person. The truth is, direct support is hugely expensive and just does not scale.

We understand that this is not ideal and that a lot of students might benefit from this additional support. Hiring resources for additional support would make our offering much more expensive, thus defeating our original purpose.

It is a hard trade-off.

Thank you for your patience and understanding!

Who is the target audience?

Yep! Folks with zero programming experience looking to learn a new skill
Machine Learning and Language Processing folks looking to apply concepts in a full-fledged programming language
Yep! Computer Science students or software engineers with no experience in Java, but experience in Python, C++ or even C#. You might need to skip over some bits, but in general the class will still have new learning to offer you :-)
Basic knowledge
No prior programming experience is needed :-)
The course will use a Python IDE (integrated development environment) called iPython from Anaconda. We will go through a step-by-step procedure on downloading and installing this IDE.
What will you learn
Pick up programming even if you have NO programming experience at all
Write Python programs of moderate complexity
Perform complicated text processing - splitting articles into sentences and words and doing things with them
Work with files, including creating Excel spreadsheets and working with zip files
Apply simple machine learning and natural language processing concepts such as classification, clustering and summarization
Understand Object-Oriented Programming in a Python context

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: