Follow

Follow

# Basic Programming in Python - Part 2

Shravani Thirunagari
·Jul 5, 2022·

In this article we are going to learn two powerful concepts of python language, 'if' and 'for'. Let us look into more problems to understand these concepts.

### If - the control statement

'If' can be used to compare two values and perform the respective sets of operations that match the condition. Say we want to check for greater numbers, we could use this control statement.

``````if 3>2:
print('3 is greater than 2')
else:
print('3 is not greater than 2')
``````

We have around three keywords - if, elif (which means else if) and else. 'elif' can be used when we have multiple conditional statements in the program. let us understand these better by solving the below programs:

Using following list of items per category, veggies = ["cauliflower", "bottlegourd", "carrot", "drumstick"] fruits = ["apples","mangoes","strawberries"] grocessories = ["soaps", "pulses", "tissue"]

let us write a program that asks the user to enter an item name and it should tell which category the item belongs to. Let us try to write a program that asks the user to enter two items and it tells you if they both are in the same category or not. For example, if I enter apples and mangoes, it will print "Both items are in fruits" but if not it should print "They don't belong to the same category"  I have another python program that can tell you if your BP is normal or not. The normal BP range is 60 to 120. Ask the user to enter his BP level If it is below 60 range then print that BP is low If it is above 120 then print that it is high otherwise print that it is normal ### For - loop

The most popular and powerful controlling features are loops. We could continue to perform a set of operations multiple times by passing them through a loop. However, make sure to have a condition to break through the loop to avoid an infinite loop. Let us see how we achieve the following problems:

Let us write a program to print squares of all numbers between 1 to 10 except even numbers, which means squares of odd numbers between 1 to 10. Now, let us try writing a program that prints a * shape Here, in the above solution, we are using nested for loops. That means one for loop inside another loop. We could achieve the same using different approaches. Try using a while loop to solve the same problem.

That's all for now. Hope this was helpful. Thanks for reading so far. Happy learning.