In programming, data type is an important concept. Variables can store data of different types like str, int, float….
Look at Table for Python Data type’s:
Int | Str | Float |
Bytes | Tuple | Set |
Range | None | Complex |
List | Dict | Bytearray |
Frozen Set | Bool |
Note: In python3 Long Data Type not there.
Int Data Type:
Binary Form: If we keep 0b or oB before number it will treat as Binary Number.
a = ob1111, b = 1111(not binary)
- Octa Form: If we keep 0O(zero, O) before number it will treat as Octa Number.
a = 0O7777, b = 7777(not octa)
- Hexa Form: If we keep 0x(zero, small x or cap’s X) before number it will treat as Hexa Number.
a = 0xFace, b = Face(not Hexa)
Float Data Type:
Python’s built-in float type has double precision (it’s a C double in CPython, a Java double in Jython). If you need more precision, get NumPy and use its numpy.
x = 1.200
Complex Data Type:
Complex data type formula is: a + bj
Where: a = Real Part, b = Imaginary Part
In Real Part, we can use any type of number’s like
Bin, Decimal, Octa, Hexa
Example: a = 10 +ob111
a.real => Get Real Value = 10.0
a.imag = > Get Imaginary Value = 20j
But Imaginary Part, it will accept only Decimal and Float Number’s only
Example: a = ob111 + 20j (Correct) b = 10j + 0b111 (Wrong)
Bool Data Type:
Bool Data type will consider as either True or False.
True + True = True
True + False = False
False + False = False
String Data Type:
In python we can define string in Quote’s. either its single or Thrible Quote’s
a = “Welcome to Stackorigin”
b = “””Stackorigin introduced Python tutorial”””
String Forward Direction: [begin:end-1:step]
Step: slice step cant be zero
String Backward Direction:[begin:end+1:step]
Step: slice step cant be zero
Concatenation Operator: Both should be string only.
example: “stack” + “origin” = stackorigin
Repetition Operator: One type should be int and another one is string.
example: “stack” * 5 => stackstackstackstackstack
len() to know how many character’s are there in string.
Strip(): remove space between string.
Count() count sub string in the give string.
Replace() replace a string from old to new string.
Split():split is applicable for string object. split() method splits a string into a list.
RSplit(): Rsplit method is used to reverse the split.
Join() join() method takes all items in an iterable and joins them into one string.
Upper(): upper() method converts all lowercase characters in a string into uppercase characters and returns it
Lower(): lower() method converts all uppercase characters in a string into lowercase characters and returns it
SwapCase() Make the lower case letters upper case and the upper case letters lower case
Title(): Make the first letter in each word upper case
Capitalize(): Upper case the first letter in this sentence
Startswith() Check if the string starts with “stackorigin”
Endswith() Check if the string end’s with “python”
find():The find() method finds the first occurrence of the specified value. if not found it will return “-1”.
RFind() The rfind() method finds the last occurrence of the specified value.
Index(): The index() method returns the position at the first occurrence of the specified value.
RIndex(): The rindex()
method finds the last occurrence of the specified value.
isalpha() The isalpha() method returns True if all the characters are alphabet letters (a-z).
isalnum() isalnum()
method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9)
isdigit():The isdigit() method returns True if all the characters are digits, otherwise False.
islower():The islower()
method returns True if all the characters are in lower case, otherwise False.
isupper(): The isupper()
method returns True if all the characters are in upper case, otherwise False.
istitle(): The istitle()
method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.
isspace(): The isspace()
method returns True if all the characters in a string are whitespaces, otherwise False.