0% found this document useful (0 votes)
6 views3 pages

Lab Practice: Logical & Relational Ops

This document outlines a lab practice focused on logical and relational operations in programming. It includes tasks for coding and running comparisons between two numbers, as well as logical operations using boolean values. The document provides examples and prompts for user input to demonstrate the operations.

Uploaded by

Salim Adebola
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)
6 views3 pages

Lab Practice: Logical & Relational Ops

This document outlines a lab practice focused on logical and relational operations in programming. It includes tasks for coding and running comparisons between two numbers, as well as logical operations using boolean values. The document provides examples and prompts for user input to demonstrate the operations.

Uploaded by

Salim Adebola
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

Module 2 Lab 3 Solution

Welcome to our Lab Practice!


This lab is about logical and relational operations3 Are you ready? Let's get started!

You will find some small tasks in sections below3

Relational Operation
In this section@ you are going to code and run relational operations of two numbers3

HintB to print out the comparison@ we can use the print() to print multiple values out3

For example@ print(x, 'operator', y, 'is', x operator y) for an operator3

###TaskB Get some comparisons3

J3 Assign x with K@ y with L3


K3 print out the comparison and result of
L3 x > y
N3 x >= y
P3 x == y
Q3 x <= y
S3 x < y
T3 x !U y

x = 2
y = 3
print(x, '>', y, 'is', x > y)
print(x, '>=', y, 'is', x >= y)
print(x, '==', y, 'is', x == y)
print(x, '<=', y, 'is', x <= y)
print(x, '<', y, 'is', x < y)
print(x, '!=', y, 'is', x != y)
###TaskB More comparisons

J3 Ask the user to enter an integer@ convert and assign it to x]


K3 Ask the user to enter another integer@ convert and assign it to y]
L3 print out the comparison and result of
N3 x > y
P3 x >= y
Q3 x == y
S3 x <= y
T3 x < y
^3 x !U y

x = int(input('Please enter an integer'))


y = int(input('Please enter an integer'))
print(x, '>', y, 'is', x > y)
print(x, '>=', y, 'is', x >= y)
print(x, '==', y, 'is', x == y)
print(x, '<=', y, 'is', x <= y)
print(x, '<', y, 'is', x < y)
print(x, '!=', y, 'is', x != y)

Logical Operation
In this section@ you are going to code and run logical operations of two numbers3

HintB to print out the equation@ we can use the print() to print multiple values out3

For example@ print(x, 'operator', y, 'is', x operator y) for an operator3

###TaskB Get some computation3

J3 Assign x with True@ y with False3


K3 print out the computation and result of
L3 x and x
N3 x and y
P3 y and x
Q3 y and y
S3 x or x
T3 x or y
^3 y or x
J[3 y or y
JJ3 not x
JK3 not y

x = True
y = False
print(x, 'and', x, 'is', x and x)
print(x, 'and', y, 'is', x and y)
print(y, 'and', x, 'is', y and x)
print(y, 'and', y, 'is', y and y)
print(x, 'or', x, 'is', x or x)
print(x, 'or', y, 'is', x or y)
print(y, 'or', x, 'is', y or x)
print(y, 'or', y, 'is', y or y)
print('not', x, 'is', not x)
print('not', y, 'is', not y)

Common questions

Powered by AI

The logical operation 'x or y' yields 'True' when x = True and y = False, since the 'or' operator returns true if at least one of the operands is true.

No, the expression 'y and x' where y is False and x is True cannot evaluate as True. The 'and' operator requires both operands to be True to yield a True result, and since y is False, the entire expression evaluates to False.

It is necessary to convert user input into integers to ensure accurate numerical comparisons. Relational operations require operands of compatible types, and numbers entered as strings would result in errors or unintended results if not converted. Conversion ensures mathematical operations are correctly applied.

The logical operation 'x and y' where x = True and y = False results in 'False'. This is because the 'and' operator returns true only if both operands are true.

In the lab document, user inputs are collected using the input() function where users are prompted to enter integers. These inputs are then converted to integer type and stored in variables x and y. These integer values are subsequently used in relational expressions to compare them directly.

Logical operations evaluate Boolean expressions such as 'True' or 'False', whereas relational operations compare values. For example, in logical operations, 'x and y' where x = True and y = False evaluates to 'False', but in relational operations 'x <= y' where x = 2 and y = 3 evaluates to 'True'.

Relational operations determine the outcome of comparisons between variables or input values, providing a basis for branching in decision-making processes, such as executing certain code blocks only if conditions are met (e.g., x > y). This control-flow capability is vital for handling varying inputs and conditions during execution.

The outcome of applying the '<=' operator between x = 2 and y = 3 is 'True', since 2 is less than 3.

The 'not' operator is used to negate a Boolean value. When 'not' is applied to a variable x = True, it returns 'False' as it inverts the truthiness of the value.

Logical operations can be used to combine multiple conditions within complex conditional structures. For instance, combining conditions with 'and' or 'or' allows for multi-layered condition checks essential for sophisticated decision-making. For example, executing a block only if x is true and either y is false or z equals a specific value.

You might also like