Python: If/Else, Functions & Logic Keywords Python: If/Else, Functions & Logic Keywords
Welcome to Episode 3! In this episode, we are going to learn how Python makes decisions โ if / elif / else conditions, the logic keywords and, not and in, and how to build our own functions with def. We'll go step by step, as always. Episode 3 mein aapka swagat hai! Is episode mein hum seekhenge ki Python decisions kaise leta hai โ if / elif / else conditions, logic keywords and, not aur in, aur def se apne khud ke functions kaise banate hain. Hamesha ki tarah hum step by step chalenge.
๐ What you'll learn today๐ Aaj aap kya seekhoge
- โMake decisions using if, elif and elseif, elif aur else se decisions lena
- โCompare values with ==, >, < and moreValues ko ==, >, < aur more se compare karna
- โCombine conditions with and and notConditions ko and aur not se combine karna
- โCheck if something is inside another using inin se check karna ki kuch kisi ke andar hai ya nahi
- โWrite your own reusable functions with defdef se apne reusable functions banana
01If / Elif / ElseIf / Elif / Else
Teaching the program to make decisions.Program ko decisions lena sikhana.
An if statement checks a condition. If the condition is true, Python runs the indented code below it. If it is not true, Python can run an else block instead.Ek if statement ek condition check karta hai. Agar condition true hai, toh Python uske neeche wala indented code chalata hai. Agar true nahi hai, toh Python uski jagah else block chala sakta hai.
age = 18
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
Indentation matters! The spaces before print are how Python knows that line belongs inside the if. Always indent with 4 spaces.Indentation zaroori hai! print se pehle ki spaces se hi Python ko pata chalta hai ki wo line if ke andar hai. Hamesha 4 spaces se indent karo.
Comparison operatorsComparison operators
These are the symbols we use to compare two values inside a condition:Ye wo symbols hain jinse hum condition mein do values ko compare karte hain:
is equal tobarabar hai
is not equal tobarabar nahi hai
is greater thanbada hai
is less thanchhota hai
is greater than or equal tobada ya barabar hai
is less than or equal tochhota ya barabar hai
Don't mix them up: a single = stores a value in a variable, but double == compares two values. In conditions we use ==.Inhe mat mila do: single = kisi variable mein value store karta hai, lekin double == do values ko compare karta hai. Conditions mein hum == use karte hain.
Adding elif (else-if)elif (else-if) jodna
When there are more than two possibilities, we use elif to check extra conditions, one by one, from top to bottom.Jab do se zyada possibilities hon, toh hum elif use karte hain extra conditions ko ek-ek karke, upar se neeche check karne ke liye.
marks = 75
if marks >= 80:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Python checks marks >= 80 first (false), then marks >= 60 (true), so it prints Grade B and skips the rest.Python pehle marks >= 80 check karta hai (false), phir marks >= 60 (true), isliye wo Grade B print karta hai aur baaki skip kar deta hai.
02and & notand & not
Joining and flipping conditions.Conditions ko jodna aur palatna.
and โ both must be trueand โ dono true hone chahiye
and joins two conditions. The whole condition is only true when both sides are true.and do conditions ko jodta hai. Poori condition tabhi true hoti hai jab dono taraf true hon.
age = 20
marks = 75
if age >= 18 and marks >= 50:
print("Welcome! You qualify")
Here age >= 18 is true and marks >= 50 is true, so the message prints. If even one side were false, nothing would print.Yahan age >= 18 true hai aur marks >= 50 bhi true hai, isliye message print hota hai. Agar ek taraf bhi false hoti, toh kuch print nahi hota.
not โ reverses a conditionnot โ condition ko ulta karta hai
not flips the result: a true condition becomes false, and a false one becomes true.not result ko palat deta hai: true condition false ban jaati hai, aur false condition true ban jaati hai.
age = 15
if not age >= 18:
print("Sorry, you are not allowed yet")
age >= 18 is false (15 is not 18+). not flips that false into true, so the message runs.age >= 18 false hai (15, 18+ nahi hai). not us false ko true mein palat deta hai, isliye message chalta hai.
03The in keywordin keyword
Checking if something is inside another.Check karna ki kuch kisi ke andar hai ya nahi.
The in keyword checks whether a value exists inside something else โ for example, a letter inside a word.in keyword check karta hai ki koi value kisi aur cheez ke andar hai ya nahi โ jaise kisi word ke andar koi letter.
name = "Condico"
if "C" in name:
print("Yes, C is in the name")
It also works to check inside a list (a group of items written in square brackets).Ye list (square brackets mein likhe items ka group) ke andar check karne ke liye bhi kaam karta hai.
fruits = ["apple", "banana", "mango"]
if "banana" in fruits:
print("We have bananas!")
Don't worry about lists for now โ we'll cover them properly in a later episode. Just know that in can search inside them too.Lists ki abhi tension mat lo โ unhe hum baad ke episode mein achhe se cover karenge. Bas itna jaan lo ki in unke andar bhi search kar sakta hai.
You can also combine not with in to check that something is not present.Aap not ko in ke saath combine karke ye bhi check kar sakte ho ki kuch maujood nahi hai.
name = "Condico"
if "z" not in name:
print("There is no z in the name")
04Functions (def)Functions (def)
Reusable blocks of code with a name.Naam wale reusable code ke blocks.
A function is a named block of code that you can run again and again. We create one with the def keyword, and we run it by writing its name followed by ().Ek function code ka ek naam wala block hota hai jise aap baar-baar chala sakte ho. Hum use def keyword se banate hain, aur uske naam ke baad () likhkar use chalate hain.
def greet():
print("Hello, welcome to Condico!")
greet()
Giving the function some input (parameters)Function ko input dena (parameters)
We can pass values into a function inside the brackets. These are called parameters, and they let the same function behave differently each time.Hum brackets ke andar function mein values bhej sakte hain. Inhe parameters kehte hain, aur ye ek hi function ko har baar alag tarike se kaam karne dete hain.
def greet(name):
print("Hello", name)
greet("Aman")
greet("Riya")
Getting a value back (return)Value wapas lena (return)
A function can return a result so we can store it or use it later, instead of just printing it.Ek function koi result return kar sakta hai taaki hum use store ya baad mein use kar saken, sirf print karne ke bajaye.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
print vs return: print just shows something on screen. return hands the value back to your program so you can keep using it.print vs return: print sirf screen par kuch dikhata hai. return value ko aapke program ko wapas de deta hai taaki aap use aage use kar sako.
05Recap & ChallengeRecap & Challenge
Quick summary, then try it yourself.Chhota summary, phir khud try karo.
โ What we coveredโ Humne kya cover kiya
- โif / elif / else let the program make decisions.if / elif / else program ko decisions lene dete hain.
- โComparison operators like ==, >, < compare values.Comparison operators jaise ==, >, < values ko compare karte hain.
- โand needs both sides true; not flips a condition.and ko dono taraf true chahiye; not condition ko palat deta hai.
- โin checks if a value is inside text or a list.in check karta hai ki value text ya list ke andar hai ya nahi.
- โdef creates reusable functions (with parameters and return).def reusable functions banata hai (parameters aur return ke saath).
๐ฏ Try these yourself๐ฏ Ye khud try karo
Write an if/else that checks if a number is positive or negative.Ek if/else likho jo check kare ki number positive hai ya negative.
Use and to check if an age is between 13 and 19 (a teenager).and ka use karke check karo ki age 13 aur 19 ke beech hai (teenager).
Use in to check whether the letter "a" is in your name.in ka use karke check karo ki letter "a" tumhare naam mein hai ya nahi.
Write a function that takes a name and prints a greeting.Ek function likho jo ek naam le aur greeting print kare.
That's a wrap for Episode 3! Practice these challenges and I'll see you in Episode 4, where we'll go even further. Keep coding!Episode 3 yahin khatam! In challenges ko practice karo aur milte hain Episode 4 mein, jahan hum aur aage badhenge. Coding karte raho!