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.
- Python supports 4 type of numeric value.
- int (any integer like 10, 20, etc.)
- long (integer having larger values like: 2320320424L)
- float (any floating point value which requires more precision that integer or larger than integer). Ex - 10.9, - 5.4, or any large value.
- 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.
b = 3.5
c = "Hello Python"
print(type(a))
print(type(b))
print(type(c))
Output >>>
<type, 'int'>
<type, 'float'>
<type, 'str'>
<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.
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.
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.
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.
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.
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.
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]