DBMS

Saturday, September 28, 2019

Python Data Types (Basic Operations Part-1)


There are few basic standard data types
a) Number, b) String.

Number

  • The number data type stores numerical values. Whenever any number is assigned to any variable, python creates a Number object.
x  =4, y = 7.  Here, x and y both are number objects.
  • Python supports 4 type of numeric value.
  1.  int (any integer like 10, 20, etc.)
  2. long (integer having larger values like: 2320320424L) 
  3. float (any floating point value which requires more precision that integer or larger than integer). Ex - 10.9, - 5.4, or any large value.
  4. complex (complex numbers like 2 + 3j).
  • If we want to check data type of any variable we can do it with the help of type function.
a = 20
b = 3.5
c = "Hello Python"
print(type(a))
print(type(b))
print(type(c))
Output >>>
<type, 'int'>
<type, 'float'>
<type, 'str'>

String

  • The string data type stores the sequence of characters in the sinlge or double or triple quotation signs. Following are the valid assignment of string to a variable.
a = 'Brij' or
b = "Krishna" or
c = '''Arohi and Nupur'''
  • If two strings are needed to be added this operation is known as concatenation of two strings. Concatenation operation is performed with the + operator. This + operator arrives in between both strings written in single, double or triple quote.
print(a+b+c)
>>>BrijKrishnaArohi and Nupur
d = "Brij "' + "Krishna " + "Arohi and Nupur"
e = 'Brij ' + "Krishna " + '''Arohi and Nupur'''
print (d)
print (e)
>>>Brij Krishna Arohi and Nupur
>>>Brij Krishna Arohi and Nupur
  • If the same string has to be repeated multiple number of times, there is provision of * operator. Depending upon how many times we need to print the same string, we can mupltiply by that number.
a = "Ram " * 5
print(a)
>>> Ram Ram Ram Ram Ram
  • There is a provision of slicing the content of string. Many times we want only limited part of any string which can be obtained by slicing operator [ : ]. It takes two parameters a[x:y] to decide which part of the string is going to be included, x = starting index, y = end + 1 index, where index starts from 0.
a = "Hello Python"
print(a)
>>> Hello Python
print(a[:]
>>>Hello Python
print(a[2:8])
>>>llo Py
  • Index starts from 0 to string length - 1 (+ve number) or the same indexing can be done from last to first in which indexing starts from last charcter treating it as -1 and -2, -3, ...towards first character of string.
a = "Hello Python"
print(a[-12:-1])
>>>Hello Pytho
print([-12:-2])
>>>Hello Pyth
  • This simply means when we start couning from starting so couning starts from 0 and increases towards next characters till the end of string. On the other hand, if counting of character in string starts from end then its starts from -1 and decreases towards start of the string.
Index    :    0   1   2   3   4   5   6   7
String        H   E   L    L  O   S   I    R
or
Index    :   -8  -7  -6  -5  -4  -3  -2  -1
String        H   E    L   L   O   S    I   R
  • The concatenation  operation + and repetition operation * are applicable not only for string but also for the list elements, tuple and dictionoary elements too.
list1 = [1, 3, 5, 7]
list2 = [2, 4, 5, 8]
print(list1+list2)
>>>[1, 3, 5, 7, 2, 4, 5, 8]
print(list1*2)

>>>[1, 3, 5, 7, 1, 3, 5, 7]