In [1]:
# This is a comment. It will not run

In [2]:
1 # This is a comment in the middle of code. It will not run

Out[2]:
1

# Types¶

In [3]:
# If you put something at the end of a code block, Jupyter will print it
1

Out[3]:
1
In [4]:
# Using the "type" keyword will tell you the type of something
type(1)

Out[4]:
int
In [5]:
# Integers are not the same as floats!
type(1.)

Out[5]:
float
In [6]:
# There are multiple ways to declare floats
type(1.0)

Out[6]:
float
In [7]:
# Here is an example of a boolean
type(True)

Out[7]:
bool
In [8]:
# This is the other boolean
type(False)

Out[8]:
bool
In [9]:
# Here is an example of a string
type('Hello world!')

Out[9]:
str
In [10]:
# Here is an example of a list
type([0, 1, 2])

Out[10]:
list
In [11]:
# Here is an example of a NoneType
type(None)

Out[11]:
NoneType

# Variable assignments¶

In [12]:
# Python variables are not math variables.
# They can be assigned and hold a value
x = 1
# Putting a variable at the end of a code block will print it
x

Out[12]:
1
In [13]:
# You can find the type of what the variable holds by using type
type(x)

Out[13]:
int
In [14]:
# Variables can be reassigned
x = 2
x

Out[14]:
2
In [15]:
# The name of a variable is not related to the content at all
two = 2
two

Out[15]:
2
In [16]:
# As you can see, the variable "two" holds 3
two = 3
two

Out[16]:
3
In [17]:
# Variables are case-sensitive
X = 4.

In [18]:
X

Out[18]:
4.0
In [19]:
x

Out[19]:
2
In [20]:
# Variables are case-senstive for any part of the variable
Xyz = 3
Xyz

Out[20]:
3
In [21]:
xyz = 4
xyz

Out[21]:
4

# Operations (integral types)¶

In [22]:
# Python things can be combined using arithmetic operators
1 + 1

Out[22]:
2
In [23]:
# The result of arithmetic operations can be assigned to variables
x = 10. - 5.3
x

Out[23]:
4.7
In [24]:
# The result of an arithmetic operation WILL NOT CHANGE the variable contents
x * 2

Out[24]:
9.4
In [25]:
# as you can see here
x

Out[25]:
4.7
In [26]:
x = x * 2
x

Out[26]:
9.4
In [27]:
# An example of floating point division
4.0 / 2.0

Out[27]:
2.0
In [28]:
# An example of division of integers
4 / 3

Out[28]:
1.3333333333333333
In [29]:
# An example of integer division, that returns ONLY the integral part
4 // 3

Out[29]:
1
In [30]:
# Can you guess the output?
7 // 3

Out[30]:
2
In [31]:
# How about now?
10 // 3

Out[31]:
3
In [32]:
4.0 // 3.0

Out[32]:
1.0
In [33]:
# An example of "modulus" or "mod" or "finding the remainder"
4 % 3

Out[33]:
1
In [34]:
# What is the answer?
7 % 3

Out[34]:
1
In [35]:
# How about now?
10 % 4

Out[35]:
2
In [36]:
# How about now?
10 % 2

Out[36]:
0
In [37]:
# And now?
143 % 5

Out[37]:
3
In [38]:
x = 1
x = x + 1
x

Out[38]:
2
In [39]:
x = 1
x += 1 # This is equivalent to "x = x + 1"
x

Out[39]:
2
In [40]:
x = 10
x -= 5
x

Out[40]:
5
In [41]:
x = 60
x *= 50
x

Out[41]:
3000
In [42]:
x = 100
x /= 10
x

Out[42]:
10.0
In [43]:
x = 100
x //= 10
x

Out[43]:
10
In [44]:
x = 10
y = 10
x == y

Out[44]:
True
In [45]:
x = 10
y = 20
x == y

Out[45]:
False
In [46]:
# Order of operations matters!
# *, /, % happen first, in the order they appear
1 + 3 * 20 % 7

Out[46]:
5

# Operations (boolean types)¶

In [47]:
# Booleans can be combined using logical ands
# The result of logical and is True if and only if both sides are True
True and False

Out[47]:
False
In [48]:
# Booleans can be combined using logical ors
# The result of logical or is True if either side is True
True or False

Out[48]:
True
In [49]:
# You can put operations that result in booleans and combine them
(1 == 1) and (2 == 2)

Out[49]:
True
In [50]:
# Logical negation changes True to False
not True

Out[50]:
False
In [51]:
# and False to True
not False

Out[51]:
True
In [52]:
not (1 == 1)

Out[52]:
False

# Operations (string, list)¶

In [53]:
# Adding strings is called "concatenation"
'Hello' + ' ' + 'world!'

Out[53]:
'Hello world!'
In [54]:
# Aside: can you have a 1-element list?
[0]

Out[54]:
[0]
In [55]:
# Aside: can you have a 0-element list?
[]

Out[55]:
[]
In [56]:
# Adding list is also called "concatenation"
[0] + [1]

Out[56]:
[0, 1]
In [57]:
x = [0]
y = [1]
x + y

Out[57]:
[0, 1]

# Errors¶

In [58]:
# Errors occur when something you do that isn't allowed
# Remember: True is supposed to be capitalized!
true

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-58-a7f31d49508d> in <module>()
1 # Errors occur when something you do that isn't allowed
2 # Remember: True is supposed to be capitalized!
----> 3 true

NameError: name 'true' is not defined
In [59]:
# Variables must be assigned before being used
abcd

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-59-0b76d1a8ccce> in <module>()
1 # Variables must be assigned before being used
----> 2 abcd

NameError: name 'abcd' is not defined
In [60]:
# Once you assign a variable you can use it
abcd = 1

In [61]:
abcd

Out[61]:
1
In [62]:
# Dividing by 0 is undefined and thus an error in Python
1 / 0

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-62-aca7077a9ee5> in <module>()
1 # Dividing by 0 is undefined and thus an error in Python
----> 2 1 / 0

ZeroDivisionError: division by zero