Data Types, User Input

Rupali Baravkar
7 min readMay 12, 2021

--

Introduction to Python Data Types

Data types are nothing but the different types of input data accepted by a programming language for defining, declaring, storing and performing mathematical & logical values/ operations. In Python, there are many data types used for dealing with the general operations on the program developer’s input data. A few of the commonly used data types are numbers for numeric values, String for single or series of characters, Tuple for a combination of different data types, List for a collection of values, etc.

Example of Python Data Types

Let’s see some examples to store different data types of value into the variables and check their type.

Code

var1 = 20
var2 = 20.65
var3 = "Hello!, World "
print( type(var1) );
print( type(var2) );
print( type(var3) );

Output:

Top 6 Python Data Types

The standard data types of python are given below :

Python Data Types

1)Numbers 2)String 3)Tuple 4)List 5)Set 6)Dictionary

1. Numbers

When a number is assigned to a variable Number class object is created.

Consider an example: var a = 100, var b = 200 # var a and var b number are assigned and these are objects of number. The Number can have 4 types of numeric data:

  1. int : int stores integers eg a=100, b=25, c=526, etc.
  2. long: long stores higher range of integers eg a=908090999L, b=-0x1990999L, etc.
  3. float: float stores floating-point numbers eg a=25.6, b=45.90, c=1.290, etc.
  4. complex: complex stores numbers eg a=3 + 4j, b=2 + 3j, c=complex(4,6), etc.

2. String

The string can be defined as the sequence of characters represented in the quotation marks. In python, the string can be quoted by single, double, or triple quotes. In python, various inbuilt operators and functions are available to work with the string data type easily.

The following example shows the string handling with inbuilt operators and functions:

Code:

s = 'hello! how are you' # s is string variable
print (s[1]) # index operator - printing second character, character start storing from index 0
print (s[2:6]) # slice operator - printing 3rd character to 5th character of the string, the syntax of slice operator str[ start: end-1: increment] print (s*3) # printing the string three times
print (s[2:12:2])
s1 = 'hello world'
print (s + s1) # printing the concatenation of s and s1

Output:

3. Tuple

Tuples also store the collection of the elements of different data types. A tuple is the same as the list, but a tuple is immutable (non-editable or cannot modify the size and elements value). To create a tuple uses the () simple parenthesis; within this brackets, stores all the elements separated with the comma (,).

The following example shows the tuple handling:

Code:

tp = ("apple", "a", 100, 20.78)
print (tp[1])
print (tp[1:])
print (tp[:3])
print (tp)
print (tp + tp)
print (tp * 3)
print (type(tp))
tp[1] = "banana"
print (tp)

Output:

4. List

List stores a collection of different types of elements. The list is mutable (editable). It is the same as arrays in C, but the list stores elements of different data types. To create a list uses the [] square brackets; within these brackets, stores all the elements separated with the comma (,). We can use index[i], slice [:] operators, concatenation operator (+), repetition operator (*) etc., to works with the list the same as with the strings.

The following example shows the list handling:

Code:

ls = ["apple", "a", 100, 20.78] print (ls[1])
print (ls[1:])
print (ls[:3])
print (ls)
print (ls + ls)
print (ls * 3)
print (type(ls))
ls[1] = "banana"
print (ls)

Output:

5. Set

Set also stores the collection of the elements of different data types. A Set is the same as the list and tuple, but the set is immutable (non-editable or cannot modify the size and elements value), un order and stores only the unique elements. To create a set uses the {} curly brackets, within this brackets stores all the elements separated with the comma (,).

The following example shows the set handling:

Code:

st = {"apple", "banana", 100, 20.78}
# set cannot support indexing st[1] # set cannot support slicing st[1:] print (st)
print (st + st)# set cannot support concatenation
print (st * 2) # set cannot support repetition
print (type(st))
# set is immutable st[2] = "hi"

Output:

6. Dictionary

Dictionary is also stored in a collection of different data types elements in the form of key-value pairs. It is ordered, mutable and stores unique keys as a set. To create a set, uses the {} curly brackets same as a set, within this, brackets stores all the elements (key-value pair) separated with the comma (,).

The following example shows the set handling:

Code:

dc = {"fruits":["apple", "banana"],'qty':100}
print("Fruits: ",dc['fruits'])
print("Quantity: ", dc['qty'])
print ("Dictionary: ",dc)# print all elements of the dictionary
print ("Keys: ",dc.keys()) # print all the keys of the dictionary
print ("values: ",dc.values()) # print all the values of the dictionary
print ("key value pairs: ",dc.items()) # print all the key values pair elements of the dictionary

Output:

Introduction to Python User Input

While programming very often, we are required to take input from the user so as to manipulate the data and process it thereafter. This ensures the program’s dynamism and robustness as it is not hardcoded and can successfully execute at any random value given by the user. In this topic, we are going to learn about Python User Input.

Methods of Inputting Data

The following are the ways of inputting data from the user: –

  • input()
  • raw_input()

Both basically do the same task, the only difference being the version of Python, which supports the two functions.

raw_input was used in older versions of Python, and it got replaced by input() in recent Python versions.

In this article, we would discuss input() as it is used in recent versions of Python.

Working of Python Input()

When we use the input() function in our program, the flow of execution is halted till the user inputs the data and clicks the Enter button. After that, the user’s value input is stored in some variable in the form of a string. No matter what data type the user intended their data to be, it gets stored as a string only. It needs to explicitly typecast to the desired format before use. Later we will look at such an example where if we do not typecast the data, it will result in an error.

Now let us have a look at the syntax of input()

Syntax

input([<prompt from user>])

Now let us see some examples and discuss how to use input() to accept data from users for different scenarios.

Example of Python User Input

Here are the following example mention below:

Example #1

Inputting normal text from the keyboard

Code:

string1 = input()
print(string1)

Output

Explanation

In the first line, we used the input() function to take input from the user and store it in the variable named string1. After we press the Enter button, we are instructed to provide our input by the show of a blank input box with a cursor blinking. When we provide the input and press the Enter button, then that value gets stored in the variable named string1. In the next line, when we are printing the variable, we actually see that the input provided by us is actually printed in the console.

Conclusion

Hence, we have covered the basic concepts of python data type & user input. data types are used to classify one particular type of data and we can say it’s very easy to take the user input in Python from the input() function.

References:

--

--

Rupali Baravkar
Rupali Baravkar

Written by Rupali Baravkar

Msc Computer science Student at ICCS, Pune.

No responses yet