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, multiplicatio…


This content originally appeared on DEV Community and was authored by kavin suresh

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)

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

>>>print(1-3)

Multiplication (): Multiplies two numbers.
eg:

>>>print(1*3)

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

>>>print(1/3)

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

>>>print(1//3)

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

>>>print(1%3)

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

>>>print(1**3)

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

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

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

>>>result is True

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

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

>>>result is True

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

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

>>>result is False

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

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

>>>result is True

*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

3.Logical Operators

These operators are used to combine conditional statements.

*and: Returns True if both statements are true.

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

>>>result is True

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

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

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

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

>>>result is False

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")

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")

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")


This content originally appeared on DEV Community and was authored by kavin suresh


Print Share Comment Cite Upload Translate Updates
APA

kavin suresh | Sciencx (2024-07-26T13:57:06+00:00) Operators, Conditionals and Inputs. Retrieved from https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/

MLA
" » Operators, Conditionals and Inputs." kavin suresh | Sciencx - Friday July 26, 2024, https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/
HARVARD
kavin suresh | Sciencx Friday July 26, 2024 » Operators, Conditionals and Inputs., viewed ,<https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/>
VANCOUVER
kavin suresh | Sciencx - » Operators, Conditionals and Inputs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/
CHICAGO
" » Operators, Conditionals and Inputs." kavin suresh | Sciencx - Accessed . https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/
IEEE
" » Operators, Conditionals and Inputs." kavin suresh | Sciencx [Online]. Available: https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/. [Accessed: ]
rf:citation
» Operators, Conditionals and Inputs | kavin suresh | Sciencx | https://www.scien.cx/2024/07/26/operators-conditionals-and-inputs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.