Showing posts with label operators. Show all posts
Showing posts with label operators. Show all posts

Monday, 22 January 2024

Understanding Numeric Comparisons in Python: How Integer and Floating-Point Equality Works

When Python compares values of different numeric types (like an integer and a floating-point number), it first converts them to a common type to make the comparison meaningful and accurate. In the case of an integer and a floating-point number, the integer is implicitly converted to a float.

 

For example, let’s compare 10 with 10.0. In this example, the integer 10 is converted to the float 10.0, and since these two numeric values are the same, 10 == 10.0 evaluates to True. This behavior allows for more intuitive and practical numerical comparisons in Python, as it focuses on the numerical equivalence rather than the specific data type of the numbers involved.

>>> 10 == 10.0
True
>>> 
>>> 23 == 23.0
True
>>> 
>>> 23 == 23.0000001
False

Keep in mind that knowing how numbers are compared in Python can help you write better and more trustworthy code.


Previous                                                 Next                                                 Home

Use * as a string replication operator in Python

When the operator '*'' is applied to two integers or floating-point values, it acts as a multiplication operator. But when it is used with one string and one integer, the * operator transforms into a string replication operator.

* operator between integers, float values

>>> 5 * 4
20
>>> 
>>> 5.5 * 4
22.0
>>>

 

* operator between a string and an integer

>>> 'Ram' * 5
'RamRamRamRamRam'
>>> 
>>> 5 * 'Ram'
'RamRamRamRamRam'

The expression ('Ram' * 5) results in a single string that duplicates the original string as many times as the integer value indicates. While string replication is a handy technique, it is not as commonly used as string concatenation.

 

 

 

Previous                                                 Next                                                 Home

Division (/) vs floored division (//) in Python

The primary distinction between division (/) and floor division (//) in Python is their approach in dealing with the fractional component of the outcome.

Division (/)

It is often referred as true division, returns a float value as result, irrespective of the operand types. This is the case even when the division's result is mathematically an integer.

>>> 5/2
2.5
>>> 
>>> 4/2
2.0

 

Floor division (//)

This operator carries out floor division by eliminating the fractional part and rounding the result down to the closest whole number. It is applicable to both integers and floating-point numbers.

>>> 5//2
2
>>> 
>>> 4//2
2

Floor division can also be accomplished using the math.floor(x) function, which is beneficial for maintaining consistency when handling both integers and floats.

>>> import math
>>> 
>>> math.floor(5/2)
2

Floor division is useful in situations where you are interested only in the integer part of the result.



 

 

Previous                                                 Next                                                 Home

Tuesday, 31 May 2022

Java not equal to (!=) operator

Not equal to (!=) is a conditional or relational operator, used to check if one operand is not equal to other operand or not.

 

Syntax

a != b

 

Above snippet return true, if a is not equal to b, else false.

 


NotEqualToOperatorDemo.java

package com.sample.app;

public class NotEqualToOperatorDemo {
	public static void main(String[] args) {
		boolean b1 = (10 != 10);
		boolean b2 = (10 != 11);

		System.out.println("(10 != 10) is evaluated to : " + b1);
		System.out.println("(10 != 11) is evaluated to : " + b2);
	}

}

Output

(10 != 10) is evaluated to : false
(10 != 11) is evaluated to : true

Since != operator return a Boolean value (true or false), you can use this in decision making statements and in loops.

 

!= operator in decision making statements

if (a != 10) {
	System.out.println("a is not equal to 10");
} else {
	System.out.println("a is equal to 10");
}

Find the below working application.

 

NotEqualToOperatorInConditionalStatements.java

package com.sample.app;

public class NotEqualToOperatorInConditionalStatements {

	public static void main(String[] args) {
		int a = 10;

		if (a != 10) {
			System.out.println("a is not equal to 10");
		} else {
			System.out.println("a is equal to 10");
		}

		a = 11;

		if (a != 10) {
			System.out.println("a is not equal to 10");
		} else {
			System.out.println("a is equal to 10");
		}
	}

}

Output

a is equal to 10
a is not equal to 10

!= operator in decision looping constructs

while (a != 0) {
	System.out.printf("a : %d\n", a);
	a--;
}

Find the below working application.

 

NotEqualToOperatorInWhileLoop.java

package com.sample.app;

public class NotEqualToOperatorInWhileLoop {
	
	public static void main(String[] args) {
		int a = 10;
		
		while (a != 0) {
			System.out.printf("a : %d\n", a);
			a--;
		}
	}

}

Output

a : 10
a : 9
a : 8
a : 7
a : 6
a : 5
a : 4
a : 3
a : 2
a : 1


  

Previous                                                 Next                                                 Home

Friday, 14 May 2021

Jq: Comparison operators

‘jq’ support comparison operators to process json data.

 

compOperators.json 

{
	"1": {
		"org": [{
			"name": "ABC Cord",
			"yrsOfExperience": 2.2
		}, {
			"name": "XYZ",
			"yrsOfExperience": 1.8
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"2": {
		"org": [{
			"name": "XYZ",
			"yrsOfExperience": 4.2
		}, {
			"name": "Bokara",
			"yrsOfExperience": 3.8
		}],
		"firstName": "Battu",
		"lastName": "Gopi",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"3": {
		"org": [{
			"name": "Dongle",
			"yrsOfExperience": 2.6
		}, {
			"name": "Binare",
			"yrsOfExperience": 1.11
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Hyderabad",
			"country": "India"
		}
	}

}

Example 1: Get all the employees who are working in Bangalore.

$cat compOperators.json | jq '.[] | select(.address.city=="Bangalore") | .firstName+","+.lastName'
"Krishna,Ram"
"Battu,Gopi"



 

  

Previous                                                    Next                                                    Home

Tuesday, 6 April 2021

Php: Arithmetic operators

Arithmetic operators are used to perform mathematical operations. Following table summarizes the arithmetic operators.

 

Operator

Name

Example

Description

-

Negation

-$a

Opposite of $a.

+

Addition

$a + $b

Sum of $a and $b

-

Subtraction

$a - $b

Difference of $a and $b

*

Multiplication

$a * $b

Multiplication of $a and $b

/

Division

$a / $b

Quotient of $a and $b.

%

Modulo

$a % $b

Remainder of $a divided by $b.

**

Exponentiation

$a ** $b

Result of raising $a to the $b'th power.

 

arithmetic_operators_demo.php

#!/usr/bin/php

<?php
$a = 20;
$b = 11;

$c = - $a;
$d = $a + $b;
$e = $a - $b;
$f = $a * $b;
$g = $a / $b;
$h = $a % $b;
$i = $a ** 2;

echo "Negation of $a is $c\n";
echo "Sum of $a and $b is $d\n";
echo "Difference of $a and $b is $e\n";
echo "Multiplication of $a and $b is $f\n";
echo "Division of $a and $b is $g\n";
echo "Modulo of $a and $b is $h\n";
echo "$a square is $i\n";

?>

 

Output

$./arithmetic_operators_demo.php 

Negation of 20 is -20
Sum of 20 and 11 is 31
Difference of 20 and 11 is 9
Multiplication of 20 and 11 is 220
Division of 20 and 11 is 1.8181818181818
Modulo of 20 and 11 is 9
20 square is 400

Php follows PEDMAS rule while evaluating Arithmetic expressions. PEDMAS is an acronym for Parenthesis, Exponential, Multiplication, Division, Addition and Subtraction.

 

As per PEDMAS rule,

a.   PHP perform the operations inside a parenthesis first

b.   Then exponents

c.    Then multiplication and division, from left to right

d.   Then addition and subtraction, from left to right

 

Example 1: 10 + 2 * 3

Since * has more priority than +, 2 * 3 will be evaluated first.

 

10 + 2 * 3 = 10 + 6 = 16.

 

Example 2: 10 + 2 * (5 - 2) * 6

Since parenthesis has greater priority, expression inside the parenthesis will be evaluated first, then multiplication operations from left to right.

 

10 + 2 * (5 - 2) * 6 = 10 + 2 * 3 * 6

                               = 10 + 6 * 6

                              = 10 + 36

                                = 46

 

pedmas_demo.php

#!/usr/bin/php

<?php
$result1 = 10 + 2 * 3;
$result2 = 10 + 2 * (5 - 2) * 6;

echo "\$result1 : $result1\n";
echo "\$result2 : $result2";

?>

 

Output

$./pedmas_demo.php 

$result1 : 16
$result2 : 46

 

Note

Operands of modulo are converted to integers before processing. For floating-point modulo, use fmod() function.

 

 

Previous                                                    Next                                                    Home