If statement

if statement in Python

Exploring Conditional Logic with the "if" Statement in Python

Programming is an exciting and creative skill that anyone can learn, regardless of age. The "if" statement in Python is a fundamental concept that allows us to make decisions and control the flow of our programs. In this article, we will introduce the "if" statement in a beginner-friendly manner, explaining its purpose, syntax, and providing simple examples

The "if" statement in Python opens the door to the exciting world of conditional programming. By using this powerful concept, you can teach your programs to make decisions and adapt to different situations. Remember to think of conditions as questions and experiment with different scenarios. With practice and curiosity, you'll become more comfortable with the "if" statement and unleash your creativity in programming. Keep exploring, coding, and enjoying the wonders of Python!

Understanding the Purpose of the "if" Statement

The "if" statement allows us to write code that responds to different conditions. It helps the program make decisions and choose different paths based on those conditions. Think of it as a way to teach your program to think and make choices, just like you do!

Syntax of the "if" Statement

The "if" statement in Python follows a specific structure that is easy to understand. Here's the basic syntax:

if condition:
  # Code block to be executed if the condition is True

The condition is an expression that the program evaluates to either True or False. If the condition is True, the code block beneath the "if" statement is executed. If the condition is False, the code block is skipped.

Example Use Case

Let's imagine a simple scenario where we want to check if a number is positive or negative. Here's how we can write an "if" statement to handle this

number = 7 if number > 0:
  print("The number is positive!")
else:
  print("The number is negative or zero.")

In this example, we assign the value 7 to the variable "number." The "if" statement checks if the number is greater than 0. If it is, the program executes the code block beneath the "if" statement and prints "The number is positive!" Otherwise, if the number is less than or equal to 0, the code block beneath the "else" statement is executed, printing "The number is negative or zero." Tips for Understanding the "if" Statement: Think of conditions like questions: When you write a condition, you're asking the program a question. If the answer is "Yes" (True), it performs a certain action. If the answer is "No" (False), it does something else or skips a step.

Experiment with different conditions

Try changing the conditions in the examples and see how the program reacts. This way, you can discover how small changes can lead to different outcomes.

Visualize the code

Draw a flowchart or use colorful blocks to represent the "if" statement and the code blocks it controls. This visual representation can help you understand how the program flows based on conditions.

Converters

Descriprion

For this project you are going to build three converters. For every single converter you need to create a function, so you can give your user an opportunity to choose what exactly they want to convert giving them three choices. Depending on their answer you will call the particular function related to their answer.

Decomposition

Converters

The first converter you are going to create is meters to centimeters, then pounds to dollars and litres to millilitres. Each converter will contain a title, an input, a formula and an output which will reveal the result of the particular calculation.

If statement

For second part of your program you will add another input and write if statement. You will compare the value of input and the keyword of the converter and call the function if these two words are the same.

Program

Building converters

Meters to centimeters

Your first line will be the title of your converter and you are using builtin function print to display it. print('Meters to centimeters') Now you need your user to enter the number of metres they want to convert and we are using builtin function input. By default the data type of this value is a string. Later, you are going to use this value to calculate the result of conversion and the data type of this value can't be a string. The interpreter will show an error if you will try to multiply '5' by 100, so you need to convert this value using builtin function. To save this value we will create a variable. As an argument of input, remember to add an explicit message to your user, to show what exactly you want them to enter. At the end of this message leave a gap, so when your user will enter the number, the text and this number are not stuck together. number_of_metres = int(input('How many metres do you want to convert? ')) Your next step is to write a formula. We know that one meter is 100 centimeters. We are creating another variable and assign the multiplication of the value of number_of_metres and 100. result = number_of_metres * 100 The last step is to show your user the result of this conversion using print function and f string. print(f'The result of conversion {number_of_metres} metres to centimetres is {result} centimeters')

Define a function

We've created your first converter and you have 4 lines of code. The interpreter will run this code line by line straight away. We don't want to run this code straight away. We want our user to choose just one converter and run the lines of your program related to the converter your user picked. Function is the solution of this problem. We will define a function and these four lines of code you've created are going to be the block of code of this function. Now you will be able to choose when to call this function.
def convert_m():
   print('Meters to centimeters')
   number_of_metres = int(input('How many metres do you want to convert? '))
   result = number_of_metres * 100
   print(f'The result of conversion {number_of_metres} metres to centimetres is {result} centimeters')

Building if statement

To give your user an opportunity to pick which converter they want to use, we will build an if statement. If statement must contain a condition, which will be evaluated as true or false. Building this program, for our condition we will compare the value of the input and choices you gave your users. To save the value of input, to reuse it later, you will create a variable. Make sure that you added an explicit message to the user, asking him or her to pick one of three converters and type it. Whatever they typed will be the value of this variable. answer = input('What do you want to convert length, money or volume? ') Now you can write your if statement.
if answer == 'length':
  convert_m()