02. Python Keywords and Operators

Python Keywords

In Python, certain words are reserved as keywords. These keywords are used by Python to define the syntax and structure of the language. You cannot use these reserved words as variable names, function names, or any other identifier names. Let’s explore Python keywords and understand why we can’t use them as variables.

List of Python Keywords

Python provides a module named keyword that allows us to check the list of keywords in the current version of Python. Here’s how you can get the list of keywords:

				
					
import keyword
print(keyword.kwlist) #The output will be a list of all keywords in Python:

Result: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']




				
			

Keywords have predefined meanings in Python. Therefore, using them as variables will result in a syntax error. Let’s see an example:

				
					not = 'this is not'
print(not)

  File "<string>", line 1
    not = 'this is not'
        ^
SyntaxError: invalid syntax

				
			

This error occurs because not is a keyword in Python, used for logical operations.

Checking if a Word is a Keyword

To avoid such errors, we can check if a word is a keyword before using it as a variable name. The keyword module provides a function iskeyword for this purpose. Here’s how to use it:

				
					import keyword
print(keyword.iskeyword('not'))
# Result: True
				
			

This confirms that not is indeed a keyword in Python.

What we understand?

  • Keywords: Reserved words in Python that have special meaning.
  • Cannot Use as Variables: Keywords cannot be used as variable names.
  • Checking Keywords: Use keyword.iskeyword('word') to check if a word is a keyword.

We will learn more about each keyword in-depth later. For now, remember that keywords cannot be used as variable names in Python.

 

Operators

what are Operators?

Operators are used to perform operations on variables and values.

List of Python Operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

List of Arithmetic Operators:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus
  • ** : Exponentiation
  • // : Floor Division

Examples:

				
					a = 10
b = 3

# Addition
add = a + b
print("Addition:", add)  # Output: 13

# Subtraction
sub = a - b
print("Subtraction:", sub)  # Output: 7

# Multiplication
mul = a * b
print("Multiplication:", mul)  # Output: 30

# Division
div = a / b
print("Division:", div)  # Output: 3.3333333333333335

# Modulus
mod = a % b
print("Modulus:", mod)  # Output: 1

# Exponentiation
exp = a ** b
print("Exponentiation:", exp)  # Output: 1000

# Floor Division
floor_div = a // b
print("Floor Division:", floor_div)  # Output: 3

				
			

Assignment Operators

Assignment operators are used to assign values to variables.

List of Assignment Operators:

  • = : Assign
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
  • **= : Exponentiation and assign
  • //= : Floor division and assign

Examples:

				
					a = 5

# Assign
a = 10
print("Assign:", a)  # Output: 10

# Add and assign
a += 3
print("Add and Assign:", a)  # Output: 13

# Subtract and assign
a -= 2
print("Subtract and Assign:", a)  # Output: 11

# Multiply and assign
a *= 2
print("Multiply and Assign:", a)  # Output: 22

# Divide and assign
a /= 2
print("Divide and Assign:", a)  # Output: 11.0

# Modulus and assign
a %= 3
print("Modulus and Assign:", a)  # Output: 2.0

# Exponentiation and assign
a **= 2
print("Exponentiation and Assign:", a)  # Output: 4.0

# Floor division and assign
a //= 2
print("Floor Division and Assign:", a)  # Output: 2.0

				
			

Comparison Operators

Comparison operators are used to compare two values.

List of Comparison Operators:

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Examples:

				
					a = 10
b = 5

# Equal to
print("Equal to:", a == b)  # Output: False

# Not equal to
print("Not equal to:", a != b)  # Output: True

# Greater than
print("Greater than:", a > b)  # Output: True

# Less than
print("Less than:", a < b)  # Output: False

# Greater than or equal to
print("Greater than or equal to:", a >= b)  # Output: True

# Less than or equal to
print("Less than or equal to:", a <= b)  # Output: False

				
			

Logical Operators

Logical operators are used to combine conditional statements.

List of Logical Operators:

  • and : Returns True if both statements are true
  • or : Returns True if one of the statements is true
  • not : Reverses the result, returns False if the result is true

Examples:

				
					a = True
b = False

# and
print("and:", a and b)  # Output: False

# or
print("or:", a or b)  # Output: True

# not
print("not a:", not a)  # Output: False
print("not b:", not b)  # Output: True

				
			

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.

List of Identity Operators:

  • is : Returns True if both variables are the same object
  • is not : Returns True if both variables are not the same object

Examples:

				
					a = [1, 2, 3]
b = a
c = [1, 2, 3]

# is
print("a is b:", a is b)  # Output: True
print("a is c:", a is c)  # Output: False

# is not
print("a is not b:", a is not b)  # Output: False
print("a is not c:", a is not c)  # Output: True

				
			

Membership Operators

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

List of Membership Operators:

  • in : Returns True if a sequence with the specified value is present in the object
  • not in : Returns True if a sequence with the specified value is not present in the object

Examples:

				
					a = [1, 2, 3]

# in
print("2 in a:", 2 in a)  # Output: True
print("4 in a:", 4 in a)  # Output: False

# not in
print("2 not in a:", 2 not in a)  # Output: False
print("4 not in a:", 4 not in a)  # Output: True

				
			

Bitwise Operators

Bitwise operators are used to compare (binary) numbers.

List of Bitwise Operators:

  • & : AND
  • | : OR
  • ^ : XOR
  • ~ : NOT
  • << : Zero fill left shift
  • >> : Signed right shift

Examples:

				
					a = 10  # 1010 in binary
b = 4   # 0100 in binary

# AND
print("a & b:", a & b)  # Output: 0 (0000 in binary)

# OR
print("a | b:", a | b)  # Output: 14 (1110 in binary)

# XOR
print("a ^ b:", a ^ b)  # Output: 14 (1110 in binary)

# NOT
print("~a:", ~a)  # Output: -11 (inverts all bits)

# Zero fill left shift
print("a << 2:", a << 2)  # Output: 40 (101000 in binary)

# Signed right shift
print("a >> 2:", a >> 2)  # Output: 2 (10 in binary)

				
			

Tomorrow, we’ll focus on learning conditions in Python. Once we’ve covered that, we’ll move on to practical Nuke examples. Today, we’ll take a break from Nuke-specific applications.

join my 60-day Nuke Python challenge!

Subscribe now!

Leave a Comment

Your email address will not be published. Required fields are marked *