A Crash Course in Programming in Python

The Midwest Big Data Summer School, May 2019

Python 2 vs Python 3

The differences are obvious only in a few places:

    • Use of the print keyword: Python 3 requires parentheses around the values to be printed.
    • Behavior of the division operator:
      • In Python 3, the single slash / operator always performs floating-point division, and a double slash // is integer division.
        25 / 10 is 2.5
        25 // 10 is 2
      • In Python 2, the single slash is integer division if both operands are integers, and floating-point division if at least one operand is floating-point.
        25 / 10 is 2
        25 / 10.0 is 2.5
        25 // 10 is an error
    • In Python 3, the input(...) function always returns a string. If you are reading numeric values, you need to explicitly convert from string to int or float using the int(...) or float(...) functions. There is no function raw_input as in Python 2.

PDF File