- Boolean: Ture or False
- Integer: Positive and Negative values like -1,-2, 0, 2, 8 and so on.
- Float: 0.2, 2.8 and so on.
- Fraction: 3/5, 2/7 and so on.
- Complex: Values that have real part and complex part like 2 + 3i.
- String: String are the sequence of Unicode characters
- Byte and Byte Array: This data type can save any binary files like Image(jpg, png, bmp and so on. )
- List: Ordered sequence of value.
- Tuples: Ordered immutable sequence of value.
- Sets: Unordered bag of values.
- Dictionaries: Unordered bags of key-value pair.
Boolean: Booleans are either true or false. There are two constants, True and False, that can be used to assign value directly. Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. We can write any expression in boolean contexts.
Example 1
In Python booleans can be treated as numbers. A True value is 1 and a False value is 0. You can also do any operation between boolean values in Python.
Example 2
Output
2Because Python treats a true value as 1 and a false value as 0. Here a and b both are true so 1 + 1 = 2.
Example 3
- a=False
- b=True
- print(a-b)
Output
-1
Example 4
- a=True
- b=False
- print(a * b)
Output
0
Example 5
- a=True
- b=False
- print(a / b)
Output
Integer
An Integer type holds an integer value or a whole number that can be a negative or positive value, like -5, -4, 0, 8, 9, 10 and so on. In the Python you can declare an integer value such as in the following:
Output
12But in the preceding example you cannot determine the type of the
a Variable.
So in Python we have a function by which we can get the type of a variable. The function is as in the following:
type(variable)
Example
We can represent any integer variable in Decimal or Octal or Hexa-Decimal format.
in Decimal
In decimal you can write directly your Python variable as in the following:
a=12
in Octal
In Octal you can represent such as in the following:
a=0O14
or
a=0o14
Example
In Hexa-Decimal: You can represent any integer variable as a Hexa-decimal such as in the following:
a=0XC
or
a=0xC
Example
Float
Float values are values that contain decimal points or a value that contains an exponent and mantissa.
Example
- PI=3.14
- print("Type of PI is: \n")
- print(type(PI))
-
- print("\n\nValue of PI is : %f" %PI)
Output
We can also represent a floating point number in
E notation.
Example
- a=1.5E+2
- electron_charge=1.602E-19
- speed_of_light=3.0E+8
-
- print(a);
- print(electron_charge)
- print(speed_of_light)
Output
Fraction
In Python you can also create a type that can hold fractional numbers, like 1/2, 3/5 and so on. To create a Fractional type of number you must import
the "fractions" module. Than you need to create a Fraction object.
Example
- import fractions
- x = fractions.Fraction(1, 3)
- print(x)
Output
Not only that, you can also operate fractional numbers.
Example
- import fractions
- x = fractions.Fraction(1, 5)
- y = fractions.Fraction(2, 5)
- print("%s + %s = %s"%(x,y,(x+y)))
Output
"Fraction objects are automatically reduced to fractions": Assume we have a number (6/12) then it will becomes (1/2).
Example
- import fractions
- x = fractions.Fraction(6, 12)
- print(x)
Output
"Note: You can not create a fraction with a zero denominator like 5/0 or 0/0"
Complex Type
In Python you can create a Complex type variable that will have a real part as well as an imaginary part, like 3 + 5i. Here the real part is 3 and the imaginary part is 5.
To create the complex type of variable in Python we have a function called complex(real_part, imaginary_part) that takes two arguments, a real part and an imaginary part.
Example
- x = complex(3,5)
- print(x)
Output
You can also perform any operation on this, like:
- x = complex(3,5)
- y = complex(5,6)
- print("%s + %s = %s"%(x,y,(x+y)))
Output: (3+5j) + (5+6j) = (8+11j)
String Type: the String datatype is the same as in other languages.
Example
- x="Hello C-Sharp Corner!"
- print(x)
Output
Byte and Byte Array
These objects can store binary buffers. The length of a Byte type is 1 byte, which is 8 bits. So its range will be 0 to 255. It means bytes can efficiently be one byte of data. A byte array is the type that can hold more than one byte. As we have discussed, a byte can hold the binary data so we can store any binary file in a byte array, like image, songs and so on.
We can create any bytearray and byte variable such as in the following:
- SomeData= [0, 200, 50, 25, 10, 255]
-
- values = bytearray(elements)
-
- data = bytes(elements)
In the bytearray we can modify the values as in:
But in the byte type we cannot modify the value.
Lists Data Type: Lists are ordered sequences of values.
Creating a list: Creating a list is very easy, you can create a Python list using square brackets and wrap comma-separated list items in square brackets.
Example
- list=['C','C++','Java','C#']
- print(list)
Output
You can also print the individual items of the list with its index like an array.
Example
- list=['C','C++','Java','C#']
- print(list[3])
The preceding example will print
"C#".
Slicing the list: Once you have created a list you can slice that list, in other words you can get any part of the list.
Example 1
- list=['C','C++','Java','C#','Python']
- print(list[1:4])
Output
Example 2
- list=['C','C++','Java','C#','Python']
- print(list[3:])
- print(list[:3])
Output
Lists are mutable: we can modify the list items after we created the list.
Example
- list=['C','C++','Java','C#','Python']
- list[3]='HTML'
- print(list[3])
OutputHTML
Tuples Type
A tuple is the same as a list but the difference is that a tuple is "immutable", in other words we cannot modify a tuple after creating it.
How to create: Tuple is created the same as a list but it is enclosed in parenthesis.
Example
- tuple=('C','C++','Java','C#','Python')
- print(tuple[3])
- tuple[3]='HTML'
Output
Sets: A set is a well-defined collection of distinct objects. This is also an immutable type. Once you have created a set you can perform set operations like union, intersection, and so on.
A set can be defined the same as a list but they are enclosed in curly braces ({}).
Example
- number_set={1,2,3,4,5,6,7,8,9}
- print(number_set)
Dictionaries
This is an unordered set of values that has two parts, the key and a value. In other words a Dictionary contains key-value pairs for fast lookup of the data. We can access our data faster using a Dictionary.
Example
- Person={'First_Name':'Sourabh','Last_Name':'Somani','Website':'http://www.sourabhsomani.com/'}
- print(Person)
- print(Person['First_Name'])
- print(Person['Last_Name'])
- print(Person['Website'])
I hope you like this. Thanks.