Python Data Types
Python Data Types |
Variables can hold values of different data types(facts sorts). Python is a dynamically typed language hence we need now not define the data type of the variable whilst maintaining it. The interpreter implicitly binds the data with its type.
Python enables us to test the kind of variable used within the program. Python presents us with the type() function which returns the sort of the variable exceeded.
take into account the following example to outline the values of different data types and checking their type.
Output:
<type 'int'> <type 'str'> <type 'float'>
Standard Data Types
A variable can keep extraordinary kinds of values. for instance, a person's name ought to be stored as a string while its identity ought to be stored as an integer.Python presents numerous popular data types that define the garage approach on each of them. The information types defined in Python are given below.
- Numbers
- String
- List
- Tuple
- Dictionary
on this segment of the academic, we can supply a brief introduction of the above data types. we will discuss each considered one of them in element later in this academic.
Numbers
a wide variety of shops numeric values. Python creates range items when a variety of is assigned to a variable. as an example;
Python supports 4 types of numeric data.
- int (signed integers like 10, 2, 29, etc.)
- long (long integers used for a higher range of values like 908090800L, -0x1929292L, etc.)
- float (float is used to store floating point numbers like 1.9, 9.902, 15.2, etc.)
- complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.)
Python lets in us to apply a decrease-case L for use with long integers. but, we should usually use an top-case L to avoid confusion.
A complex number includes an ordered pair, i.e., x + y where x and y denote the real and imaginary elements respectively).
String
The string can be described because of the collection of characters represented inside the quotation marks. In python, we will use single, double, or triple quotes to define a string.String coping within python is an honest venture on the grounds that there are various inbuilt capabilities and operators supplied.
in the case of string dealing with, the operator + is used to concatenate two strings as the operation "hi there"+" python" returns "hi there python".
The operator * is known as a repetition operator as the operation "Python " *2 returns "Python Python ".
the subsequent instance illustrates the string coping within python.
Output:
he o hello studenthello student hello student how are you
Lists
Lists are much like arrays in C. however; the list can incorporate statistics of different kinds. The gadgets saved in the listing are separated with a comma (,) and enclosed within rectangular brackets [].we can use slice [,:] operators, to get right of entry to the facts of the list. The concatenation operator (+) and repetition operator (*) works with the list inside the same way as they had been working with the strings.
take into account the following instance.
Output:
[2] [1, 'hi'] [1, 'hi', 'python', 2] [1, 'hi', 'python', 2, 1, 'hi', 'python', 2] [1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
Tuple
A tuple is much like the listing in many ways. Like lists, tuples additionally contain the collection of the gadgets of various facts types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().A tuple is a read-handiest records shape as we cannot adjust the size and value of the items of a tuple.
let's see an easy instance of the tuple.
Output:
('python', 2) ('hi',) ('hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2) ('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2) <type 'tuple'> Traceback (most recent call last): File "main.py", line 8, in <module> t[2] = "hi"; TypeError: 'tuple' object does not support item assignment
Dictionary
Dictionary is an ordered set of a key-price pair of gadgets. it's far like an associative array or a hash table wherein each key stores a specific price. Key can keep any primitive data type whereas value is an arbitrary Python object.The gadgets inside the dictionary are separated with the comma and enclosed inside the curly braces {}.
don't forget the following instance.
Output:
1st name is John 2nd name is Stive {1: 'John', 2: 'Sohan', 3: 'Jecob', 4: 'Stive'} [1, 2, 3, 4] ['John', 'Sohan', 'Jecob', 'Stive']
Python Data Types
Reviewed by Sk Jha
on
January 06, 2019
Rating:
No comments: