# WRITING IF/ELSE STRUCTURES IN PYTHON:

In any programming language, learning how to write if/else structures is crucial. These structures allow you to compare conditionals and allows the computer to make decisions based on those conditions. In today’s tutorial, we will learn how to write if/elif/else structures in python, and as always, we will using a coding example to explain this concept. Without further ado, lets get right into the example:

Write a program to find the largest number out of two numbers excepted from user.

This is a relatively easy question to solve, but for the sake of absolute beginners, I will be solving this question. In upcoming posts, I will be solving harder examples.

Solution:

We will start by defining the function. I am calling my function “largest_of_two” for obvious reasons, but keep in mind you can name your function whatever you want.

We will create 2 variables, num1 and num2, that accept integer values:

For our first conditional, we are going to check if they are the same. If they are, lets print the statement “They are the same!” and end the program there. Alternatively, we can use a while loop to keep printing “Enter the second number” until they are no longer the same number, but we will not be doing that in today’s tutorial. Now, we are going to check if num1 is greater than num2. If this is true then print, using f string literals, “f “{num1} is greater!!””. In upcoming posts, I will be explaining f string literals, but in short, they are convenient ways to access the values of our variables and calling them in strings. If num1 is not greater than num2, then print “f “{num2} is greater!!””

When the function is called, you should have something that resembles this:

The goal of this blog is to teach and explain python algorithm questions to both new and experienced python software developers.

Hey there! I’m the newbieprogrammer and my goal is to make sure that my readers understand the various python algorithms/ problems I solve. Feel free to check out my Instagram @newbieprogrammer and make sure to give me a follow on there. You can also check my GitHub for all source code @newbieprogrammer.

## More from newbieprogrammer

I am a Python Web App developer and Machine learning Engineer.

## Building a plugin architecture with Python

Get the Medium app