# This is a comment. It will not run
1 # This is a comment in the middle of code. It will not run
1
# If you put something at the end of a code block, Jupyter will print it
1
1
# Using the "type" keyword will tell you the type of something
type(1)
int
# Integers are not the same as floats!
type(1.)
float
# There are multiple ways to declare floats
type(1.0)
float
# Here is an example of a boolean
type(True)
bool
# This is the other boolean
type(False)
bool
# Here is an example of a string
type('Hello world!')
str
# Here is an example of a list
type([0, 1, 2])
list
# Here is an example of a NoneType
type(None)
NoneType
# 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
1
# You can find the type of what the variable holds by using type
type(x)
int
# Variables can be reassigned
x = 2
x
2
# The name of a variable is not related to the content at all
two = 2
two
2
# As you can see, the variable "two" holds 3
two = 3
two
3
# Variables are case-sensitive
X = 4.
X
4.0
x
2
# Variables are case-senstive for any part of the variable
Xyz = 3
Xyz
3
xyz = 4
xyz
4
# Python things can be combined using arithmetic operators
1 + 1
2
# The result of arithmetic operations can be assigned to variables
x = 10. - 5.3
x
4.7
# The result of an arithmetic operation WILL NOT CHANGE the variable contents
x * 2
9.4
# as you can see here
x
4.7
x = x * 2
x
9.4
# An example of floating point division
4.0 / 2.0
2.0
# An example of division of integers
4 / 3
1.3333333333333333
# An example of integer division, that returns ONLY the integral part
4 // 3
1
# Can you guess the output?
7 // 3
2
# How about now?
10 // 3
3
4.0 // 3.0
1.0
# An example of "modulus" or "mod" or "finding the remainder"
4 % 3
1
# What is the answer?
7 % 3
1
# How about now?
10 % 4
2
# How about now?
10 % 2
0
# And now?
143 % 5
3
x = 1
x = x + 1
x
2
x = 1
x += 1 # This is equivalent to "x = x + 1"
x
2
x = 10
x -= 5
x
5
x = 60
x *= 50
x
3000
x = 100
x /= 10
x
10.0
x = 100
x //= 10
x
10
x = 10
y = 10
x == y
True
x = 10
y = 20
x == y
False
# Order of operations matters!
# *, /, % happen first, in the order they appear
1 + 3 * 20 % 7
5
# 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
False
# Booleans can be combined using logical ors
# The result of logical or is True if either side is True
True or False
True
# You can put operations that result in booleans and combine them
(1 == 1) and (2 == 2)
True
# Logical negation changes True to False
not True
False
# and False to True
not False
True
not (1 == 1)
False
# Adding strings is called "concatenation"
'Hello' + ' ' + 'world!'
'Hello world!'
# Aside: can you have a 1-element list?
[0]
[0]
# Aside: can you have a 0-element list?
[]
[]
# Adding list is also called "concatenation"
[0] + [1]
[0, 1]
x = [0]
y = [1]
x + y
[0, 1]
# 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
# 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
# Once you assign a variable you can use it
abcd = 1
abcd
1
# 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