Python for Developers — 100daysoflearning (PART 2)
Day 52 , 53 & 54
In continuation to the course for Python For Developers lets move on with next set of learning topics.
Then Next section is Control flow which will include- Conditionals & comparisons, Logic operators. ‘while’ and ‘for’ loop. So lets get started with
Conditionals and Comparisons:
For any program the execution that happens is mostly based on true or false result from the condition and the condition result is based on the input to the program. Different parts of code can be executed depending upon the result of the specific conditions within the code. Let us look at some basic Comparisons and Conditions.
Standard arithmetic Comparison :
Above are some examples for arithmetic operations :
>(greater than) , < (Less than) , == (Equal to) , != (not equal to) , >=(greater than equal to ) and <=(less than equal to).
If you try to company different type then it will return an error except for int and Float as they can still be compared as they are numbers.
Above you can see ‘==’ operation will result in false as string cant be compare with int but if you try any other comparison then it results in error.
String Comparison :
As you can see that Strings can be compared as well . Equality works if the strings are exactly the same including the capital and small letters. If you compare a character with a string with ‘>’ or ‘<’ symbols then only the first letter of the string is compared to the character.
In and not in: An easy way to check whether something is present in the list or not.
Conditional Statements :
if, elif & else
If you want to run a block of code on the basis of the result form the comparisons then for that we use the conditional keywords in python which are if, elif , and else.
As you can see from the above example that you write condition starting with keyword ‘if’ then a space then condition check followed by colon and press enter , give 4 spaces for indentation and do the login if that condition is true. If the condition is true the its piece of code gets executed and the other statements becomes invalid. If the first ‘if’ statement is false then it check the ‘elif’ and then the next ‘elif’ till it finds the correct result and if no condition results in true then it executes the code for else block.
‘Not’ , ‘And’ & ‘OR’
‘not’ operation is used to get the opposite value for something in terms of boolean. It will return True or False based on the result of the condoition.
As you can see that empty string’s not will return true and if you try to put that in a condition, print gets executed as the name is empty string and not for that will return true. Can be used sometimes though not used so often.
‘or’ : or will return the value of the true part on the condtion. Suppose you want to logically check for two conditions and then perfom some operation if any of the condition is true then you will use ‘or’. You can also set default values for the variables using or.
‘and’ : and will do the opposite of ‘or’ operation meaning that the execution of the code will happen if both the conditions on the left/right of the ‘and’ is true. The and operation will wither return the first value which is false or the last value if both are true. Let us see some examples.
As you can see None is true but its return type is nothing so nothing is returned. Also you may notice how to print variables inside the print statement.
While loop : a loop is something like running a piece of code again and again unless the condition becomes false. It is very useful in parsing lists etc and for lot of other purpose as well. Lets see how the while loop works :
As you can see first the count is initialized to 0 then we have a condition in while loop and with each print statement we are increasing the count value so once the count value become 5 the while condition becomes false and the loop exits. you can also create infinite loops . Let us see another example with odd/even program.
Above you can see that with while we can use if conditions inside it and in order to re execute the loop after if condition you need to write ‘continue’ so that the while lope is executed again until its condition becomes false. If you want to exit the loop after the if condition then instead of continue you just need to write ‘break’.
For loop : A for loop is basically executing code over sequences and that is the place where its majorly used. So you have different sorts of sequence types like Lists, Tuples , Dictionaries where you can use for loop . For loop takes care of incrementing the counter for you unlike the while loop and also you can use break and continue inside a for loop as well. Let is see some examples:
So for we have learnt about :
- arithmetic comparisons (<,>,≤,≥,==,!=)
- not in , in
- conditional statements (if, elif, else)
- and & or
- while and for loop
A lot of practice can be done around the looping and different variety of programs can be executed in order to practice them like even/odd, Fibonacci series, Factorial , Palindrome etc.
So use the knowledge above and try out some of the programs and let me know if you face any issue. Feel free to post the code in the comments section . The more you practice the more comfortable you will become with the language (even I am doing the same).
Happy Learning & Happy Coding