0% found this document useful (0 votes)
34 views6 pages

Python Selection Statements Explained

The document explains selection statements in programming, focusing on logical tests and conditions such as equality, inequality, and comparison operators. It provides examples of using simple if, else, and elif statements in Python, along with practice questions and solutions for user input scenarios. Additionally, it covers boolean operators and nested if statements for more complex logical structures.

Uploaded by

nothing12334343
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Python Selection Statements Explained

The document explains selection statements in programming, focusing on logical tests and conditions such as equality, inequality, and comparison operators. It provides examples of using simple if, else, and elif statements in Python, along with practice questions and solutions for user input scenarios. Additionally, it covers boolean operators and nested if statements for more complex logical structures.

Uploaded by

nothing12334343
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Selection Statements

Selection statements test conditions and based on your condition/logical test, results may be
generated or it may also cause a different set of instructions to execute.

Different type of logical tests/conditions


 Equal : a == b
 Not Equal : a != b
 Less Than :a<b
 Greater Than :a>b
Syntax of simple IF
If (logical test/condition):
print (“value true”)
Note: Python relies on indentation (whitespace at the beginning of a line) to define scope in
the code. Other programming languages often use curly-brackets for this purpose.

Practice Question:
Take an input from a user as integer number and if that value is equals to 10 then print “You
guessed it right!”

Solution:
user_num=input("Kindly input your guess number to win the prize: ")

if int(user_num)==10:
print("You guessed it right!")

Output:
‘else:’ is the keyword if the first condition is not true or in other words when our condition is
false then the instructions after the ‘else:’ are executed

Practice Question:
Take an input from a user as integer number and if that value is equals to 10 then print “You
guessed it right!”, if the entered number is not equals to 10 then print “You did not win the
prize!”

Solution:
user_num=input("Kindly input your guess number to win the prize: ")

if int(user_num)==10:
print("You guessed it right!")
else:
print("You did not win the prize!")

Output:
‘elif (Condition):’ is pythons way of saying that if previous logical tests/conditions are not
met then try the next set of logical tests/conditions

Practice Question:
Take an input from a user as integer number and if that value is equals to 10 then print “You
guessed it right!”, if the entered number is equals to 9 and if it is equals to 11 then print “You
were close in guessing the number and winning the prize” and if none of the logical
tests/conditions are true then print “You did not win the prize!”

Solution:
user_num=input("Kindly input your guess number to win the prize: ")

if int(user_num)==10:
print("You guessed it right!")
elif int(user_num)==11:
print("You were close in guessing the right number and winning the
prize")
elif int(user_num)==9:
print("You were close in guessing the right number and winning the
prize")
else:
print("You did not win the prize!")

Output:
Boolean Operators

and : Both the logical tests/conditions should be true


or : any one logical test/condition should be true

Practice Question:
 Take input of three numbers and print the largest number
 Take an input of three numbers and print the smallest number

Solution 1:
num1=int(input("Enter your first number: "))
num2=int(input("Enter your second number: "))
num3=int(input("Enter your third number: "))

if num1>num2 and num1>num3:


print(num1,"is the largest number")
elif num2>num1 and num2>num3:
print(num2,"is the largest number")
else:
print(num3,"is the largest number")

Output:

Solution 2:
num1=int(input("Enter your first number: "))
num2=int(input("Enter your second number: "))
num3=int(input("Enter your third number: "))

if num1<num2 and num1<num3:


print(num1,"is the smallest number")
elif num2<num1 and num2<num3:
print(num2,"is the smallest number")
else:
print(num3,"is the smallest number")

Output:
Nested IF
Nested if is a set of instructions in which if is applied within an if instruction set

Practice Question:
 Take input of three numbers and print the largest number
Note: Use nested if technique and not the ‘and’ operator to execute the questions

Solution 1:
num1=int(input("Enter your first number: "))
num2=int(input("Enter your second number: "))
num3=int(input("Enter your third number: "))

if num1>num2:
if num1>num3:
print(num1,"is the laregest number")
if num2>num1:
if num2>num3:
print(num2,"is the largest number")
else:
print(num3,"is the largest number")

Output:
Practice Question:
Take an input of an email address and a login password. If the email and password match the
following details then print “login successful”, otherwise print “Incorrect login details, try
again!”
Details for successful login:
Email: computerscience@[Link]
Password: computer123

Solution:
email=str(input("Please enter the email address: "))
pword=str(input("Please enter your password: "))

if email=="computerscience@[Link]" and pword=="computer123":


print("Login Successful")
else:
print("Incorrect login details, try again!")

Output:

Common questions

Powered by AI

Logical tests in programming can be combined using boolean operators to evaluate multiple variables simultaneously. This enables complex decision-making by setting composite conditions where all components must meet specific criteria ('and') or any component can meet a criterion ('or') for execution branching. Such structures facilitate multi-level decisions, from simple pass/fail scenarios to intricate evaluations of user inputs and data to determine the flow .

The 'elif' statement in Python allows for cleaner, more efficient code by reducing the need for nested if-else structures. It introduces new conditions directly without ending the current conditional block, which would otherwise require a closing and new if statement if replaced solely with 'if' and 'else'. This integration minimizes indentation levels and keeps incremental condition checks in sequence within a single block, enhancing readability and maintenance .

In Python, user input can be captured using the input function, converted to the appropriate data type, and then evaluated with conditional statements. To generate responses based on proximity to a target number, the input value is compared directly to the target using if-elif statements. If it matches, a specific success message can be printed. If it's near the target (e.g., one number higher or lower), a different message can indicate proximity. All other values result in a default failure message executed in the 'else' block .

Indentation in Python is crucial as it defines the scope of control flow structures like if, for, and while loops. Contrasting with languages that use brackets to define these scopes, Python's reliance on indentation makes code readability more inherent but requires consistent alignment of code blocks to avoid runtime errors. This approach reduces syntactic bulk but increases the importance of visually managing code structure .

In scripting, controlling flow conditions determine which sequences of operations are executed based on user input or other dynamic data. This affects user-interactive applications by influencing the output and behavior based on runtime evaluations. Properly structured flow control ensures that applications respond correctly to user actions, provide appropriate feedback, and maintain logical consistency. Errors in control structure can lead to sequences that produce incorrect results or fail to cover necessary logical branches, impacting application reliability .

Nested IF statements involve placing an if statement within another if statement to divide conditions into more granular checks. To determine the largest of three numbers, we start with the first number and compare it with the second in a nested manner. If the first number is greater than the second, we then compare it against the third. If both conditions are true, the first number is the largest. A similar logic is applied to the second and third numbers in subsequent nested if checks .

An authentication process can be executed using conditional statements by comparing user-provided input against stored valid credentials. Using an if statement, the provided email and password are compared to see if they match the preset values. If both values match, it prints a message indicating successful login; otherwise, it prints an error prompting the user to try again. The use of 'and' ensures that both fields must correct for a successful login message to appear .

Selection statements in programming, such as if, else, and elif, test conditions and determine the flow based on whether those conditions evaluate true or false. An 'if' statement executes its code block if the condition is true. An 'else' statement executes if the 'if' condition is false. The 'elif' statement checks an additional condition if the previous conditions were not met. This allows for multiple branching based on different logical conditions. Python uses indentation to define code scopes, contrasting with languages that use curly brackets .

A guessing game can be designed using if-elif-else statements where the user's input is evaluated against a target number. If the user's guess is exact, the program acknowledges the correct guess. If the guess is close to the target, different feedback suggests near success. For instance, comparing the input exactly with the target for success, or checking one unit lower or higher for proximity, provides tiered feedback. All non-matching inputs trigger a generic feedback message indicating failure .

Boolean operators, such as 'and' and 'or', are used to combine multiple logical conditions. The 'and' operator requires all combined conditions to be true for the compound condition to evaluate as true, whereas the 'or' operator needs only one of the conditions to be true. This affects the branching of execution in conditional statements; 'and' ensures a more restrictive check, while 'or' allows multiple paths to be valid .

You might also like