Data Types
I LOVE DOING THIS :)
Numbers
If you want to verify if an integer belongs to the class int
you can use isinstance
You will need to pass number as the first argument and class name as the second argument, and it will check if the object is an instance of the class
Numeric types
There are three numeric types in Python - int
for integers, float
for decimal numbers, and complex
for complex numbers. General mathematical operations such as addition, subtraction, and multiplication can be done on them
Assign two complex numbers to two variables. say, complex_var
and complex_var1
and print the sum of the two
Strings and thier Operations
We can assign a character ‘a’ to a variable single_quote_character
. Note that the string is a single character and it is “enclosed” by single quotes
Interestingly we can check the equivalence of one to the other using the keyword is
, it returns True
String Methods
Get the index of a substring in a string.
Test if a substring is a member of a larger string. This is done using the keyword in
and writing the test
Join a list of strings using the join method. A list of strings is written by delimiting the sequence with a comma ,
and enclosing the whole group with brackets []
Break a string based on some rule. This takes in the string as the object on which the method split
is passed using the dot operator (.)
The syntax [::-1]
can be used to reverse the elements in a list or other sequence, or we can even use reverse()
method to reverse the elements
You can also use the slice notation to reverse just a portion of a list. For example, to reverse the middle three elements of the list numbers
, you could use the slice notation numbers[1:-1][::-1]
. This would give you the list [1, 5, 4, 3, 5]
Access individual characters in a string. Note the first element has index 0
. You access the first element with the index 0
, second element with the index 1
, and so on
Formatting in String
Just like C language we can use %s
as a formatter which will enable you to insert different values into a string at runtime and thus format the string
An alternative way is to use the format
keyword
Truth value testing of String
A string is considered to be true
, if it's got some value in it or else it is considered to be false
Last updated