Conditionals in thonny12/2/2023 ![]() You can have one or more nested if blocks inside if statements. ![]() Now, let us add an elif block to it as well and see what happens: > num = 44 You can try the following example to understand better. To implement this you can look at the if. Python language offers some special types of operators like the identity operator and the membership operator. Selection and Python If statements Creating if blocksĪs a programmer, you will continually feel the need to control the flow of your program and let it make runtime decisions based on some condition. > length = len(fruits) # get the length that will be needed for the while condition We can implement the fruit example in the while block as well, although the logic becomes a bit complicated than the for block. Note that similar to the for loop, the while statement ends with a colon : and the remaining code block is indented by 4 spaces. The skeleton of a while block is shown below. The while statement will execute a block of code as long as the condition is true. Taking the example above, you can print the name of the fruit and the index of the list of fruits. However, if you are interested in working with the index, then you can call the enumerate function which returns a tuple of the index and the item. In the previous section, index or the place value of the item in the iterable was not considered. Download my infographic There are four main kinds of conditionals: The Zero Conditional: (if + present simple. print("name of fruit: %s is has length %s" % (fruit, string_size)) They are made using different English verb tenses. In the example above, note that items in the iterable (i.e fruits) will be assigned to the for loop variable (i.e fruit) during the iteration process. If you want to print an element of a list of fruits, you can write the following code to achieve that. for item in iterable: # you can place any list or tuple or string in place of iterable An iterable is any object that can be looped on such as list, tuple, string etc. The skeleton for using the for loop is shown below.Note that the for statement line will end with a colon : and the rest of the code block must be indented with a spacing of 4 spaces. If you want to run an operation on a collection of items, then you can do it using for loops. Other topics like Recursion, Exception Handling, Concurrency will be discussed in later tutorials. This can be done by the use of functions. How to have sequential, selective and iterative flows in your code. Advanced way to check the exact word, that we need to find in a long string: import re text 'This text was of edited by Rock' try this string also text 'This text was officially edited by Rock' for m in re.finditer (r'bofb', text): if m.group (0): print ('Present') else: print ('Absent') Share. You can control the flow of logic in your code through various methods. Python Control Structures - Loops and Conditionals
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |