Zach Taiji's Wiki

Home

❯

pages

❯

Python Cheatsheet & Wiki

Python Cheatsheet & Wiki

Oct 04, 20256 min read

  • programming
  • python
  • wiki
  • cheatsheet

banner

Tips

  • Modules 1
    • Import modules at top of file I.e. import mymodule
    • Module functions are called in code with modulename.function. Example: mymodule.greeting("Jonathan")

Operators

Arithmetic

Arithmetic operators are used with numeric values to perform common mathematical operations:

OperatorNameExampleTry it
+Additionx + yTry it »
-Subtractionx - yTry it »
*Multiplicationx * yTry it »
/Divisionx / yTry it »
%Modulusx % yTry it »
**Exponentiationx ** yTry it »
//Floor divisionx // yTry it

Python Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 3x = x + 3Try it »
-=x -= 3x = x - 3Try it »
*=x *= 3x = x * 3Try it »
/=x /= 3x = x / 3Try it »
%=x %= 3x = x % 3Try it »
//=x //= 3x = x // 3Try it »
**=x **= 3x = x ** 3Try it »
&=x &= 3x = x & 3Try it »
|=x |= 3x = x | 3Try it »
^=x ^= 3x = x ^ 3Try it »
>>=x >>= 3x = x >> 3Try it »
<⇐x <⇐ 3x = x << 3Try it »
:=print(x := 3)x = 3
print(x)
Try it »

Python Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExampleTry it
==Equalx == yTry it »
!=Not equalx != yTry it »
>Greater thanx > yTry it »
<Less thanx < yTry it »
>=Greater than or equal tox >= yTry it »
⇐Less than or equal tox ⇐ yTry it »

Python Logical Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExampleTry it
andReturns True if both statements are truex < 5 and  x < 10Try it »
orReturns True if one of the statements is truex < 5 or x < 4Try it »
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)Try it »

Python Identity Operators

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:

OperatorDescriptionExampleTry it
isReturns True if both variables are the same objectx is yTry it »
is notReturns True if both variables are not the same objectx is not yTry it »

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExampleTry it
inReturns True if a sequence with the specified value is present in the objectx in yTry it »
not inReturns True if a sequence with the specified value is not present in the objectx not in yTry it »

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescriptionExampleTry it
&ANDSets each bit to 1 if both bits are 1x & yTry it »
|ORSets each bit to 1 if one of two bits is 1x | yTry it »
^XORSets each bit to 1 if only one of two bits is 1x ^ yTry it »
~NOTInverts all the bits~xTry it »
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offx << 2Try it »
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2Try it »

Conditional Statements

If

Python supports the usual logical conditions from mathematics (see Operators)

These conditions can be used in several ways, most commonly in “if statements” and loops.

An “if statement” is written by using the if keyword.

Example

In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that “b is greater than a”.

a = 33  
b = 200  
if b > a:  
   print("b is greater than a")
 ```

Elif

The elif keyword is Python’s way of saying “if the previous conditions were not true, then try this condition”.

Example

In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that “a and b are equal”.

a = 33  
b = 33
if b > a:
	print("b is greater than a")
elif a == b:
	print("a and b are equal")

Else

The else keyword catches anything which isn’t caught by the preceding conditions.

Example

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that “a is greater than b”.

You can also have an else without the elif.

a = 200  
b = 33
if b > a:
	print("b is greater than a")
elif a == b:
	print("a and b are equal")
else:
	print("a is greater than b")

Short-Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

Example

if a > b: print("a is greater than b")

Short-Hand If … Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

Example

a = 2
b = 330
print("A") if a > b else print("B")

And, Or, and Not

The and keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, AND if c is greater than a:

a = 200  
b = 33  
c = 500  
if a > b and c > a: 
	print("Both conditions are True")

The or keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b, OR if a is greater than c:

a = 200
b = 33
c = 500
if a > b or a > c:
	print("At least one of the conditions is True")

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

Example

Test if a is NOT greater than b:

a = 33
b = 200
if not a > b:
	print("a is NOT greater than b")

More Conditional Statements

See here:

title: "www.w3schools.com"
image: "https://www.w3schools.com/images/colorpicker2000.png"
description: ""
url: "https://www.w3schools.com/python/python_conditions.asp"
favicon: ""
aspectRatio: "85"

Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

Examples

Print each fruit in a fruit list:

fruits = ["apple", "banana", "cherry"]  
for x in fruits:  
  print(x)

Loop through the letters in the word “banana”:

for x in "banana":  
  print(x)

Exit the loop when x is “banana”:

fruits = ["apple", "banana", "cherry"]  
for x in fruits:  
  print(x)  
  if x == "banana":  
    break

Exit the loop when x is “banana”, but this time the break comes before the print:

fruits = ["apple", "banana", "cherry"]  
for x in fruits:  
  if x == "banana":  
    break  
  print(x)

More for loops

title: "W3Schools.com"
image: "https://www.w3schools.com/images/w3schools_logo_436_2.png"
description: "W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more."
url: "https://www.w3schools.com/python/python_for_loops.asp"
favicon: ""
aspectRatio: "52.293577981651374"

Footnotes

Footnotes

  1. More info here ↩


Graph View

  • Tips
  • Operators
  • Arithmetic
  • Python Assignment Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python Identity Operators
  • Python Membership Operators
  • Python Bitwise Operators
  • Conditional Statements
  • If
  • Elif
  • Else
  • Short-Hand If
  • Short-Hand If ... Else
  • And, Or, and Not
  • More Conditional Statements
  • Loops
  • Examples
  • Footnotes

Backlinks

  • Resources & Wiki & Bookmarks

Created with Quartz v4.5.2 © 2025

  • Blog