PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • What is input() function?
  • Parameter to be passed in input() function:
  • Data Type of Values inputted by user:
  • Converting Data type of user input values:
  • Assignment:
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

4.User Input In Python

Previous3.Operators In PythonNext5.TypeCasting In Python

Last updated 2 years ago

What is input() function?

input() function is used to get input from the user. It takes input from the user and then it converts it into a string.

A program has meaning only if it interacts with user, in simpler terms, it takes instruction from user and execute itself based on it. To understand better , let's make a simple addition program.

In [3]:

num1 = 10
num2 = 20
add = num1+num2
print(add)
30

Now , no matter how many times you run this program output is always going to be 30. This program is not doing anything meaningful.

But what if user can give values of num1 and num2 , then this program has some value. Here comes user input. user input takes value from user and python operates on these values accordingly.

Parameter to be passed in input() function:

You can pass a 'string' under the brackets of input() functions as parameter , to let the user know what kind of information user need to give. For Example:

In [ ]:

company = input("Please Enter the Company name:")

Now passing a string in input() is completely optional , but it's a good practice as it tells user what values should he provide.

Data Type of Values inputted by user:

The default data-type of values inputted by user is String. Let's see:

In [ ]:

age = input("please enter your age:")

print(type(age))
<class 'str'>

Hence , no matter what value user enters , the default data type of the value will be String.

Can you tell me the value of add in this program?

In [ ]:

num1 = input("please enter first number: ")
num2 = input("please enter second number: ")
add = num1+num2

It will give you output : 1020.

Converting Data type of user input values:

As we have seen in previous example, failure of addition program because user input values were in string , so we need to convert data type of user input values with the help of type conversion, Something like this:

In [ ]:

num1 = int(input("please enter first number: "))
num2 = int(input("please enter second number: "))
add = num1+num2

print(add)
30

Note:You can convert user input values to any desired data type such as float,bool,str limiting the rules of Type casting that we have learned before.

Assignment:

  • Write a Program to calculate Simple interest with all the inputted user values.

  • Write a small calculator program with all the inputted user values.

  • Ask user's birthyear and calculate his age.