Basic programming course: Lesson #3 Operations

in devjr-s20w3 •  2 months ago 

algo w3.png

Edited By Canva

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:

image.png

Result:

image.png

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:

image.png

Result

image.png

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): Returns True if all conditions are True.
  • Logical OR (or in Python, || in Java): Returns True if at least one condition is True.
  • Logical NOT (not in Python, ! in Java): Inverts the boolean value of the condition.

Examples in Python:

image.png

Result

image.png

Make a program that asks the user for 2 numbers and evaluates whether both numbers are equal

Program in Python:

image.png

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.
  • Output:

    • The program prints the message "Are both numbers equal? " followed by the boolean result (True or False).
    • No conditional statements are used; we directly output the result of the comparison.

Sample Run:

image.png

image.png

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:

image.png

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:

image.png

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 of x is greater than or equal to zero, the variable x_non_negative will be True, otherwise it will be False.

Displaying the Results:

  • print(f"x >= 0 : {x_non_negative}") prints whether the result of x is greater than or equal to zero, and similarly for y and z.
  • 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, and z_non_negative) are True, the variable all_non_negative will also be True.

Expected Output:

image.png


Thank you very much for reading, it's time to invite my friends @josepha, @lhorgic, @mohammadfaisal to participate in this contest.

Best Regards,
@kouba01

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@tipu curate

;) Holisss...

--
This is a manual curation from the @tipU Curation Project.

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.

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!

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.

This post has been upvoted/supported by Team 7 via @httr4life. Our team supports content that adds to the community.

image.png

Loading...

You have done a great job and I have learnt something from your post. Thank you for the invite and good luck to you.

Thank you my friend!