Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

The input function

Programs usually request for some user input to serve its function (e.g. calculators asking for what numbers to use, to add/subtract etc.). In Python, we request user input using the input() function.

Syntax

message = input("This is where you type in your input request: ")

This set of code is requesting for user input, and will store it in the message variable.

Note: Inputs are automatically saved as strings. Therefore, always convert to integers before doing any math operators like addition / subtraction. 

Example

Inputs are automatically saved as strings. Therefore, before executing any arithmetic operations such as addition and division, we need to convert inputs to integers. Recall type conversion in Section 1.3.

Here, we want to add 2 numbers chosen by the user. We have to:

1. Ask user to input the first number
2. Ask user to input the second number
3. Tell Python to add the numbers
4. Tell Python to print the result

Run the codes below and compare the difference. Can you tell what is it?

Before type conversion, Python concatenates the inputs and joins them together as a string data. After type conversion, Python recognizes the input as integers, and adds them together as numbers. 

Video Guide

Watch from 0:57 to 5:31

Exercises

Request for user input and print them out

1. Request for user's favourite fruit

2. Ask user how many family members they have

3. Ask user for 2 numbers and subtract them