A complex number has two parts, real part and imaginary part. Complex numbers are represented as A+Bi or A+Bj, where A is real part and B is imaginary part. First thing first, python uses We can create complex number from two real numbers. Syntax for doing this is: Where, a & b are of real data types and c will be of complex data types.
Let's try it out in python console: After creating complex data type, we can access real and imaginary part using built-in data descriptors We can read complex number directly from user using built-in function Output Complex data type has built-in method called Python supports direct addition, subtraction, multiplication and division using operator Data types in PythonEvery value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. There are various data types in Python. Some of the important types are listed below. Python NumbersIntegers, floating point numbers and complex numbers fall under
Python numbers category. They are defined as We can use the
Output 5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is complex number? True Integers can be of any length, it is only limited by the memory available. A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point number. Complex numbers are written in the form,
Notice that the Python ListList is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type. Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets
We can use the slicing operator
Output a[2] = 15 a[0:3] = [5, 10, 15] a[5:] = [30, 35, 40] Lists are mutable, meaning, the value of elements of a list can be altered.
Output [1, 2, 4] Python TupleTuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. Tuples are used to write-protect data and are usually faster than lists as they cannot change dynamically. It is defined within parentheses
We can use the slicing operator
Output t[1] = program t[0:3] = (5, 'program', (1+3j)) Traceback (most recent call last): File "test.py", line 11, in <module> t[0] = 10 TypeError: 'tuple' object does not support item assignment Python StringsString is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes,
Output This is a string A multiline string Just like a list and tuple, the slicing operator
Output s[4] = o s[6:11] = world Traceback (most recent call last): File "<string>", line 11, in <module> TypeError: 'str' object does not support item assignment Python SetSet is an unordered collection of unique items. Set is defined by values
separated by comma inside braces
Output a = {1, 2, 3, 4, 5} <class 'set'> We can perform set operations like union, intersection on two sets. Sets have unique values. They eliminate duplicates.
Output {1, 2, 3} Since, set are unordered collection, indexing has no meaning. Hence, the slicing operator
Python DictionaryDictionary is an unordered collection of key-value pairs. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. In Python, dictionaries are defined within braces
We use key to retrieve the respective value. But not the other way around.
Output <class 'dict'> d[1] = value d['key'] = 2 Traceback (most recent call last): File "<string>", line 9, in <module> KeyError: 2 Conversion between data typesWe can convert between different data types by using different type conversion functions like
Conversion from float to int will truncate the value (make it closer to zero).
Conversion to and from string must contain compatible values.
We can even convert one sequence to another.
To convert to dictionary, each element must be a pair:
What is the use of complex data type?An important advantage that complex data types have over user-defined types is that users can access and manipulate the individual components of a complex data type. In contrast, built-in types and user-defined types are self-contained (encapsulated) data types.
How do you represent complex data types in Python?Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point number. Complex numbers are written in the form, x + yj , where x is the real part and y is the imaginary part.
What is complex Python?Python complex() function returns a complex number ( real + imaginary) example (5+2j) when real and imaginary parts are passed, or it also converts a string to a complex number.
|