Python If Statements

Description

Here we will write a Sage subroutine containing an if/else statement. The subroutine will determine if a number passed to it is odd.

Sage Cell

Code

def isOdd(n):
    if (n % 2 != 0):
        return true
    else:
        return false

Options

If Comparators

When making a comparison there are a total of 6 comparators you may use. Here is the math notation and the Sage equivalent:

  • $>$ is >
  • $<$ is <
  • $\geq$ is >=
  • $\leq$ is <=
  • $=$ is ==
  • $\neq$ is !=

Note that equality is determined with a double equals sign, ==. If statements can also be controlled with boolean variables.

Tags

Primary Tags—Programming: Python

Secondary Tags—Python: Control flow statements

Related Cells

None.

Attribute

Permalink:

Author:

Date: 12 Mar 2019 17:37

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License