[Ch 2] Everything About Python Operators from the Official Documentation

jun young Yim
July 12, 2025

[Ch 2] Everything About Python Operators from the Official Documentation

Use Original Cover Image
Type
Post
Children
Language
en
Tags
Python
Operator
Authors
jun young Yim
Published
July 12, 2025

Overview

This article provides an easier-to-understand summary of all the content in 3. An Informal Introduction to Python from the official Python v3.13 Tutorial.
In the Python interpreter, the >>> and ... prompts distinguish input from output.
 

1. Using Python as a Calculator

1.1. Numbers

Python can act like a calculator.
* is the multiplication operator, ** is the exponentiation operator, // is the floor division operator, and % is the remainder operator.
The order of operations is: 1st: **, 2nd: *, /, //, %, 3rd: +, -.
  • You can use +, -, *, **, /, //, %, ().
  • Operations are performed in the order of ** > *, /, //, % > +, -.
>>>2+3-(5*2**3/4+7//2+8%5) #In the operation 5*2**3/4, ** is calculated first -11
The data type for integers is int, and for numbers with a fractional part (like 4.0, 1.7), the data type is float. If a calculation involves both floats and ints, the result will be a float. Also, the result of a / operation is always a float, even if the result is a whole number.
  • The data type for integers is int, and for numbers with a fractional part, it is float.
  • The result of a / operation is always a float.
= is used to assign a value to a variable.
_ represents the most recently printed expression.
>>> weight=60 >>> height=1.6 >>> weight/height**2 #This is a BMI calculation 23.437499999999996 >>> _//3 7.0

1.2. Strings

The data type for characters is str.
They can be represented with '...' or "..."
However, when you need to include a ' or " within a string, you must escape it with a \.
To create a newline, use \n. This is only rendered when using print().
However, if you want to include \n as literal characters in a string, you can prefix the string with r to prevent its interpretation.
>>> k='First line.\nSecond line.' >>> k 'First line.\nSecond line.' >>>print(k) First line. Second line. >>>print('C:\some\name') C:\some ame >>>print(r'C:\some\name') C:\some\name
Strings also support arithmetic operations. They can also be concatenated by placing them next to each other without the + operator. However, when concatenating with a variable, the + operator is required.
>>> 5*'doble'+'cheeseburger' 'dobledobledobledobledoblecheeseburger'
You can get the nth character using []. Using a negative number starts from the right. An error will occur if the index is out of range.
Also, using [m:n] allows you to get characters from the (m+1)th to the nth position. This works even if the indices are out of range.
Using [:m] and [m:] you can get characters from the beginning to the mth position, and from the (m+1)th position to the end, respectively.
s[:i] + s[i:] is always equal to s.
len() returns the length of the string.
>>> word = 'python' 'python' >>> word[-6] 'p' >>> word[:2] 'py' >>> len(word) 6
The list is the most versatile compound data type. It is written as a list of comma-separated values between square brackets, like [m, n]. It supports methods similar to strings, as well as addition (concatenation). You can also change the elements of a list.
You can use list.append() to add an item to the end of the list.
List elements can be not only numbers but also strings or even other lists.
list[:] creates a shallow copy of the list. Use this when you want to create a new, modified list while leaving the original unchanged.
>>> primes = [2, 3, 5, 7] >>> primes [2, 3, 5, 7] >>> primes[4]=11 >>> primes [2, 3, 5, 11] >>> kprimes=primes[:] >>> kprimes.append(13) >>> primes [2, 3, 5, 11] >>> len(kprimes) 5 >>> x = [primes, kprimes] [[2, 3, 5, 11], [2, 3, 5, 11, 13]]

Reference

[1] Python Software Foundation. "3. An Informal Introduction to Python." The Python Tutorial, version 3.13, Python Software Foundation, 2024, https://docs.python.org/3.13/tutorial/introduction.html, Accessed 10 July 2025.