Chapter 2 - Introduction to Python

1. Python

Python is a programming language that has been under development for over 25 years [1].

1.1 Statements

Python is an imperative language based on statements. That is, programs in Python consists of lines composed of statements. A statement can be:

  • a single expression
  • an assignment
  • a function call
  • a function definition
  • a statement; statement

1.1.1 Expressions

  • Numbers
    • integers
    • floating-point
    • complex numbers
  • strings
  • boolean values
  • lists

1.1.1.1 Numbers


In [1]:
1


Out[1]:
1

In [ ]:
2

In [10]:
-3


Out[10]:
-3

In [2]:
1
2


Out[2]:
2

In [5]:
3.14


Out[5]:
3.14

1.1.1.2 Strings


In [7]:
'apple'


Out[7]:
'apple'

In [8]:
"apple"


Out[8]:
'apple'

Notice that the Out might not match exactly the In. In the above example, we used double-quotes but the representation of the string used single-quotes. Python will default to showing representations of values using single-quotes, if it can.

1.1.1.3 Boolean Values


In [21]:
True


Out[21]:
True

In [22]:
False


Out[22]:
False

1.1.1.4 Lists

List, and a read-only list (called a tuple).


In [11]:
[1, 2, 3]


Out[11]:
[1, 2, 3]

In [23]:
(1, 2, 3)


Out[23]:
(1, 2, 3)

In [24]:
1, 2, 3


Out[24]:
(1, 2, 3)

1.1.2 Function Calls

There are two ways to call functions in Python:

  1. by pre-defined infix operator name
  2. by function name, followed by parentheses

Infix operator name:


In [4]:
1 + 2


Out[4]:
3

In [9]:
abs(-1)


Out[9]:
1

1.1.2.1 Print

Evaluating and display result as an Out, versus evaluating and printing result (side-effect).


In [3]:
print(1)


1

1.1.3 Special Values


In [12]:
None

1.1.4 Defining Functions


In [16]:
def plus(a, b):
    return a + b

In [17]:
plus(3, 4)


Out[17]:
7

In [18]:
def plus(a, b):
    a + b

In [19]:
plus(3, 4)

What happened? All functions return something, even if you don't specify it. If you don't specify a return value, then it will default to returning None.


In [1]:
"a" + 1


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-bbb7406f1324> in <module>()
----> 1 "a" + 1

TypeError: Can't convert 'int' object to str implicitly

Sidebar 2-1: How to Read Python Error Messages

Python error messages

TypeError: Can't convert 'int' object to str implicitly

Above the error message is the "traceback" also called the "call stack". This is a representation of the sequence of procedure calls that lead to the error. If the procedure call originated from code from a file, the filename would be listed after the word "File" on each line. If the procedure call originated from a notebook cell, then the word "ipython-input-#-HEX".

1.2 Equality

1.2.2 ==


In [2]:
1 == 1


Out[2]:
True

1.2.1 is


In [4]:
[] is []


Out[4]:
False

In [6]:
list() is list()


Out[6]:
False

In [5]:
tuple() is tuple()


Out[5]:
True

In [7]:
57663463467 is 57663463467


Out[7]:
True

In [ ]: