Python — 3
Hi guys… 😍
Today too, I’m going to continue the post about Python from where we left off. In the last post, we focused on lists, basic arithmetic operators, string formatting and basic string operations. You can find my previous posts below:
Python — 2 → https://himashikarunathilake.medium.com/python-2-5d3b48c8da68
Python — 1 → https://himashikarunathilake.medium.com/python-1-ca8f34d2ee79
Now, let’s get coding!!!
The Input Statement
Sometimes, you need to get input values from the user. Python normally takes any input as a string. Therefore, if we have to take numerical inputs, we have to convert the inputs to the respective type.
Conditions
***** Please note that the assignment operator is =, the equals operator is == and that the not equals operator is !=. ******
In Python, when a condition is checked, it would return a Boolean value, either true or false.
The if condition in Python follows the following syntax:
The if — else condition in Python follows the following syntax:
The else — if condition follows the following syntax:
Boolean Operators
The and operator will make sure that all conditions have to be true.
The or operator will make sure that one or all conditions are true.
The “in” Operator
This operator can be used to check whether a specified object exists for instance, in a list.
e.g., If you need to find a specific name in a list of names, you can use this operator.
The “is” Operator
This operator should not be confused with == operator. The equals operator checks whether the values are the same. The is operator checks whether the instances are the same. Please refer the example given below:
The “not” Operator
This operator is used to invert the output.
Loops
Loops are mainly used for iteration to prevent repetition of code segments.
The “for” Loop
***** The Python print() statement adds a new line at the end of execution by default. But, if we need to get rid of this feature, we can choose how we need to end the statement. We can simply use an additional parameter as in, print(“My name is Vanessa”, end = “!!!”). This will give an output as My name is Vanessa!!! — no new line is added at the end of the statement, but three exclamation points as defined, is added. *****
In a previous post, we discussed about the for loop. There are many forms that this loop can take. These are explained in the example below:
The “while” Loop
These conditions iterates until a certain condition is met. The syntax is as follows:
The “else” Loop
Unlike in some languages, we can have an else segment in the loop. What happens here is that, if the condition of the loop fails, this else part of the loop is executed. The syntax is as follows:
Special Statements
There are certain statements that can be used with loops:
- break statement → completely exits the current loop; the else segment is not executed
- continue statement → immediately skips the current iteration and proceeds to the next; else segment will be executed
So guys, that’s it for this post. Please try out the tutorial for this lesson and check your answers with the answers given from the folder Python — 3 in https://github.com/Himashi-Karunathilake/Python.
In face of any difficulty please do your own research and discover new ways to answer too.
Till I see you in the next one then, Happy Coding! 😍
Ciao! 👋
Originally published at http://mysnowfrostpersonal.wordpress.com on November 30, 2019.