✿~Conditions and Conditional Statements~✿

✿~Conditions and Conditional Statements~✿

·

3 min read

Veryyyy interesting and super fun topic coming upppp!

Certainly one of my favourite things to do in coding in general tbh as of now (but again, just a beginner so gotta see all of the fun things that there are to explore)

But lets start quickly todayyyy~!


Conditions

Conditions are basically statements that choose between True and False.

Easy enough, an even easier example is :-

print(15>20)

False

There,

EZ

We can use this to compare two values as well, as :-

a=20
b=30

print(a>b)
print(a<=b)

False

True

Still ez,

the symbols that I used to compare, there are a few of them, which I will show with the use of an image (cause lazy :p)

VERY ez

BUT a thing to remember is that while checking equals, it's crucial to use the equals symbol twice ==, using it once will just set a new value to a variable lmao (yeah its kinda funny)

OKAY Moving onnnnnn!


Conditional statements

Conditional Statements basically allow us to get different results based on different conditions that we provide our program with.

-> "if...else" Condition

We are gonna understand this with an example :-

I can make a simple program that tells me if it's too hot outside, cause obviously, making a code and inserting the temperature value, and then finding out if its hot outside is easier than checking it on the phone or going outside to see for ourselves.......yeah........ Anyways, I'll consider 35-degree celsius as "hot"

For thattttt :-

def temp(c):
  if c > 35:
    return "Too hot! Apply sunscreen! :D"
  else:
    return "Its chill~ Picnic time!"

Now we can check as

temp(26)

Its chill~ Picnic time!

temp(39)

Too hot! Apply sunscreen! :D

-> "if...elif...else" conditions

This condition is used to check if multiple conditions can be true. We will get straight to an example to understand this since it's absolutely similar to what we just did above.

def age(num):
  if num < 18:
     print("You are a minor")
  elif num >= 18 and num < 30:
     print("You are a young adult")
  elif num >= 30 and num < 60:
     print("You are an adult")
  else:
     print("You are a senior citizen")

Here are some results:

age(34)

You are an adult

age(67)

You are a senior citizen

age(12)

You are a minor


ANDDDD That should be it for today's topic! I'll make sure to practice more questions based on this cause this is quite important~!

So Thank You to everyone who read it till here! And I'm trying my best to keep these articles coming as soon as I study about them. Appreciate everyone who's trying to get better every day, even if it's a small climb today, but hey, at least we did it, which is so good, so let's just keep going step-by-step until it all becomes easier and easier, like a good habit. Cause always remember,

Consistency is the Key 🗝


Peace~🕊️

Yash K.