Matching whitespaces

Lesson Details:
June 29, 2020


I: Introduction – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Python is a high-level interpreted programming language. Python supports multiple programming paradigms. It comes with the basic features such as variables, functions, conditionals, and loops. You can do logical operations on them which produces a new object. There are some built-in data structures like list, dicts, strings, tuple etc.

The interpreter is used to run the python codes. For writing the code in python, there are several IDEs available for use.

II: Body-

A: Matching whitespaces

Python gives you the ability to match whitespaces. It means that you can say that two sequences are same if they have the same length and same whitespace between them. When it comes to compare strings, python uses normal equality testing operators.

When you are comparing strings, python makes sure that both the strings are of the same length. String format operator is used to check whether the two strings have the same length or not.

For example:-

>>> “abc” == “abc” # return True

>>> “abc” == “abc” # return True

>>> “abc” == “abc” # return False

Here, firstly, we are comparing two strings which has length one i.e., abc with the string abc. As both the strings have the identical characters, python returns true. Now, let us suppose that we are comparing “abc” with “abc”. As both the strings have different characters, python returns false. An example of using it would be like this. Suppose you want to check whether a given string is same as another string or not. Here is how you can do it:- >>> str1 = ‘abcd’ >>> str2 = ‘abcd’ >>> if str1 == str2: print(“Yes”) else: print(“No”) Yes Another example would be like this. You want to check whether your name is same as that of other person or not:- >>> if ‘Sherlock Holmes’ == ‘Sherlock Holmes’: print(“You are Sherlock Holmes”) else: print(“You are not Sherlock Holmes”) You are Sherlock Holmes

B: The Boolean Values

The boolean values are one of the data types provided by Python programming language. They are also known as booleans. They are either true or false values. Unlike integers, floats etc., booleans always evaluate to themselves. There are not any infinite number of booleans that can be created unlike integers or floats which can be created infinitely. The methods that can be used for creating booleans are listed below:- A variable without any value assigned to it is considered to be a boolean value false A variable with no value assigned to it is considered to be a boolean value true A variable with a null value assigned to it is considered to be a boolean value false A variable with an empty string assigned to it is considered to be a boolean value false A variable with zero value assigned to it is considered to be a boolean value false A variable with an empty collection assigned to it is considered to be a boolean value false Everything else is treated as a boolean value true Here, we will try to understand this concept in more detail. Let us start with an example:- >>> bool_var = 2 >>> bool_var True >>> bool_var = 0 0 >>> bool_var False >>> bool_var = [0] [0] >>> bool_var [0] >>> bool_var = {} {} >>> bool_var {} >>> bool_var = [] [] >>> bool_var [0] >>> bool_var = None None >>> bool_var False A variable with an empty string assigned to it is considered to be a boolean value false A variable with zero value assigned to it is considered to be a boolean value false A variable with an empty collection assigned to it is considered to be a boolean value false Everything else is treated as a boolean value true In Python programming language, everything is considered as a boolean value except True and False. For example:- >>> True False >>> False True In Python programming language, everything is considered as a boolean value except True and False. For example:- List of booleans:- False Empty strings Zero numbers Empty collections Empty dictionaries None of the previous examples of booleans Above mentioned examples show that a boolean value of a variable determined by whether the variable has a non-empty value or not. It means that a variable can have any of the following values -True, False or None Only when you assign a value to a variable, its boolean value becomes known. If no value is assigned then it takes on either one of two possible values depending on what kind of variable it is:- If the variable is a file object then its boolean value is false If the variable is a class object then its boolean value is true String class objects specify their own __nonzero__() special method that returns either True or False depending on whether the string object has a non-empty string or not. The __len__() method belongs to all objects which means that every object has a length property and hence all objects are iterable too which implies that they can be looped over using for loop which implies that they support indexing and slicing operations too. So just by looking at an object you cannot tell if it is empty or non-empty although there are certain properties it may have which will indicate that it contains information or not but since Python does not support static typing so there is no way of telling which type of object it might be until you perform operations on it like calling methods or accessing certain properties but still there are certain rules governing this so for example if you access certain properties of an object it could mean that you know for sure about what kind of object it might be because only certain types of objects may have these properties like for example when you access length property of an integer type object then you know for sure that it must be an integer type object because only integers may have length property but if you access length property of an object then you don’t know for sure about what type of object it might be because there may be certain objects which may contain length properties like lists do but still they will usually contain other information too because lists in Python don’t store only their lengths in their length properties because they also contain other information like their actual contents which means that lists can have length equal to 1 but still they will have something stored in them so that even though they have length equal to 1, you cannot say for sure that they are empty lists because you cannot say for sure whether they are empty or not because you cannot get any information about their contents from their length property alone so for example here I am setting length equal to 1 but still I am actually storing some other information in my list so here I am adding 3 elements so actually keeping track of actual contents of this list I am keeping track of how many elements it has which means that if I try accessing its length property again then I will get result equal to 3 instead of 1 because usually people usually store something other than just their lengths in their length properties so even if they have length equal to 1 then you cannot say for sure that they are empty lists because there may be some elements stored in them but still they should also work fine when their length equals 1 because according to python documentation lists are iterable but still you cannot say for sure about their contents because whenever you try accessing their contents directly then python will raise IndexError exception which means that you cannot access them directly but only through indexing operations so if you try accessing them directly then you will get IndexError exception saying that ‘list’ object does not support indexing operation and hence we cannot access its contents directly and we can access them only through indexing operation and indexing operation requires us to specify exact position where we want our element and since we don’t know its exact position we cannot access its contents directly and hence we get IndexError exception which tells us that we cannot access its contents directly and we can only access them through indexing operations and indexing operations require us to specify exact position where we want our element and since we don’t know its exact position we cannot access its contents directly and hence we get IndexError exception which tells us that we cannot access its contents directly and we can only access them through indexing operations and indexing operations require us to specify exact position where we want our element and since we don’t know

loader
Course content