Python Data Types
Python Data Types Codigence
Standard Data Types
Python has Six standard data types −
- Numbers
- String
- List
- Tuple
- Set
- Dictionary
Python Numbers
Number data types store numeric values. There are three types of numeric values in python:
- Integer (int)
-
Integers are the whole numbers consisting of + or – sign with decimal
digits like 100000, -99, 0, 17. While writing a large integer value, don't
use commas to separate digits. Also integers should not have leading
zeros.
When we are working with integers, we need not to worry about the size of integer as a very big integer value is automatically handled by Python. When we want a value to be treated as very long integer value append L to the value. Such values are treated as long integers by python.
Note: We can know the largest integer in our version of Python by following the given set of commands:
Integers contain Boolean Type which is a unique data type, consisting of two constants, True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty.import sys print sys.maxint
- Floating
-
Numbers with fractions or decimal point are called floating point
numbers.
A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a number in engineering/ scientific notation.For example -3.0X109 will be -3.0e9 and 3.0X10-9 will be 3.0E-9 - Complex
- Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by „j‟ instead of „i‟, so 1+0j denotes zero imaginary part.
Example :
a = 12 #int
al = 5289L #long integer
ab = True #Boolean
b = 10.2 #float
c = 10+2j #complex
print(a,type(a))
print(al,type(al))
print(ab,type(ab))
print(b,type(b))
print(c,type(c))
Python Strings
String is an ordered sequence of letters/characters. They are enclosed in
single quotes („ ‟) or double („‟ “). The quotes are not part of string. They only tell the computer where the string constant begins and ends. They can have
any character or sign, including space in them. These are immutable data types.
Subsets of strings can be taken using the slice operator ([ ] and [:] ) with
indexes starting at 0 in the beginning of the string.
Example :
txt = "Hello World"
print(txt) #whole string
print(type(txt))
print(txt[0]) #1st Character
print(txt[2:6]) #3rd to 7th Char
Python Lists
List is also a sequence of values of any type. Values in the list are called elements / items. These are mutable and indexed/ordered. In a list items are separated by commas and enclosed within square brackets ([]).
Example :
li = ["abcd",22,"Ram",10.3,"hello"]
print(li) #whole list
print(type(li))
print(li[0]) #1st item
print(li[2:4]) #3rd to 5th items
Python Tuples
Tuples are a sequence of values of any type, and are indexed by integers.
however, tuples are enclosed within parentheses - ( ).
Tuples can be thought of as read-only lists.
Example :
tuple = ("abcd",22,"Ram",10.3,"hello")
print(tuple) #whole tuple
print(type(tuple))
print(tuple[0]) #1st item
print(tuple[2:4]) #3rd to 5th items
Python Sets
Sets are used to store multiple items in a single variable.
A set is a collection which is both unordered and unindexed.
Sets are written with curly brackets { }.
Example :
my_set = {"html","python","php","js"}
print(my_set)
print(type(my_set))
Python Dictionary
Python's dictionaries consist of key-value pairs.
Dictionaries are enclosed by curly braces { }.
Example :
dict = {
'name':'Ram',
'subject':'Computer',
'country':'India'
}
print(dict)
print(type(dict))
print(dict['name']) #name value
print(dict['country'])
Post a Comment