Python Data Types
Introduction
In this chapter, we will discuss Python Data Types.
Python Data Types
Python has many native data types that are listed as in the following:
- 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 parts and complex parts like 2 + 3i.
- String: String is 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: an Ordered sequence of value.
- Tuples: Ordered immutable sequence of value.
- Sets: Unordered bag of values.
- Dictionaries: Unordered bags of key-value pairs.
Boolean
Booleans are either true or false. There are two constants, True and False, that can be used to assign a 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
- a=True;
- print(a);
In Python, booleans can be treated as numbers. True value is 1 and a False value is 0. You can also do any operation between boolean values in Python.
Example 2
- a=True
- b=True
- print(a+b)
- a=False
- b=True
- print(a-b)
- a=True
- b=False
- print(a * b)
- a=True
- b=False
- print(a / b)
Integer
- a=12;
- print(a);
type(variable)
- a=12
- print(type(a))
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
- a=0O14
- print(a)
In Hexa-Decimal: You can represent any integer variable as a Hexa-decimal such as in the following:
a=0XC
or
a=0xC
Example
- a=0xC
- print(a)
Float
- PI=3.14
- print("Type of PI is: \n")
- print(type(PI)) #This will print type of PI
- print("\n\nValue of PI is : %f" %PI)
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
- import fractions
- x = fractions.Fraction(1, 3)
- print(x)
Output
Not only that, but 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 become (1/2).
Example
Output
- import fractions
- x = fractions.Fraction(6, 12)
- print(x)
"Note: You can not create a fraction with a zero denominator like 5/0 or 0/0"
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
Complex Type
- 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)))
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
- SomeData= [0, 200, 50, 25, 10, 255]
- values = bytearray(elements)#creating Byte array
- data = bytes(elements)#creating Byte data.
- values[0]=4
- values[1]=10
- data[2]=10 # This cannot be possible because Byte does not support item assignment.
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])
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])
Example 2
- list=['C','C++','Java','C#','Python']
- print(list[3:])# it will print : ['C#', 'Python']
- print(list[:3]) # it will print : ['C', 'C++', 'Java']
Lists are mutable: we can modify the list items after we created the list.
Example
- list=['C','C++','Java','C#','Python']
- list[3]='HTML' #we can modify list item
- print(list[3])
Tuples Type
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'# this is not possible because tuple is immutable
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
- 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'])
Conclusion
In the next chapter, you will learn about the print statements and escape sequences.
Author
Sourabh Somani
Tech Writter
47.6k
10.9m