Data Types in Python
Hey! Hope you are doing great.
Today let us see types of data types in python.
Variables are used to store values every value has a data type. Python is dynamically typed language so there is no need to define type of variable while defining which means we can directly assign a value to a variable with out declaring variable type like sum=0 but in other languages we need to declare the type of variable before assigning the value to a variable like int sum=0 but here we can directly assign like sum=0. Data types are the classification of data type.
In python we need to use input() to take input from the user but when user gives input it will consider it as string by default. So for int, float data types we need to convert them using built in functions like for int we need to use int(input()) for float need to use float(input())
Python provides various standard data types they are given below:
- Numbers
- Sequence Type
- Set
- Dictionary
- Boolean
Before the data types let us look what is type function - type(), type function is used to know type of a variable.
Example:
val=8
print(type(val))
Output is: <class 'int'>
Numbers:
As name indicates this data type represents the data with numbers. In this numbers/numeric data type it has 3 types integers, float, complex.
Integers: It can have positive or negative whole numbers but not decimal or fraction numbers In this we can have integers with any length, in python there is no length restrictions.
Example's for integer data type is:
number=100
a,b,c=1,2,3
a=b=c=0
negative=-12345
Float: It is used to store floating numbers such as 1.987, 156.02. It will be accurate up to 15 decimal points.
Example's for float data type is:
floatnum=125.976
a,b,c=1.75,52.14,6.21
Complex: A complex number contains an ordered pair it is x+iy. Where x denotes real part and y denotes imaginary part. (real part) + (imaginary part)j
Example's of complex data type:
complexnum=1+2j
complexnum=1+2j
Sequence Type:
As name indicates it is ordered collection of data types which may be of same type or different
Sequence type in python are:
- String
- List
- Tuple
1) String:
It is a sequence of character's represented in the quotes. Python supports single quotes-' ', double quotes- " ", and triple quotes-''' ''' to represent string. There is no character data type in python so a character is a string with length one. String handling in python is easy as it provides many built in functions. Every input from user using input() function the input will be in string by default.
The * operator is a repetition operator, 'a'*2 return 'aa.
The + operator is used to concatenate two strings.
2) List:
Lists are similar to arrays, Lists are ordered collection of data. In python lists we can store elements of different data types. List is denoted using closed brackets [] elements in list separated using comma (,). We can access list elements using index.
Suppose if l is an list with elements 1,2,3,4,5
l=[1,2,3,4,5] the index of 1 is 0, index of 5 is 4. so access the element 3 we need to use list[index] that is print(l[2]) to print element 3.
List is mutable we can change elements in list anytime we need.
We can use pop, remove built in functions to remove an element in list, append to add element at last of the list, insert to add element at specific position. Pop returns the element it deletes.
There are many other built in functions in python they are:
Here we can see some list functions.
3) Tuple:
Tuple is very similar to list but it is immutable that we can modify the elements. It is a collection of ordered elements of different data types. Elements are separated using comma (,) and tuple is denoted using ().
We cannot change the size and values of tuple.
Set:
It is unordered collection of data types, it is iterable, mutable- we can modify after its creation and has unique elements no duplication of elements is allowed. Every time you print it return the elements in different sequence as it is unordered. It is created using built in function set().
Built in functions in set are:
Dictionary:
Dictionary is an unordered set of a key-value pair of items. Other Data Types that hold only single value as an element, but Dictionary holds
key:value
pair. Key-value is provided in the dictionary to make it more optimized. The key and value are separated using colon : Dictionary's are created using curly braces separated by comma ','Syntax is:
dict={'key1':'value1','key2':'value2'}
Built in functions in dictionary is:
Boolean:
Boolean has two built in values True and False. These values are used to determine the given statement true or false. The true value in the Boolean context is called “truthy”, and for false value in the Boolean context, it is called “falsy”. True can be represented by non zero value and false can be represented by zero. It denotes by the class bool.
Sasi Kumar M :) 🐇
Comments
Post a Comment