Posts

Showing posts with the label data type

Set Data Type in Python

Image
Concept Set a collection which is unordered, unindex. Set does not allow duplicate data. Code Example Example 1: >>> s = {'A', 'B', 'C', 'D'} >>> type(s) <class 'set'> >>>  s = {'A', 'B', 'C', 'D'} {'B', 'D', 'A', 'C'} >>>  s.add('New') >>> s {'B', 'HH', 'D', 'A', 'C'} >>> s.update(['XX', 'YY']) >>> s {'YY', 'XX', 'B', 'HH', 'D', 'A', 'C'} Quiz Level 1 Quiz Level 2 Request Me  

Tuple Data Type in Python

Image
  Concept Tuple  is a collection which is  immutable,  ordered and unchangeable. Allows duplicate members Code Example Example 1: >>> t = () >>> t () >>> type(t) <class 'tuple'> >>> t = (10, 20.35, 'Hello', [1, 2]) >>> t (10, 20.35, 'Hello', [1, 2]) Example 2: >>> t = (10, 20.35, 'Hello', [1, 2]) >>> len(t) 4 Get Help

Dict Data Type in Python

Image
Concept Dictionary  is a collection which is unordered, changeable and indexed. No duplicate members Code Example >>> d = {} >>> type(d) <class 'dict'> >>> d = {'key': 'value'} >>> d {'key': 'value'} >>> d = {'1': 1, '2': 2} >>> d {'1': 1, '2': 2} >>> l = [10, 20] >>> d.update({'l1': l}) >>> d {'1': 1, '2': 2, 'l1': [10, 20]} # data within dict [Tested on IDLE ] >>> l = [1, 2, 3, 4] >>> d1 = {1: 'A', 2: 'a'} >>> t = (1, 2) >>> s = {1, 2}  >>> d = {'1': 1, '2': l, '3': d1, '4': t, '5': s} >>> d {'1': 1, '2': [1, 2, 3, 4], '3': {1: 'A', 2: 'a'}, '4': (1, 2), '5': {1, 2}}  >>> d['3'][1] 'A'  >>> d = {i: i...

List Data Type in Python

Image
Concept List  is a collection which is mutable, ordered and changeable. Allows duplicate members. List are just like the arrays, declared in other languages. List can contain int, float, str, list, dict, set any data type. Code Example Example 1: # empty list >>> l = [] [] Example 2: >>> l = [10, 20.35, 'Hello'] [10, 20.35, 'Hello'] Example 3: >>> l = [10, 20.35, 'Hello'] >>> len(l) 3 >>> l [1] 20.35 # append >>>  l.append("New") [10, 20.35, 'Hello', 'New'] >>  l [1: 3] [20.35, 'Hello'] >>> l [-3, -1] 20.35, 'Hello'] >>> l [0] = 40 >>> l [40, 20.35, 'Hello', 'New'] #  reverse >>> l.reverse() >>> l ['New', 'Hello', 20.35, 40] #  extend >>> l = ['New', 'Hello', 20.35, 10] >>> l1 = ['a', 'b'] >>> l.extend(l1) >>> l ['N...

Python String Data Type

Image
  Example String is one the datatype for python. String literals in python are surrounded by either single quotation marks, or double quotation marks. 'hello'  is the same as  "hello". Python Support positive as well as Negative Indexing var = "Hello" is same as var = 'Hello' var = "I Love Python" # Multiline String var = ''' I Love Python I Love Django '''' ''' ''' ---> this one is known as docstring in python This is How Python store a string, see the below Image a =  "Hello, World!" print (a[ 0 ]) Output: H a =  "Hello, World!" print (a[1 ]) Output: e a =  "Hello, World!" print (a[- 1 ]) Output: ! a =  "Hello, World!" print (a[ -2 ]) Output: d # Python Slice Concept # Remember it touches Lower bound not Does not touch the upper bound. a =  "Hello, World!" print (a[ 0: 3 ]) Output: Hel a =  "Hello, World!" print (a[ 2: 3 ]) O...