In our previous blog on Python, we discussed some of the basic features of Python language. We discussed features like Mathematical Operations in Python, Python and Exponents, Quotient and Remainder, String Operations, Variables, etc. In this blog, we will continue with more basics that are essential for Python. This blog further aims to cover some essential topics like Comparisons and Boolean, If Statement, Boolean logic, Lists (Operations and Functions). Let’s begin!
Jump to Section
Comparisons and Boolean
The Python console gives the output as True or False by comparing values using double equal to (==) sign. For example:
>>> print(1==2) False >>> print(5==5) True >>> print("Pizza" == "Pizza") True >>> print("Pizza" == "Burger") False
You can even compare items using the not equal (!=) operator.
>>> print(2 != 2) False >>> print("Pizza" != "Burger") True
Using Python, you can check whether one number is greater than the other or not using greater than or less than operators (>,<).
>>> print(2 > 2) False >>> print(6 < 8) True >>> print(9 <= 9.0) True
If Statement
The If statement is used to run codes with certain conditions provided by the user. The statement is carried out if the expression is evaluated as true to the condition and vice-versa. For example:
>>> if 16%2==0: >>> print("16 is a multiple of 2") >>> else: >>> print("Program Ended") 16 is a multiple of 2
We can introduce one if statement inside the other if statement, hence, we can say that if the statement can be nested. For example:
>>> x= 15 >>> if x > 7: >>> print("x is greater than 7") >>> if x <= 90: >>> print("x is less than 90") >>> if x == 8: >>> print("x is equal to 8") >>> print("x is 15") x is greater than 7 x is less than 90 x is 15
Boolean Logic
We can create more complicated if statements by adding Boolean operators like and, or and not.
The and operator takes two arguments and evaluates as true only if both the conditions are true, else, returns false.
>>> print(("a" == "a") and ("b" == "b")) True >>> print(7 == 6 and 5==5) False >>> print(8<4 and 9>7) False
While, or operator takes two arguments and evaluates as true even if one of the conditions is true, it returns false when both the conditions are false.
>>> print(7>=6 or 7==6) True >>> print(9<1 or 8!=8) False >>> print(5==5 or 3==3) True
Unlike others, not operator only takes one argument and returns the reversed statement. The result of not (Value if true) returns False and not (Value if false) returns True. For example:
>>> print(not("a"=="a")) False >>> print(not(3>=4)) True
While Statement
The If statement runs when the condition is evaluated as True and doesn’t run or stops if the condition is evaluated as false. But, the While statement runs more than once. The statement inside While loop runs multiple times until the conditions provided in it are turned to be false. Once the condition becomes false, the next section of the statement runs. For example:
>>> a= 2 >>> while a<8: >>> print(a) >>> a=a+1 >>> print("End") 2 3 4 5 6 7 End
We use the break statement to stop a loop execution of an infinite loop at any point we want. For example:
>>> a=0 >>> while 1==1: >>> print(a) >>> a=a+1 >>> if a>=6: >>> print("Break") >>> break >>> print("End") 0 1 2 3 4 5 Break End
Another statement that we can use within the While loop is continue statement. The continue statement, rather than to stop the loop like break statement, it returns to the top of the loop. For example:
>>> a=0 >>> while True: >>> a= a+2 >>> if a==8: >>> print("Loginworks") >>> continue >>> if a==12: >>> print("Breaking") >>> break >>> print(a) >>> print("End") 2 4 6 Loginworks 10 Breaking End
Lists
Another type of object in Python is Lists. A list may contain data types like strings, integers, and other objects. It is created by using square brackets and commas separating different items. These are mutable; hence they can be altered even after there creation. The indexing of the list is done by 0 being the first index. For example:
>>> a= ["Hello","this", "is", "Loginworks!"] >>> print(a[0]) Hello >>> print(a[1]) this >>> print(a[2]) is >>> print(a[3]) Loginworks!
We can create an Empty List using an empty pair of square brackets.
>>> empty= [] >>> print(empty) []
You can use the nested function with lists too.
>>> a=8 >>> random= [7,3,"Loginworks",[2,4,6,a]] >>> print(random[0]) >>> print(random[1]) >>> print(random[2]) >>> print(random[2][3]) >>> print(random[3][3]) Result- 7 3 Loginworks i 8
Lists Operations
The list has the function to reassign the values in it by selecting its index and assigning it a new value. For example-
>>> x= [1,2,3,4,5,6,8,8] >>> x[6]=7 >>> print(x) [1,2,3,4,5,6,7,8]
You can add and multiply Lists. For example:
>>> x= [1,2,3,4] >>> y= x + [5,6,7,8] >>> print(y) >>> y= x * 2 >>> print(y) [1,2,3,4,5,6,7,8] [1,2,3,4,1,2,3,4]
To check whether an item is present in a list, we use in operator. It returns true if the item is present in the list else, returns false. For example:
>>> a= ["Hello","we","are","Loginworks"] >>> print("we" in a) >>> print("Software" in a) True False
Lists Functions
To add an item at the end of the existing list, you can use the .append function. For example:
>>> a= [2,4,6,8] >>> a.append(10) >>> print(a) [2,4,6,8,10]
To find the number of items in a list, we use the len() function.
>>> a= [2,4,6,8] >>> print(len(a)) 4
An advanced method of .append function is .insert method, and this method allows you to add an item at any index of the existing list.
>>> a= ["I","a","Foodie"] >>> a.insert(1,"am") >>> print(a) ['I','am','a','Foodie']
The index method allows you to to find the index of a given item in a list:
>>> a= ["I","am","a","foodie"] >>> print(a.index("foodie")) >>> print(a.index("am")) 3 1
Conclusion
This blog covers many important topics like Comparisons and Boolean, If Statement, Boolean logic, Lists (its Operations and functions) and sub-topics like while, break, continue, append, len, insert, etc. We also discussed basics like Mathematical Operations in Python, Python and Exponents, Quotient and Remainder, String Operations, variables. For and While loops are one of the most critical topics in Python that we had just begun with, further, we will face harder for and while loops, which could only be completed through deep understanding and practice. Hence, summarizing all this, we get the idea of performing simple Python coding and now will face fewer problems in understanding the more profound concepts of Python language. Please feel free to leave any suggestions or comments in the comment box below.
To know more about our services, please visit Loginworks Softwares Inc.
- Business Intelligence Vs Data Analytics: What’s the Difference? - December 10, 2020
- Effective Ways Data Analytics Helps Improve Business Growth - July 28, 2020
- How the Automotive Industry is Benefitting From Web Scraping - July 23, 2020