Python Data Types-: Python has several built-in data types such as integers, floats, complex numbers, strings, lists, tuples, dictionaries, and file objects. These data types are manipulated using language operators, built-in functions, library functions, or a data type’s own methods.
Built In Data Type-: Python has several built-in data types, from scalars such as numbers and Booleans to more complex structures such as lists, dictionaries, and files. The List of data types..
- Numbers
- Lists
- Tuples
- String
- Dictionaries
- Sets
- File Objects
Numbers-: Python language provide four number types are integers, floats, complex numbers, and Booleans.
- Int number-: 1,2, -1 etc.
- Float Number-: 3e, 4.0 etc.
- Complex Number-: 3+2j etc.
- Boolean -: True false .
We can manipulate them by using the arithmetic operators: + (addition), – (subtraction), * (multiplication), / (division), ** (exponentiation), and % (modulus).
as like this integer example…
>>> x = 5 + 2 - 3 * 2 >>> x 1 >>> 5 / 2 2.5 1 >>> 5 // 2 2 2 >>> 5 % 2 1 >>> 2 ** 8 256 >>> 1000000001 ** 3 1000000003000000003000000001 3
Example of float value
>>> x = 4.3 ** 2.4 >>> x 33.13784737771648 >>> 3.5e30 * 2.77e45 9.695e+75 >>> 1000000001.0 ** 3 1.000000003e+27
Example of complex number
>>> (3+2j) ** (2+3j) (0.6817665190890336-2.1207457766159625j) >>> x = (3+2j) * (4+9j) >>> x 1 (-6+35j) >>> x.real -6.0 >>> x.imag 35.0
Boolean example
>>> x = False >>> x False >>> not x True >>> y = True * 2 1 >>> y 2
Lists-: Python has a powerful built in list types. Lists are similar to arrays in C. Although the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
[] [1] [1, 2, 3, 4, 5, 6, 7, 8, 12] [1, "two", 3, 4.0, ["a", "b"], (5,6)]
Example of List
l = [1, "netnic", "python", 2] print (l[3:]); print (l[0:2]); print (l); print (l + l); print (l * 3);
Output of this programme
[2] [1, 'netnic'] [1, 'netnic', 'python', 2] [1, 'netnic', 'python', 2, 1, 'netnic', 'python', 2] [1, 'netnic', 'python', 2, 1, 'netnic', 'python', 2, 1, 'netnic', 'python', 2]
Tuples-: Tuples are similar to lists but are immutable that is they can’t be modified after they have been created. The operators (in, +, and *) and built-in functions (Len, max, and min) operate on them the same way as they do on lists because none of them modifies the original. Index and slice notation work the same way for obtaining elements or slices but can not be used to add, remove, or replace elements. There are only two tuple methods are count and index. An important purpose of tuples is for use as keys for dictionaries. They are also more efficient to use when we do not need modifiability.
() (1,) 1 (1, 2, 3, 4, 5, 6, 7, 8, 12) (1, "two", 3L, 4.0, ["a", "b"], (5, 6)) 2
A one-element tuple 1 needs a comma. A tuple like a list can contain a mixture of other types as its elements including strings, tuples, lists, dictionaries, functions, file objects, and any type of number 2.
String -: String processing is one of Python’s strengths. There are many options for delimiting strings as like
"A string in double quotes can contain 'single quote' characters." 'A string in single quotes can contain "double quote" characters.' '''\tA string which starts with a tab; ends with a newline character.\n''' """This is a triple double quoted string, the only kind that can contain real newlines."""
Dictionaries-: Python’s built-in dictionary data type provides associative array functionality implemented by using hash tables. The built-in Len function returns the number of key-value pairs in a dictionary. The Del statement can be used to delete a key-value pair. As is the case for lists, several dictionary methods (clear, copy, get, items, keys, update, and values) are available.
>>> x = {1: "one", 2: "two"} >>> x["first"] = "one" 1 >>> x[("Delorme", "Ryan", 1995)] = (1, 2, 3) 2 >>> list(x.keys()) ['first', 2, 1, ('Delorme', 'Ryan', 1995)] >>> x[1] 'one' >>> x.get(1, "not available") 'one' >>> x.get(4, "not available") 3 'not available'
1 Sets the value of a new key, “first”, to “one”
Keys must be of an immutable type 2, including numbers, strings, and tuples. Values can be any kind of object, including mutable types such as lists and dictionaries. If we try to access the value of a key that isn’t in the dictionary, a Key Error exception is raised. To avoid this error, the dictionary method get 3 optionally returns a user-definable value when a key isn’t in a dictionary.
Sets-: A set in Python is an unordered collection of objects, used in situations where membership and uniqueness in the set are the main things we need to know about that object. Sets behave as collections of dictionary keys without any associated values as like
>>> x = set([1, 2, 3, 1, 3, 5]) 1 >>> x {1, 2, 3, 5} 2 >>> 1 in x 3 True >>> 4 in x 3 False >>>
We can create a set by using set on a sequence, like a list 1. When a sequence is made into a set, duplicates are removed 2. The in keyword 3 is used to check for membership of an object in a set.
File objects-: A file is accessed through a Python file object as like
>>> f = open("myfile", "w") 1 >>> f.write("First line with necessary newline character\n") 44 >>> f.write("Second line to write to the file\n") 33 >>> f.close() >>> f = open("myfile", "r") 2 >>> line1 = f.readline() >>> line2 = f.readline() >>> f.close() >>> print(line1, line2) First line with necessary newline character Second line to write to the file >>> import os 3 >>> print(os.getcwd()) c:\My Documents\test >>> os.chdir(os.path.join("c:\\", "My Documents", "images")) 4 >>> filename = os.path.join("c:\\", "My Documents", "test", "myfile") 5 >>> print(filename) c:\My Documents\test\myfile >>> f = open(filename, "r") >>> print(f.readline()) First line with necessary newline character >>> f.close()
The open statement 1 creates a file object. Here, the file myfile in the current working directory is being opened in write (“w”) mode. After writing two lines to it and closing it 2, we open the same file again, this time in read (“r”) mode. The os module 3 provides several functions for moving around the filesystem and working with the pathnames of files and directories. Here we move to another directory 4. But by referring to the file by an absolute pathname 5, we are still able to access it.