logo

Opérateur Python ET

Introduction:

Dans cet article, nous discutons de l'opérateur AND en Python. Il existe deux opérateurs ET en Python : « ET logique » et « ET au niveau du bit ». Cet article traite des deux opérateurs et les différencie.

ET logique :

Ce « ET » appartient aux opérateurs logiques. Ces opérateurs s'occupent principalement besoins de prise de décision avec des instructions conditionnelles comme if-else. Il existe trois opérateurs logiques en Python :

  1. ET logique
  2. OU logique
  3. XOR logique

Maintenant, comprenons mieux l'opérateur ET logique :

Basique:

En C et Java, « && » représente l'opérateur AND, mais en Python, nous disons « et » sans utiliser de symbole spécial. On écrit « et » entre deux opérandes en Python.

Syntaxe:

La syntaxe du ET logique en Python est donnée ci-dessous -

 a and b 

Valeur de retour :

La valeur de retour du ET logique en Python est donnée ci-dessous -

Cet opérateur renvoie « Vrai » ou « Faux », selon les opérandes.

Opération:

  1. Si tous les opérandes/expressions participants sont vrais, AND renvoie True.
  2. Si au moins un des opérandes est faux, il renvoie False.
  3. Si tous les opérandes/expressions participants sont faux ET renvoie faux.
  4. Si au moins un des opérandes est vrai, il renvoie False.

Conte de vérité pour l'opérateur « ET » :

Nous donnons maintenant ici la table de vérité de l'opérateur « ET ». Le tableau est donné ci-dessous -

Expression 1 Expression 2 (Expression 1 et Expression 2) renvoie :
Vrai Vrai Vrai
Vrai FAUX FAUX
FAUX Vrai FAUX
FAUX FAUX FAUX

Besoin de l'opérateur AND :

Supposons que vous essayiez d'écrire un code pour trouver si un nombre est supérieur à 20 et est pair . Vous devez vérifier deux conditions :

  1. Si nombre > 20
  2. Si num est pair

Les deux conditions doivent être remplies.

Code du programme :

Dans le code ci-dessous, nous vérifions que le nombre donné est supérieur à 20 et est également un nombre pair. Le code est donné ci-dessous -

 Enter a number: 34 34 is greater than 20 and is even. 

Dans le code ci-dessus, nous résolvons le problème donné en 4 lignes. Mais ici, nous utilisons l’opérateur AND, et le code se fait en seulement trois lignes. Le code est donné ci-dessous -

 num = int (input ('Enter a number: ')) if (num > 20 and num % 2 == 0): print (num, ' is greater than 20 and is even') 
Opérateur Python ET

Cela a pris 4 lignes de code. Utilisons maintenant la fonctionnalité de l'opérateur AND :

 num = int (input ('Enter a number: ')) if (num > 20 and num % 2 == 0): print (num, ' is greater than 20 and is even') 

Sortir:

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 Enter a number: 34 34 is greater than 20 and is even 

Explication:

Dans la ligne if (num > 20 et num % 2 == 0), la condition de gauche num > 20 est d'abord vérifiée. Pour num = 34, c'est vrai ; donc la bonne condition est vérifiée : 34 est divisible par 2. Les deux conditions sont satisfaites. L'instruction print est exécutée. Si nous avons 31, même si c'est 20, comme il n'est pas divisible par 2, il ne sera pas imprimé. Les deux conditions sont ici remplies. Ainsi, le résultat de cet opérateur AND est vrai.

Prenons un autre exemple avec une application temps réel :

Supposons que vous organisiez un concours de codage pour les étudiants en b-tech et m-tech ; pour qu'un étudiant puisse participer, il doit être âgé de 18 à 28 ans. Toute personne plus jeune ou plus âgée n'est strictement pas autorisée à participer au concours. Nous devons donc vérifier les deux conditions pour inscrire l’étudiant.

Code du programme :

Ici, on vérifie d'abord que l'âge donné est supérieur ou égal à 18 ans et inférieur ou égal à 28 ans. Si cette condition est remplie, alors l'étudiant est éligible. Le code est donné ci-dessous -

 name = input (&apos;Please enter the participant&apos;s name: &apos;) age = int (input (&apos;Please enter the valid age of the participant: &apos;)) if (age &gt;= 18 and age <= 18 28 28): print ('congratulations!, you are eligible for the competition') else: print('only students between to allowed competition. sorry! we can't enroll in') < pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Please enter the participant&apos;s name: Priyanka Please enter the valid age of the participant: 23 Congratulations!, You are eligible for the competition </pre> <h2>&apos;and&apos; with just two numbers:</h2> <p>Let us now see what happens if we give two decimal integers on both sides of the &apos;and&apos; operator:</p> <p> <strong>Pre-requisite:</strong> Generally, &apos;and&apos; checks if both the operands are True. Any number greater than 0 represents True, and 0 represents False.</p> <pre> num1 = int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (num1 and num2) </pre> <p> <strong>Sample Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 3 Enter another number: 4 4 Enter a number: 0 Enter another number: 3 0 Enter a number: 0 Enter another number: 0 0 </pre> <p> <strong>Explanation:</strong> </p> <p> <strong>1. In the first case:</strong> </p> <ol class="points"> <li>Both the numbers are greater than 0 - True and True condition. Hence, &apos;and&apos; returns True.</li> <li>The condition of &apos;and&apos; is that both the operands should be true. Hence, after confirming that the first number- 3 (&gt;0) is true, it checks the second number-4 (&gt;0), which is true. So, it returns &apos;True&apos; with the second number.</li> </ol> <p> <strong>2. In the second case:</strong> </p> <ol class="points"> <li>False and True condition</li> <li>The first number is 0-False.</li> <li>There should be no &apos;False&apos; for &apos;and&apos; to be True. Hence, once it confirms that the first number is &apos;False&apos;, without checking the second number, it returns &apos;False&apos;.</li> </ol> <p> <strong>3. In the third case:</strong> </p> <ol class="points"> <li>The first number is 0-False.</li> <li>Immediately, &apos;and&apos; returns False.</li> <li>False and False condition</li> </ol> <p> <strong>Let us see another example:</strong> </p> <pre> num = int (input (&apos;Enter a number: &apos;)) if (num &gt; 0 and num 10 and num <50): 0 10 print ('num is between and 50') else: either less than or greater < pre> <p> <strong>Sample Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 67 num is either less than 0 or greater than 50 Enter a number: 3 num is between 0 and 10 Enter a number: 34 num is between 10 and 50 </pre> <p> <strong>Explanation:</strong> </p> <p>We checked if the num belongs in the intervals 0 to 10 or 10 to 50. If the number does not belong to both intervals, the number can either be less than 0 or greater than 50.</p> <p> <strong>Program code:</strong> </p> <p>Here we give an example using the &apos;and&apos; operator. We multiplied three numbers using the &apos;and&apos; operator and displayed the result. The code is given below -</p> <pre> num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (&apos;The result is: &apos;, num1 and num2 and num3) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 The result is: 0 </pre> <p> <strong>Explanation:</strong> </p> <p>Firstly, we enter the value of num1, which is a positive non-zero value. Then it is checked, and it returns true. After that, we enter the value of num2, it is a positive non-zero value. It is checked, and it returns true. Lastly, we enter the value of num3, and it is a zero. Then it is checked, and num3 returns false.</p> <p> <strong>Program code:</strong> </p> <p>Here we give an example using the &apos;and&apos; operator. Using the &apos;and&apos; operator, we checked multiple numbers greater or less than the given number. If all the condition is true, then it returns true. Otherwise, it returns false. Then display the result. The code is given below -</p> <pre> num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (num1 &gt; 30 and num2 &gt; 23 and num3 <9) < pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 False </pre> <p> <strong>Explanation:</strong> </p> <p>Firstly, we enter the value of num1, and it is 4. Then we checked the condition &apos;num1&gt;30,&apos; which returned false. After that, we enter the value of num2, which is 5. Then we checked the condition &apos;num2&gt;23,&apos; and num2 returned false. Lastly, we enter the value of num3, and it is 0. Then we checked the condition &apos;num3<9' and num3 returns true. num1 num2 return false, so, the output of this code is false.< p> <h2>Bitwise AND (&amp;):</h2> <p>There is another &apos;and&apos; operator in Python. It is a bitwise operator. We represent it as &apos;&amp;&apos;.</p> <p>Binary language is the language of a computer. All the inner mechanisms happen with respect to bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. There are six bitwise operators:</p> <ol class="points"> <li>Bitwise AND</li> <li>Bitwise OR</li> <li>Bitwise NOT</li> <li>Bitwise XOR</li> <li>Bitwise right shift</li> <li>Bitwise left shift</li> </ol> <p>The difference between (logical or, bitwise or), (logical and, bitwise and), (logical not, bitwise not) lies in the word &apos;bitwise&apos; itself.</p> <ul> <li>If we use any bitwise operator, first, the <strong>integer is converted into binary bits</strong> , and then and will perform the operation <strong>bit-by-bit</strong> .</li> <li>Here, <strong>1 represents True, and 0 represents False</strong> .</li> <li>After the operation, the binary number will be converted to decimal and returned as the output.</li> </ul> <p> <strong>The operation of bitwise and:</strong> </p> <p>In the bitwise &apos;and&apos; operator, we first convert the decimal number into binary numbers. Then &apos;and&apos; operate on every corresponding bit of the two numbers.</p> <table class="table"> <tr> <th>Bit 1 (operand 1)</th> <th>Bit 2 (operand 2)</th> <th>Return value</th> </tr> <tr> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>1</td> <td>0</td> </tr> </table> <p>Let us take an example:</p> <pre> If num1 = 5 and num2 = 6: 5 -&gt; 101 6 -&gt; 110 Performing &amp; and result is: 100 -&gt; 4 If num1 = 1 and num2 = 2: 1 -&gt; 001 2 -&gt; 010 Performing &amp; and result is: 000 -&gt; 0 </pre> <p>If we perform logical and on 5 and 6, we&apos;ll get True and 6 will be returned:</p> <pre> num1= int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (&apos;The result of logical and: &apos;, num1 and num2) print (&apos;The result of bitwise and: &apos;, num1 &amp; num2) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 5 Enter another number: 6 The result of logical and: 6 The result of bitwise and: 4 </pre> <p>These are the two &apos;and&apos; operators available to use in Python language. Let us see the difference between these two operators:</p> <h2>Logical AND vs. Bitwise AND</h2> <table class="table"> <tr> <th>Logical AND</th> <th>Bitwise AND</th> </tr> <tr> <td>Logical AND is represented by &apos;and.&apos;</td> <td>Bitwise and is represented by &apos;&amp;.&apos;</td> </tr> <tr> <td>It is defined only for Boolean values; even if we use expressions, they are evaluated as True or False.</td> <td>It is defined for integer values as well as Boolean values <br> <strong>Print (True &amp; False) -&gt; False</strong> </td> </tr> <tr> <td>It searches for a False value; if it finds one, it doesn&apos;t evaluate the rest of the expression and returns &apos;False&apos;. This is called &apos; <strong>Lazy evaluation</strong> &apos;.</td> <td>Even if the operator finds a False value, it continues evaluating the rest expressions.</td> </tr> <tr> <td>This operator is mostly used for decision-making and truth testing.</td> <td>Bitwise operators are designed for lower-level bit manipulations.</td> </tr> <tr> <td>In logical and, we need not convert the digit into binary form.</td> <td>In bitwise and, we need to convert the decimal digit into binary form.</td> </tr> <tr> <td>Every number greater than 0 evaluates to &apos;True&apos; and every number == 0 or <0 evaluate to 'false'< td> <td>1 represents &apos;True,&apos; and 0 represents &apos;False&apos;.</td> </0></td></tr> <tr> <td>In the operator hierarchy, Bitwise operators have higher precedence than the logical operators.</td> </tr> </table> <hr></9'></p></9)></pre></50):></pre></=>

'et' avec seulement deux chiffres :

Voyons maintenant ce qui se passe si nous donnons deux entiers décimaux de part et d'autre de l'opérateur « et » :

programme java simple

Prérequis: Généralement, « et » vérifie si les deux opérandes sont vrais. Tout nombre supérieur à 0 représente True et 0 représente False.

 num1 = int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (num1 and num2) 

Exemple de sortie :

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 Enter a number: 3 Enter another number: 4 4 Enter a number: 0 Enter another number: 3 0 Enter a number: 0 Enter another number: 0 0 

Explication:

1. Dans le premier cas :

  1. Les deux nombres sont supérieurs à 0 - Vrai et Vrai condition. Par conséquent, « et » renvoie True.
  2. La condition de « et » est que les deux opérandes soient vrais. Par conséquent, après avoir confirmé que le premier chiffre 3 (>0) est vrai, il vérifie le deuxième chiffre 4 (>0), qui est vrai. Ainsi, il renvoie « Vrai » avec le deuxième nombre.

2. Dans le deuxième cas :

  1. Condition fausse et vraie
  2. Le premier nombre est 0-Faux.
  3. Il ne devrait pas y avoir de « Faux » pour que « et » soit vrai. Par conséquent, une fois qu'il confirme que le premier nombre est « Faux », sans vérifier le deuxième nombre, il renvoie « Faux ».

3. Dans le troisième cas :

  1. Le premier nombre est 0-Faux.
  2. Immédiatement, « et » renvoie False.
  3. Condition fausse et fausse

Voyons un autre exemple :

 num = int (input (&apos;Enter a number: &apos;)) if (num &gt; 0 and num 10 and num <50): 0 10 print (\'num is between and 50\') else: either less than or greater < pre> <p> <strong>Sample Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 67 num is either less than 0 or greater than 50 Enter a number: 3 num is between 0 and 10 Enter a number: 34 num is between 10 and 50 </pre> <p> <strong>Explanation:</strong> </p> <p>We checked if the num belongs in the intervals 0 to 10 or 10 to 50. If the number does not belong to both intervals, the number can either be less than 0 or greater than 50.</p> <p> <strong>Program code:</strong> </p> <p>Here we give an example using the &apos;and&apos; operator. We multiplied three numbers using the &apos;and&apos; operator and displayed the result. The code is given below -</p> <pre> num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (&apos;The result is: &apos;, num1 and num2 and num3) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 The result is: 0 </pre> <p> <strong>Explanation:</strong> </p> <p>Firstly, we enter the value of num1, which is a positive non-zero value. Then it is checked, and it returns true. After that, we enter the value of num2, it is a positive non-zero value. It is checked, and it returns true. Lastly, we enter the value of num3, and it is a zero. Then it is checked, and num3 returns false.</p> <p> <strong>Program code:</strong> </p> <p>Here we give an example using the &apos;and&apos; operator. Using the &apos;and&apos; operator, we checked multiple numbers greater or less than the given number. If all the condition is true, then it returns true. Otherwise, it returns false. Then display the result. The code is given below -</p> <pre> num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (num1 &gt; 30 and num2 &gt; 23 and num3 <9) < pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 False </pre> <p> <strong>Explanation:</strong> </p> <p>Firstly, we enter the value of num1, and it is 4. Then we checked the condition &apos;num1&gt;30,&apos; which returned false. After that, we enter the value of num2, which is 5. Then we checked the condition &apos;num2&gt;23,&apos; and num2 returned false. Lastly, we enter the value of num3, and it is 0. Then we checked the condition &apos;num3<9\' and num3 returns true. num1 num2 return false, so, the output of this code is false.< p> <h2>Bitwise AND (&amp;):</h2> <p>There is another &apos;and&apos; operator in Python. It is a bitwise operator. We represent it as &apos;&amp;&apos;.</p> <p>Binary language is the language of a computer. All the inner mechanisms happen with respect to bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. There are six bitwise operators:</p> <ol class="points"> <li>Bitwise AND</li> <li>Bitwise OR</li> <li>Bitwise NOT</li> <li>Bitwise XOR</li> <li>Bitwise right shift</li> <li>Bitwise left shift</li> </ol> <p>The difference between (logical or, bitwise or), (logical and, bitwise and), (logical not, bitwise not) lies in the word &apos;bitwise&apos; itself.</p> <ul> <li>If we use any bitwise operator, first, the <strong>integer is converted into binary bits</strong> , and then and will perform the operation <strong>bit-by-bit</strong> .</li> <li>Here, <strong>1 represents True, and 0 represents False</strong> .</li> <li>After the operation, the binary number will be converted to decimal and returned as the output.</li> </ul> <p> <strong>The operation of bitwise and:</strong> </p> <p>In the bitwise &apos;and&apos; operator, we first convert the decimal number into binary numbers. Then &apos;and&apos; operate on every corresponding bit of the two numbers.</p> <table class="table"> <tr> <th>Bit 1 (operand 1)</th> <th>Bit 2 (operand 2)</th> <th>Return value</th> </tr> <tr> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>1</td> <td>0</td> </tr> </table> <p>Let us take an example:</p> <pre> If num1 = 5 and num2 = 6: 5 -&gt; 101 6 -&gt; 110 Performing &amp; and result is: 100 -&gt; 4 If num1 = 1 and num2 = 2: 1 -&gt; 001 2 -&gt; 010 Performing &amp; and result is: 000 -&gt; 0 </pre> <p>If we perform logical and on 5 and 6, we&apos;ll get True and 6 will be returned:</p> <pre> num1= int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (&apos;The result of logical and: &apos;, num1 and num2) print (&apos;The result of bitwise and: &apos;, num1 &amp; num2) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 5 Enter another number: 6 The result of logical and: 6 The result of bitwise and: 4 </pre> <p>These are the two &apos;and&apos; operators available to use in Python language. Let us see the difference between these two operators:</p> <h2>Logical AND vs. Bitwise AND</h2> <table class="table"> <tr> <th>Logical AND</th> <th>Bitwise AND</th> </tr> <tr> <td>Logical AND is represented by &apos;and.&apos;</td> <td>Bitwise and is represented by &apos;&amp;.&apos;</td> </tr> <tr> <td>It is defined only for Boolean values; even if we use expressions, they are evaluated as True or False.</td> <td>It is defined for integer values as well as Boolean values <br> <strong>Print (True &amp; False) -&gt; False</strong> </td> </tr> <tr> <td>It searches for a False value; if it finds one, it doesn&apos;t evaluate the rest of the expression and returns &apos;False&apos;. This is called &apos; <strong>Lazy evaluation</strong> &apos;.</td> <td>Even if the operator finds a False value, it continues evaluating the rest expressions.</td> </tr> <tr> <td>This operator is mostly used for decision-making and truth testing.</td> <td>Bitwise operators are designed for lower-level bit manipulations.</td> </tr> <tr> <td>In logical and, we need not convert the digit into binary form.</td> <td>In bitwise and, we need to convert the decimal digit into binary form.</td> </tr> <tr> <td>Every number greater than 0 evaluates to &apos;True&apos; and every number == 0 or <0 evaluate to \'false\'< td> <td>1 represents &apos;True,&apos; and 0 represents &apos;False&apos;.</td> </0></td></tr> <tr> <td>In the operator hierarchy, Bitwise operators have higher precedence than the logical operators.</td> </tr> </table> <hr></9\'></p></9)></pre></50):>

Explication:

Nous avons vérifié si le nombre appartient aux intervalles de 0 à 10 ou de 10 à 50. Si le nombre n'appartient pas aux deux intervalles, le nombre peut être soit inférieur à 0, soit supérieur à 50.

Code du programme :

Nous donnons ici un exemple utilisant l'opérateur « et ». Nous avons multiplié trois nombres à l'aide de l'opérateur « et » et affiché le résultat. Le code est donné ci-dessous -

 num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (&apos;The result is: &apos;, num1 and num2 and num3) 

Sortir:

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 The result is: 0 

Explication:

Tout d’abord, nous entrons la valeur de num1, qui est une valeur positive non nulle. Ensuite, il est vérifié et renvoie vrai. Après cela, nous entrons la valeur de num2, c'est une valeur positive non nulle. C'est vérifié et cela renvoie vrai. Enfin, nous entrons la valeur de num3, et c'est un zéro. Ensuite, il est vérifié et num3 renvoie false.

Code du programme :

Nous donnons ici un exemple utilisant l'opérateur « et ». À l'aide de l'opérateur « et », nous avons vérifié plusieurs nombres supérieurs ou inférieurs au nombre donné. Si toutes les conditions sont vraies, alors elle renvoie vrai. Sinon, il renvoie faux. Affichez ensuite le résultat. Le code est donné ci-dessous -

 num1 = int (input (&apos;Enter the first value: &apos;)) num2 = int (input (&apos;Enter the second value: &apos;)) num3 = int (input (&apos;Enter the third value: &apos;)) print (num1 &gt; 30 and num2 &gt; 23 and num3 <9) < pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter the first value: 4 Enter the second value: 5 Enter the third value: 0 False </pre> <p> <strong>Explanation:</strong> </p> <p>Firstly, we enter the value of num1, and it is 4. Then we checked the condition &apos;num1&gt;30,&apos; which returned false. After that, we enter the value of num2, which is 5. Then we checked the condition &apos;num2&gt;23,&apos; and num2 returned false. Lastly, we enter the value of num3, and it is 0. Then we checked the condition &apos;num3<9\' and num3 returns true. num1 num2 return false, so, the output of this code is false.< p> <h2>Bitwise AND (&amp;):</h2> <p>There is another &apos;and&apos; operator in Python. It is a bitwise operator. We represent it as &apos;&amp;&apos;.</p> <p>Binary language is the language of a computer. All the inner mechanisms happen with respect to bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. There are six bitwise operators:</p> <ol class="points"> <li>Bitwise AND</li> <li>Bitwise OR</li> <li>Bitwise NOT</li> <li>Bitwise XOR</li> <li>Bitwise right shift</li> <li>Bitwise left shift</li> </ol> <p>The difference between (logical or, bitwise or), (logical and, bitwise and), (logical not, bitwise not) lies in the word &apos;bitwise&apos; itself.</p> <ul> <li>If we use any bitwise operator, first, the <strong>integer is converted into binary bits</strong> , and then and will perform the operation <strong>bit-by-bit</strong> .</li> <li>Here, <strong>1 represents True, and 0 represents False</strong> .</li> <li>After the operation, the binary number will be converted to decimal and returned as the output.</li> </ul> <p> <strong>The operation of bitwise and:</strong> </p> <p>In the bitwise &apos;and&apos; operator, we first convert the decimal number into binary numbers. Then &apos;and&apos; operate on every corresponding bit of the two numbers.</p> <table class="table"> <tr> <th>Bit 1 (operand 1)</th> <th>Bit 2 (operand 2)</th> <th>Return value</th> </tr> <tr> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>0</td> <td>0</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>1</td> <td>0</td> </tr> </table> <p>Let us take an example:</p> <pre> If num1 = 5 and num2 = 6: 5 -&gt; 101 6 -&gt; 110 Performing &amp; and result is: 100 -&gt; 4 If num1 = 1 and num2 = 2: 1 -&gt; 001 2 -&gt; 010 Performing &amp; and result is: 000 -&gt; 0 </pre> <p>If we perform logical and on 5 and 6, we&apos;ll get True and 6 will be returned:</p> <pre> num1= int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (&apos;The result of logical and: &apos;, num1 and num2) print (&apos;The result of bitwise and: &apos;, num1 &amp; num2) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -</p> <pre> Enter a number: 5 Enter another number: 6 The result of logical and: 6 The result of bitwise and: 4 </pre> <p>These are the two &apos;and&apos; operators available to use in Python language. Let us see the difference between these two operators:</p> <h2>Logical AND vs. Bitwise AND</h2> <table class="table"> <tr> <th>Logical AND</th> <th>Bitwise AND</th> </tr> <tr> <td>Logical AND is represented by &apos;and.&apos;</td> <td>Bitwise and is represented by &apos;&amp;.&apos;</td> </tr> <tr> <td>It is defined only for Boolean values; even if we use expressions, they are evaluated as True or False.</td> <td>It is defined for integer values as well as Boolean values <br> <strong>Print (True &amp; False) -&gt; False</strong> </td> </tr> <tr> <td>It searches for a False value; if it finds one, it doesn&apos;t evaluate the rest of the expression and returns &apos;False&apos;. This is called &apos; <strong>Lazy evaluation</strong> &apos;.</td> <td>Even if the operator finds a False value, it continues evaluating the rest expressions.</td> </tr> <tr> <td>This operator is mostly used for decision-making and truth testing.</td> <td>Bitwise operators are designed for lower-level bit manipulations.</td> </tr> <tr> <td>In logical and, we need not convert the digit into binary form.</td> <td>In bitwise and, we need to convert the decimal digit into binary form.</td> </tr> <tr> <td>Every number greater than 0 evaluates to &apos;True&apos; and every number == 0 or <0 evaluate to \'false\'< td> <td>1 represents &apos;True,&apos; and 0 represents &apos;False&apos;.</td> </0></td></tr> <tr> <td>In the operator hierarchy, Bitwise operators have higher precedence than the logical operators.</td> </tr> </table> <hr></9\'></p></9)>

Explication:

Tout d'abord, nous entrons la valeur de num1, et elle est 4. Ensuite, nous avons vérifié la condition « num1>30 », qui a renvoyé faux. Après cela, nous entrons la valeur de num2, qui est 5. Ensuite, nous avons vérifié la condition « num2>23 » et num2 a renvoyé false. Enfin, nous entrons la valeur de num3, et elle est 0. Ensuite nous avons vérifié la condition 'num3<9\' and num3 returns true. num1 num2 return false, so, the output of this code is false.< p>

ET au niveau du bit (&) :

Il existe un autre opérateur « et » en Python. C'est un opérateur au niveau du bit. Nous le représentons par '&'.

Le langage binaire est le langage d'un ordinateur. Tous les mécanismes internes se produisent par rapport aux bits. Les opérateurs au niveau du bit sont l'ensemble des opérateurs qui permettent au programmeur d'effectuer des opérations au niveau du bit sur des nombres entiers. Il existe six opérateurs au niveau du bit :

  1. ET au niveau du bit
  2. OU au niveau du bit
  3. PAS au niveau du bit
  4. XOR au niveau du bit
  5. Décalage à droite au niveau du bit
  6. Décalage vers la gauche au niveau du bit

La différence entre (logique ou, au niveau du bit ou), (logique et, au niveau du bit et), (logique non, au niveau du bit non) réside dans le mot « au niveau du bit » lui-même.

  • Si nous utilisons un opérateur au niveau du bit, d'abord, le l'entier est converti en bits binaires , puis et effectuera l'opération petit à petit .
  • Ici, 1 représente Vrai et 0 représente Faux .
  • Après l'opération, le nombre binaire sera converti en décimal et renvoyé en sortie.

Le fonctionnement du bit à bit et :

Dans l'opérateur « et » au niveau du bit, nous convertissons d'abord le nombre décimal en nombres binaires. Ensuite, « et » opère sur chaque bit correspondant des deux nombres.

Bit 1 (opérande 1) Bit 2 (opérande 2) Valeur de retour
0 0 0
1 0 0
1 1 1
0 1 0

Prenons un exemple:

 If num1 = 5 and num2 = 6: 5 -&gt; 101 6 -&gt; 110 Performing &amp; and result is: 100 -&gt; 4 If num1 = 1 and num2 = 2: 1 -&gt; 001 2 -&gt; 010 Performing &amp; and result is: 000 -&gt; 0 

Si nous effectuons un and logique sur 5 et 6, nous obtiendrons True et 6 sera renvoyé :

 num1= int (input (&apos;Enter a number: &apos;)) num2 = int (input (&apos;Enter another number: &apos;)) print (&apos;The result of logical and: &apos;, num1 and num2) print (&apos;The result of bitwise and: &apos;, num1 &amp; num2) 

Sortir:

Maintenant, nous compilons le code ci-dessus en Python, et après une compilation réussie, nous l'exécutons. Ensuite, le résultat est donné ci-dessous -

 Enter a number: 5 Enter another number: 6 The result of logical and: 6 The result of bitwise and: 4 

Ce sont les deux opérateurs « et » disponibles à utiliser en langage Python. Voyons la différence entre ces deux opérateurs :

ET logique vs ET au niveau du bit

ET logique ET au niveau du bit
Le ET logique est représenté par « et ». Au niveau du bit, il est représenté par « & ».
Il est défini uniquement pour les valeurs booléennes ; même si nous utilisons des expressions, elles sont évaluées comme Vrai ou Faux. Il est défini pour les valeurs entières ainsi que pour les valeurs booléennes
Imprimer (Vrai et Faux) -> Faux
Il recherche une valeur False ; s'il en trouve un, il n'évalue pas le reste de l'expression et renvoie « False ». C'est appelé ' Évaluation paresseuse '. Même si l'opérateur trouve une valeur False, il continue d'évaluer les autres expressions.
Cet opérateur est principalement utilisé pour la prise de décision et les tests de vérité. Les opérateurs au niveau du bit sont conçus pour les manipulations de bits de niveau inférieur.
En logique et, nous n'avons pas besoin de convertir le chiffre sous forme binaire. Au niveau du bit et, nous devons convertir le chiffre décimal en forme binaire.
Chaque nombre supérieur à 0 est évalué à « Vrai » et chaque nombre == 0 ou<0 evaluate to \'false\'< td> 1 représente « Vrai » et 0 représente « Faux ».
Dans la hiérarchie des opérateurs, les opérateurs au niveau du bit ont une priorité plus élevée que les opérateurs logiques.