Home > Backend Development > Python Tutorial > Operators, Conditionals and Inputs

Operators, Conditionals and Inputs

WBOY
Release: 2024-07-27 00:11:14
Original
736 people have browsed it

Operators, Conditionals and Inputs

Operators

Operators are symbols that tell the computer to perform specific mathematical or logical operations.

1.Arithmetic Operators

These operators perform basic mathematical operations like addition, subtraction, multiplication, and division.

*Addition (+): Add two numbers.
eg:

>>>print(1+3)
Copy after login

*Subtraction (-): Subtracts one number from another.
eg:

>>>print(1-3)
Copy after login

Multiplication (): Multiplies two numbers.
eg:

>>>print(1*3)
Copy after login

*Division (/): Divides one number by another.
eg:

>>>print(1/3)
Copy after login

*Floor Division (//): Divides one number by another and rounds down to the nearest whole number.
eg:

>>>print(1//3)
Copy after login

*Modulus (%): Returns the remainder when one number is divided by another.
eg:

>>>print(1%3)
Copy after login

Exponentiation (*): Raises one number to the power of another.
eg:

>>>print(1**3)
Copy after login

2.Comparison Operators

These operators compare two values and return either True or False.

*Equal to (==): Checks if two values are equal.

>>>a = 5
>>>b = 3
>>>result = (a == b)  

>>>result is False
Copy after login

*Not equal to (!=): Checks if two values are not equal.

>>>a = 5
>>>b = 3
>>>result = (a != b)  

>>>result is True
Copy after login

*Greater than (>): Checks if one value is greater than another.

>>>a = 5
>>>b = 3
>>>result = (a > b)  

>>>result is True
Copy after login

*Less than (<): Checks if one value is less than another.

>>>a = 5
>>>b = 3
>>>result = (a < b)  

>>>result is False




</p>
<p>*Greater than or equal to (>=): Checks if one value is greater than or equal to another.<br>
</p>

<pre class="brush:php;toolbar:false">>>>a = 5
>>>b = 3
>>>result = (a >= b)  

>>>result is True
Copy after login

*Less than or equal to (<=): Checks if one value is less than or equal to another

>>>a = 5
>>>b = 3
>>>result = (a <= b)  
>>>result is False




</p>
<h3>
  
  
  3.Logical Operators
</h3>

<p>These operators are used to combine conditional statements.</p>

<p>*and: Returns True if both statements are true.<br>
</p>

<pre class="brush:php;toolbar:false">>>>a = 5
>>>b = 3
>>>result = (a > b and a > 0)  

>>>result is True
Copy after login

*or: Returns True if one of the statements is true.

>>>a = 5
>>>b = 3
>>>result = (a > b or a < 0)  
>>>result is True
Copy after login

*not: Reverses the result, returns False if the result is true.

>>>a = 5
>>>result = not (a > 0)  

>>>result is False
Copy after login

Conditionals

Conditionals are like traffic signals for your code. They help your program decide which path to take based on certain conditions.

1. The if Statement

The if statement checks a condition and executes the code block if the condition is True.
eg:

>>>a = 5
>>>b = 3
>>>if a > b:
    print("a is greater than b")
Copy after login

2. The elif Statement

The elif statement is short for “else if”. It checks another condition if the previous if condition was False.
eg:

>>>a = 5
>>>b = 5
>>>if a > b:
    print("a is greater than b")
>>>elif a == b:
    print("a is equal to b")
Copy after login

3. The else Statement

The else statement catches anything that isn’t caught by the preceding conditions.
eg:

>>>a = 3
>>>b = 5
>>>if a > b:
    print("a is greater than b")
>>>elif a == b:
    print("a is equal to b")
>>>else:
    print("a is less than b")
Copy after login

The above is the detailed content of Operators, Conditionals and Inputs. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template