Contents
This notebook is based on "Think Python, 2Ed" by Allen B. Downey
https://greenteapress.com/wp/think-python-2e/
In [1]:
# Integer data
type( 17 )
Out[1]:
In [2]:
# Floating-point data
type( 17.0 )
Out[2]:
In [3]:
# A number inside a string
type( '17' )
Out[3]:
In [4]:
count = 55
size = 42.0
print
function displays the value of a variable
In [5]:
print( count )
type
of a variable is the type of its value
In [6]:
type( count )
Out[6]:
x
, y
, and z
as names unless they have real meaning in the problem (like 3-d points)max
and Max
are different variables
In [7]:
# Operator: + (addition)
# Operands: 3 and 4
3 + 4
Out[7]:
In [8]:
# Operator: - (subtraction)
# Operands: 3 and 4
3 - 4
Out[8]:
In [9]:
# Operator: *tiplication (mul)
# Operands: 3 and 4
3 * 4
Out[9]:
In [10]:
# Operator: / (division)
# Operands: 3 and 4
3 / 4
Out[10]:
//
called floor division
In [11]:
10 / 3
Out[11]:
In [12]:
10 // 3
Out[12]:
float
, the result is a float
In [13]:
5 * 2
Out[13]:
In [14]:
5 * 2.0
Out[14]:
In [15]:
5.0 * 2
Out[15]:
In [16]:
5.0 * 2.0
Out[16]:
In [17]:
1.0 / ( 2 * 3.14159 )
Out[17]:
In [18]:
# Can't divide a string by an integer
# Uncomment to demonstrate
# '13' / 42
+
operator
In [19]:
'fizz' + 'buzz'
Out[19]:
*
operator
In [20]:
'la' * 3
Out[20]:
In [21]:
# Poor example
# Calculate it
y = 2
x = 2 * 3.14 * y
In [22]:
# Good example
# Calculate circumference of a circle
pi = 3.14
radius = 2
circumference = 2 * pi * radius
#
character#
to the end of the line is ignored