Python | Operators | Codecademy (2024)

fajardomike

Python | Operators | Codecademy (2)

fajardomike1 total contribution

fajardomike

Published May 6, 2021Updated Aug 13, 2024

Contribute to Docs

Operators are used to perform various operations on variables and values. The standard arithmetic and assignment operators are the most familiar.

Syntax

The following code snippet uses the assignment operator, =, to set my_variable to the value of num1 and num2 with an arithmetic operator acting on them. For example, if operator represented *, my_variable would be assigned a value of num1 * num2.

my_variable = num1 operator num2

Python operators can be organized into the following groups:

  • Arithmetic operators for performing traditional math evaluations.
  • Assignment operators for assigning values to variables.
  • Comparison operators for comparing two values.
  • Logical operators for combining boolean values.

Arithmetic Operators

Python has the following arithmetic operators:

  • Addition, +, which returns the sum of two numbers.
  • Subtraction, -, which returns the difference of two numbers.
  • Multiplication, *, which returns the product of two numbers.
  • Division, /, which returns the quotient of two numbers.
  • Exponentiation, **, which returns the value of one number raised to the power of another.
  • Modulus, %, which returns the remainder of one number divided by another.
  • Floor division, //, which returns the integer quotient of two numbers.

Assignment Operators

Python includes the following assignment operators:

  • The = operator assigns the value on the right to the variable on the left.
  • The += operator updates a variable by incrementing its value and reassigning it.
  • The -= operator updates a variable by decrementing its value and reassigning it.
  • The *= operator updates a variable by multiplying its value and reassigning it.
  • The /= operator updates a variable by dividing its value and reassigning it.
  • The %= operator updates a variable by calculating its modulus against another value and reassigning it.

Comparison Operators

Python has the following comparison operators:

  • Equal, ==, for returning True if two values are equal.
  • Not equal, !=, for returning True if two values are not equal.
  • Less than, <, for returning True if left value less than right value.
  • Less than or equal to, <=, for returning True if left value is less than or equal to right value.
  • Greater than, >, for returning True if left value greater than right value.
  • Greater than or equal to, >=, for returning True if left value greater than or equal to right value.

Logical Operators

Python has the following logical operators:

  • The and operator returns True if both statements are True.
  • The or operator returns True if either statement is True.
  • The not operator returns True if its associated statement is False.

Order of Operations

Python evaluates an expression in order of precedence as follows:

  • Items in parentheses, (()), have the highest level of precedence, expressions within them are evaluated first.
  • Exponentiation (**)
  • Multiplication and division operators (*, /, // & %)
  • Addition and subtraction (+ & -)
  • Comparison (<, <=, > & >=)
  • Equality (== & !=)
  • not
  • and
  • or

Membership operators

  • The in operator returns True if the element on the left is found within the iterable object on the right. Here, the element on the right side of the in operator must be an iterable object like a list, string, dictionary, etc.
  • The not in operator returns True if the element on the left is not found within the iterable object on the right. Again, the element on the right side of the not in operator must be an iterable object.

Note: Items at the same precedence are evaluated left to right. The exception to this is exponentiation, which evaluates right to left.

All contributors

Looking to contribute?

  • Learn more about how to get involved.
  • Edit this page on GitHub to fix an error or make an improvement.
  • Submit feedback to let us know how we can improve Docs.

Learn Python on Codecademy

Career pathComputer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Checker DenseIncludes 6 CoursesChecker DenseWith Professional CertificationChecker DenseBeginner Friendly75 hours
Career pathData Scientist: Machine Learning SpecialistMachine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Checker DenseIncludes 27 CoursesChecker DenseWith Professional CertificationChecker DenseBeginner Friendly90 hours
CourseLearn Python 3Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.Checker DenseWith CertificateChecker DenseBeginner Friendly23 hours
Python | Operators | Codecademy (2024)
Top Articles
What is a MultiSig wallet? - Bitstamp Learn Center | Learn Center
Average American savings balance by age, household size, and education level
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5798

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.