Skip to main content

Conditional statements in Python if, if-else, if-elif-else पायथन में सशर्त कथन

 In Python, conditional statements help you make decisions in the code. There are three main types of conditionals:-

पायथन में, सशर्त कथन आपको कोड में निर्णय लेने में मदद करते हैं। सशर्त के तीन मुख्य प्रकार हैं:- 

1.) Conditional `if` statement:-

An `if` statement executes the code block only if the condition evaluates to `True`.

1.) सशर्त `if` कथन:- एक `if` स्टेटमेंट कोड ब्लॉक को केवल तभी निष्पादित करता है जब स्थिति सही पर मूल्यांकन करती है।

Syntax:-

if condition:

   Statement(s)

Example:-

x = 10

if x > 5:

    print("x is greater than 5")


2.) Alternative `if-else` statement: 

An `if-else` statement checks a condition and executes a block of code('if' block), if the condition is true otherwise provides an alternative block of code ('else' block), if the condition is false.

2.) वैकल्पिक `if- else` कथन:- एक `if-else` स्टेटमेंट एक शर्त की जाँच करता है और कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है, यदि स्थिति सही है अन्यथा कोड का एक वैकल्पिक ब्लॉक ('else' ब्लॉक) प्रदान करता है, यदि स्थिति गलत है।

Syntax:-

if condition:

   Statement(s)

else:

   Statement(s)

Example:-

x = 3

if x > 5:

    print("x is greater than 5")

else:

    print("x is not greater than 5")


3.) Chained `if-elif-else` statement:- It allows multiple conditions to be checked one after another. It means we can chain multiple conditions together using `if`, `elif` (short for "else if"), and `else`. Here, It executes a block of code('if' block), if the condition is true otherwise it checks 'elif' and executes a block of code('elif' block), if the condition is true and in the end it also provides an alternative block of code ('else' block), Now It executes a block of code ('else' block),  if both of the above conditions are false.

3.) जंजीर `if-elif-else` कथन:- यह कई स्थितियों को एक के बाद एक जांचने की अनुमति देता है। इसका मतलब है कि हम `if`, `elif` ("else if" का संक्षिप्त रूप), और `else` का उपयोग करके कई स्थितियों को एक साथ जोड़ सकते हैं। यहां, यदि स्थिति सत्य है तो यह कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है अन्यथा यह 'elif' की जांच करता है और यदि स्थिति सत्य है तो कोड के एक ब्लॉक ('elif' ब्लॉक) को निष्पादित करता है और अंत में यह एक वैकल्पिक ब्लॉक ('else' ब्लॉक) प्रदान करता है। अब यदि उपरोक्त दोनों स्थितियाँ गलत हैं, तो कोड का  एक ब्लॉक (`else` ब्लॉक) निष्पादित किया जाता है। 

This is useful when you have two or more than two possible conditions.

यह तब उपयोगी होता है जब आपके पास दो या दो से अधिक संभावित स्थितियाँ हों। 

Syntax:-

if condition:

   Statement(s)

elif condition:

   Statement(s)

else:

   Statement(s)

Example:-

x = 10

if x < 5:

    print("x is less than 5")

elif x == 5:

    print("x is equal to 5")

else:

    print("x is greater than 5")

 

Comments

Popular posts from this blog

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...