Last active
July 13, 2024 09:37
-
-
Save Sarthak-Sidhant/c1937b86f2321fa55758e2b5641d48af to your computer and use it in GitHub Desktop.
Detailed Introduction to Python Operators - Sarthak Sidhant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Edited by: @Sarthak-Sidhant (GitHub) and Copyrights Reserved By https://sarthaksidhant.me/ & https://decodificate.tech/ | |
| # No Parts Of This Text or Code Shall be Reproduced Without Permission, Unless Used For Educational Purposes. | |
| #Operators in Python | |
| #Operators are used to perform operations on variables and values. | |
| #There are 4 types of Python Operators, they are: Arithmetic Operators, Comparison Operators, Assignment Operators, Logical Operators | |
| #Arithmetic Operators | |
| #Arithmetic operators are used with numeric values to perform common mathematical operations: | |
| print("Arithmetic Operators") | |
| print("Addition") | |
| print(10+5) #this adds 10 and 5, output = 15; | |
| print("Subtraction") | |
| print(10-5) #this subtracts 10 and 5, output = 5; | |
| print("Multiplication") | |
| print(10*5) #this multiplies 10 and 5, output = 50; | |
| print("Division") | |
| print(10/5) #this divides 10 and 5, output = 2; | |
| print("Modulus") | |
| print(10%5) #this divides 10 and 5, output = 0; | |
| #what this basically does is that it divides 10 by 5 and gives the remainder, if the remainder is 0, then the output is 0; | |
| print("Exponentiation") | |
| print(10**5) #this raises 10 to the power of 5, output = 100000; | |
| #what this basically does is that it raises 10 to the power of 5, output = 100000; | |
| print("Floor Division") | |
| print(10//5) #this divides 10 and 5, output = 2; | |
| #what this basically does is that it divides 10 by 5 and gives the quotient, output = 2; | |
| #Comparison Operators | |
| #Comparison operators are used to compare two values: | |
| print("Comparison Operators") | |
| print("Equal") | |
| print(10==5) #this compares 10 and 5, output = False; | |
| print("Not Equal") | |
| print(10!=5) #this compares 10 and 5, output = True; | |
| print("Greater Than") | |
| print(10>5) #this compares 10 and 5, output = True; | |
| print("Less Than") | |
| print(10<5) #this compares 10 and 5, output = False; | |
| print("Greater Than or Equal to") | |
| print(10>=5) #this compares 10 and 5, output = True; | |
| print("Less Than or Equal to") | |
| print(10<=5) #this compares 10 and 5, output = False; | |
| #Assignment Operators | |
| #Assignment operators are used to assign values to variables: | |
| print("Assignment Operators") | |
| print("Equal") | |
| x=10 #this assigns value of 10 (int) to x | |
| print(x) #this prints the value of x, output = 10; | |
| print("Addition") | |
| x+=5 #this adds 5 to x, output = 15; | |
| print(x) | |
| print("Subtraction") | |
| x-=5 #this subtracts 5 from x, output = 10; | |
| print(x) | |
| print("Multiplication") | |
| x*=5 #this multiplies x by 5, output = 50; | |
| print(x) | |
| print("Division") | |
| x/=5 #this divides x by 5, output = 10; | |
| print(x) | |
| print("Modulus") | |
| x%=5 #this divides x by 5, output = 0; | |
| print(x) | |
| x=10 #this assigns value of 10 (int) to x, again | |
| print("Exponentiation") | |
| x**=5 #this raises x to the power of 5, output = 100000; | |
| print(x) | |
| print("Floor Division") | |
| x//=5 #this divides x by 5, output = 20000; | |
| print(x) | |
| #Logical Operators | |
| #Logical operators are used to combine conditional statements: | |
| print("Logical Operators") | |
| print("And") | |
| print(10>5 and 10<5) #this compares 10 and 5, output = False; | |
| print("Or") | |
| print(10>5 or 10<5) #this compares 10 and 5, output = True; | |
| print("Not") | |
| print(not(10>5)) #this compares 10 and 5, output = False; | |
| #Identity Operators (Bonus) | |
| #Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, | |
| #with the same memory location: | |
| print("Identity Operators") | |
| print("Is") | |
| x=10 #this assigns value of 10 (int) to x | |
| y=10 #this assigns value of 10 (int) to y | |
| print(x is y) #this compares x and y, output = True; | |
| print("Is Not") | |
| x=10 #this assigns value of 10 (int) to x | |
| y="10" #this assigns value of 10 (int) to y | |
| print(x is not y) #this compares x and y, output = True; | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Text/Code Shall Not Be Used For Commercial Purposes.
It acts as an external gist to my notion and python guide (https://sarthaksidhant.me/python)
For Any Doubts, Kindly Contact Me Through My Github.