Hello Steemit friends!
As part of the basic programming course, I will cover the lesson on arithmetic, comparison and logical operations. These operations are fundamental in almost all programming languages, as they allow you to perform calculations, make decisions and manipulate data.
In this participation, I will explain the importance of these operations and how they are used to solve simple problems in programming. I will also demonstrate, through concrete examples, how to write arithmetic expressions, perform comparisons, and use logical operators to make programs more interactive.
Finally, I will present you with a series of practical codes to illustrate these concepts, following the instructions of the contest.
Give a brief summary of what arithmetic, comparison, and logical operations are used for. |
---|
Arithmetic Operations
Arithmetic operations are fundamental mathematical functions that I use to perform calculations on numerical data. They are essential in programming for tasks ranging from simple computations to complex algorithm implementations.
Common Arithmetic Operators:
- Addition (
+
): I add two numbers. - Subtraction (
-
): I subtract the second number from the first. - Multiplication (
*
): I multiply two numbers. - Division (
/
): I divide the first number by the second. - Modulo (
%
): I return the remainder of the division of the first number by the second. - Exponentiation (
**
in Python,Math.pow()
in Java): I raise one number to the power of another.
Examples in Python:
Result:
Comparison Operations
Comparison operations allow me to compare two values or expressions and evaluate a condition that returns a boolean result (True
or False
). They are essential for making decisions in a program, especially in conditional structures and loops.
Common Comparison Operators:
- Equal to (
==
): I check if two values are equal. - Not equal to (
!=
): I check if two values are different. - Greater than (
>
): I check if the left value is greater than the right value. - Less than (
<
): I check if the left value is less than the right value. - Greater than or equal to (
>=
): I check if the left value is greater than or equal to the right value. - Less than or equal to (
<=
): I check if the left value is less than or equal to the right value.
Examples in Python:
Result
Logical Operations
Logical operations are used to combine multiple conditions or boolean expressions. They allow me to create more complex conditional structures and control the flow of the program precisely.
Common Logical Operators:
- Logical AND (
and
in Python,&&
in Java): ReturnsTrue
if all conditions areTrue
. - Logical OR (
or
in Python,||
in Java): ReturnsTrue
if at least one condition isTrue
. - Logical NOT (
not
in Python,!
in Java): Inverts the boolean value of the condition.
Examples in Python:
Result
Make a program that asks the user for 2 numbers and evaluates whether both numbers are equal |
---|
Program in Python:
Explanation:
Input:
- The program prompts the user to enter two numbers.
input()
function is used to read the user's input as a string.float()
converts the input string to a floating-point number to handle decimal values.
Evaluation:
- The expression
num1 == num2
compares the two numbers for equality. - This expression returns a boolean value:
True
if they are equal,False
otherwise. - The result is stored in the variable
are_equal
.
- The expression
Output:
- The program prints the message
"Are both numbers equal? "
followed by the boolean result (True
orFalse
). - No conditional statements are used; we directly output the result of the comparison.
- The program prints the message
Sample Run:
Further Explanation:
In programming languages like Python, comparison operators such as ==
return a boolean value (True
or False
). This boolean value can be stored in a variable or used directly in expressions.
By storing the result of num1 == num2
in a variable (are_equal
), we can print this value directly without needing to use a conditional statement to check the result and then decide what to print.
Extension: Using Mathematical Operations (Optional)
If you prefer to avoid even the equality operator ==
, you can use mathematical operations to determine equality.
Example in Python:
Explanation:
- We calculate the difference between the two numbers.
- If the difference is zero, the numbers are equal.
- This still doesn't use any conditional structures.
Transform the following mathematical expressions into arithmetic expressions on your computer |
---|
I am going to translate each mathematical expression into Python syntax. Python follows the same operator precedence rules as mathematics (parentheses, multiplication/division, then addition/subtraction).
Expression 1:
In Python, this is directly translated as:
y = (8 - 2) * (5 + 4)
Expression 2:
Here, it is important to note that Python performs floating-point division by default, so the expression is:
x = 8 / 2 + 9 / 9
Expression 3:
In this expression, the division ( 2 / 3 ) must be done first inside the parentheses:
z = (4 + 8) * 3 / (2 / 3)
Evaluate whether the results of the 3 operations are greater than or equal to 0 (>=) and display it on the screen. |
---|
Once the expressions are evaluated, we need to check if the results of the three operations are greater than or equal to zero. This is done using the comparison operator >=
.
Complete Python Code
Here is the complete code with the three mathematical expressions, their evaluation, and checking whether the results are non-negative:
Calculating the Expressions:
- The first expression is evaluated as:
( (6) * (9) = 54 ). - The second expression is evaluated as:
( 4 + 1 = 5 ). - The third expression is evaluated as:
( 12 * 3 / (0.6667) = approx 54 ).
Comparison:
- We use the
>=
operator to check if each result is greater than or equal to zero. x_non_negative = x >= 0
means that if the result ofx
is greater than or equal to zero, the variablex_non_negative
will beTrue
, otherwise it will beFalse
.
Displaying the Results:
print(f"x >= 0 : {x_non_negative}")
prints whether the result ofx
is greater than or equal to zero, and similarly fory
andz
.- Then, we check if all results are greater than or equal to zero using the logical operator
and
. If all variables (x_non_negative
,y_non_negative
, andz_non_negative
) areTrue
, the variableall_non_negative
will also beTrue
.
Expected Output:
Thank you very much for reading, it's time to invite my friends @josepha, @lhorgic, @mohammadfaisal to participate in this contest.
Best Regards,
@kouba01
@tipu curate
;) Holisss...
--
This is a manual curation from the @tipU Curation Project.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Upvoted 👌 (Mana: 0/8) Get profit votes with @tipU :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the invitation You have presented each question with programming examples. And you have solved the programs in a unique way using your creativity and logic. Best of luck for the contest. I have also dropped mine entry recently.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for your kind words and encouragement! I had the chance to read your participation, and it was truly excellent. Your approach to solving the problems was creative and well-executed. Best of luck to both of us in the contest, and I look forward to seeing more of your great work!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Than you for your best wishes. You will see more soon. I am preparing yours task of PHP as well and soon will be published then you will see that as well.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
This post has been upvoted/supported by Team 7 via @httr4life. Our team supports content that adds to the community.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You have done a great job and I have learnt something from your post. Thank you for the invite and good luck to you.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you my friend!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit