Python Data Types – Basic overview and Input Output
|Data types are particular kind of data items which can hold some special kind of values. Python provides various built-in data types with a wide range of inbuilt functions.
These are basic data types in python which we are used frequently during programming-
- Numbers – These data types store numeric values. It is of 3 types –
- int – These are the signed integers like 1, 1000, -987, -45, 0 etc.
- Float – These are floating point real values like 1.023, -678.9, 0.987 etc.
- Complex – These are complex numbers like 14.3j , 10.j etc.
- Strings – Strings are a sequence of Unicode characters like “codingeek”, an HTML document etc.
- Lists – Lists are ordered sequence of values i.e. a list can contain any type of object even different type of objects at the same time which are ordered and can be accessed based on their location in the list. Eg – [1, ‘codingeek’, ‘python’, 2]
- Sets – Set is an unordered collection of values(all types). This data type holds only unique values like [1, ‘codingeek’, True]
- Tuples – An immutable list is called a tuple. It can not be changed once it is initialized like (1, ‘codingeek’, True) (Note – ‘[]’ symbol in lists changes to ‘()’ in tuples).
- Dictionaries – An unordered collection of key value pairs is a dictionary. Key always has a unique value in dictionary. Example – {‘name’: ‘codingeek’, ‘category’ : ‘education-programming’}.
The biggest advantage of using data types in python is, while declaring variables, you don’t have to explicitly define what the data type is. It automatically detects the data type whenever we assign any value while initializing it.
When it comes to the numeric data types, Unlike C/C++, we have a bigger storage capacity in the int data type. It can store very big integers. There is no need for defining long. int in Python 3 is sufficient to handle even all the big & long integers.
Mutable & Immutable Objects-
A Mutable object has a property that it can be changed after it’s creation, but values inside an Immutable object cannot be changed after it is created.
Data types in Python can be classified into mutable and immutable data types. Giving an example, here’s a program explaining the case with Strings data structure in python –
Strings in python are immutable, i.e. the contents which are inside a string cannot be changed after its creation. The following example can clear this out.
a="Hello" a[0]="K" print(a)
Here we took the 0th index and tried to change it, but the above code would generate an error. Strings cannot be edited once they are created.
To achieve this, we have to convert a string into a mutable data type (list), change the value of the index, and then convert it back to a string . But, we will get to that later in the further articles.
Variables Declaration and Assignment
The following programs explain how to declare variables and how to assign values to them by input. The input/output format is also different for different data types.
The input of string can be done as follows –
a=input("Enter the String -") print(a)
Input:-
Enter the string - Hello
Output:-
Hello
Let us see the Input and Output of a numeric data type. Make a Python Program in which you will be given two integers, a & b, on a separate line, Print the sum of those two integers.
The code for that would be
a=int(input("Enter value for a - ")) b=int(input(" Enter value for b - ")) print(" Sum = ",a+b)
Input:-
Enter value for a - 2
Enter value for b - 3
Output:-
Sum- 5
In above program (Python 3)-
- input() – This function takes a whole line as input, in the form of a string.
- int() – The int() converts the string to an integer, and the inputs in the form of integers 2 & 3 are stored in a & b respectively. As you can see, Python implicitly makes these variables as integers. We didn’t explicitly specify during the variable declaration.
- print() – Then the print(a+b) prints the result of a+b=2+3=5. That’s how basic single integer input-output works.
Multiple Assignment –
In python, we can assign the same value to multiple variables as well as different values to different variables at the same time i.e. in a single statement. Example –
#Single value assignment to multiple variables x = y = z = 1 #Multiple value assignments to multiple variables x, y, z = 1, “codingeek”, “python”
The next article “Data Types – Lists” would cover a detailed explanation of Lists, which is one of the data types commonly used in Python.
Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.
Keep Learning… Happy Learning.. 🙂