In this tutorial, we will continue our discussion on Selection Statements in Python programming. We will cover the concepts of Nested Selection Statements and the use of Logical Operators in making complex decisions in programs.
Nested Selection Statements:
Nested selection statements are used when we need to make multiple decisions based on multiple conditions. In Python, we can nest if-else statements within another if-else statement to handle such situations. The syntax for a nested selection statement is as follows:
if condition1:
if condition2:
# code to be executed if condition1 and condition2 are true
else:
# code to be executed if condition1 is true but condition2 is false
else:
# code to be executed if condition1 is false
Let’s understand this with an example:
x = 10
if x > 0:
if x % 2 == 0:
print("x is a positive even number")
else:
print("x is a positive odd number")
else:
print("x is not a positive number")
In this example, if x
is greater than 0, we check if it is even or odd, and if it is not positive, we print that it is not a positive number. This is an example of a nested selection statement.
Logical Operators:
Logical operators are used to combine multiple conditions in a selection statement. Python supports three logical operators: and, or, and not. These operators are used to check if two or more conditions are true, or to reverse the result of a condition. The logical operators work as follows:
- and: Returns True if both conditions are true
- or: Returns True if at least one of the conditions is true
- not: Returns the opposite of the condition
Let’s see how logical operators can be used in a selection statement:
x = 10
y = 5
if x > 0 and y > 0:
print("Both x and y are positive")
if x > 0 or y > 0:
print("At least one of x or y is positive")
if not(x < 0):
print("x is not negative")
In this example, we use the logical operators to check if both x
and y
are positive, at least one of them is positive, and if x
is not negative.
With the knowledge of nested selection statements and logical operators, you can now make more complex decisions in your Python programs. Practice writing and executing programs using these concepts to reinforce your understanding.
That’s all for this tutorial on Selection Statements in Python. Stay tuned for more tutorials on Python programming!
Sir please make a video on capstone project and data literacy.
And on Employability skills also
Sirr , plz bring importance ip practical for 11th ip , i have done syallbus for my half yearly with your chaneel but troubling with questions and practical
love your videos
👍👍
Hello sir , please make a video on your JEE Journey ! 😍♥
Hi bhaiya! For grade 10 the syllabus has changed from open office to libreoffice ..pls upload the updated syllabus video pls bhaiya
sir from where r u belong and what course did u do after 12th?
Hello sir, pls make a video on data storytelling for AI 843 class 12
You really helped me a lot in class 10 IT and 11 AI.
I want just this chapter's video so that my ai syllabus gets finished.
Hope you will make it soon.
Pls upload IP class also it was very helpful
Sir did computer science students can watch this video for python
bhaiya please make sep video for JEE Preperation please please !!!! Jee aspirant like
bro please teach for class 11 Computer Science from Sumita Arora book 🙏 Code 083
Thanks
Sir plss class 12 CS Python ke liye bhi videos daliya nah sir plsss
Sir 10 me apse padh kr 99 aaye the
Sir plss uski videos bhi upload kr dijiye
SQL wali unit sbse pehle kr dijiyega plss sir❤
Bhaiya ye sab toh mast hai but IIT jane ki kutch tips de do plz
Bhaiya pls make a detailed video on TUPLE Also
Hello sir
I really want to thank you.. You helped me so much in my class 10 .. Thank you sir😊