Session | Date | Content |
---|---|---|
Day 0 | 06/16/2023 (2:30-3:30 PM) | Introduction, Setting up your Python Notebook |
Day 1 | 06/19/2023 (2:30-3:30 PM) | Python Data Types |
Day 2 | 06/20/2023 (2:30-3:30 PM) | Python Functions and Classes |
Day 3 | 06/21/2023 (2:30-3:30 PM) | Scientific Computing with Numpy and Scipy |
Day 4 | 06/22/2023 (2:30-3:30 PM) | Data Manipulation and Visualization |
Day 5 | 06/23/2023 (2:30-3:30 PM) | Materials Science Packages |
Day 6 | 06/26/2023 (2:30-3:30 PM) | Introduction to ML, Supervised Learning |
Day 7 | 06/27/2023 (2:30-3:30 PM) | Regression Models |
Day 8 | 06/28/2023 (2:30-3:30 PM) | Unsupervised Learning |
Day 9 | 06/29/2023 (2:30-3:30 PM) | Neural Networks |
Day 10 | 06/30/2023 (2:30-3:30 PM) | Advanced Applications in Materials Science |
123 # 'int' (integer) type
123
1.23e-2 # 'float' (floating point decimal) type
0.0123
1.2 + 3.4j # 'complex' (complex number type)
(1.2+3.4j)
(3 * (20 / 5)**3) - (0.125 * 64**(1/4))
191.64644660940672
str
) in Python:'Hello' # strings are text enclosed in single quotes ('...')
'Hello'
print(type('Hello')) # prints the type of a python object
<class 'str'>
# The `+` operator concatenates two strings:
'Hello' + 'World'
'HelloWorld'
Variables are named Python objects. We assign values to variables using the =
operator:
number_1 = 1234
number_2 = 5.67
another_variable = 'some text'
=
) behavior:number_3 = number_1 + number_2
print(number_3)
1239.67
count = 0 # creates a variable called 'count'
count = count + 1 # adds 1 to count
print(count)
count += 1 # also adds 1 to count
print(count)
count -= 1 # decreases count by 1
print(count)
1 2 1
bool
type# create some Boolean variables
bool_a = True
bool_b = False
# print out the type of these variables:
print(bool_a, bool_b)
print(type(bool_a), type(bool_b))
True False <class 'bool'> <class 'bool'>
and
or
, and not
operations with bool
variablesand
and or
operations:¶# behavior of the `and` operator:
print(False and False)
print(False and True)
print(True and False)
print(True and True)
False False False True
# behavior of the `or` operator:
print(False or False)
print(False or True)
print(True or False)
print(True or True)
False True True True
not
operation:¶# behavior of the `not` operator:
print(not False)
print(not True)
True False
bool
variables can be nested in parentheses:# create boolean variables:
bool_a = True
bool_b = False
bool_c = False
# evaluate nested expression:
print((not bool_a) and ((not bool_b) or bool_c))
False
bool
type.a < b
a <= b
a > b
a >= b
a == b
a != b
# create some numerical variables:
x = 0.25
y = -3
z = 20
z_2 = 20.0
# simple comparisons:
print(x < y)
print(y < z)
print(x == y)
print(z >= z_2)
False True False True
if
and if
/else
blocks:¶bool
variable or expression.if
block, which only executes if the condition is True
:# syntax of if statements:
if condition:
body
if
statement: checking if the denominator in division is zero.numerator = 1.0
denominator = 0.0
# set quotient to a default value:
quotient = 0.0
# perform division if denominator is nonzero:
if denominator != 0:
quotient = numerator / denominator
# print quotient:
print(quotient)
0.0
if
/else
block:numerator = 1.0
denominator = 0.0
# perform division if denominator is nonzero:
if denominator != 0:
quotient = numerator / denominator
else:
quotient = 0.0
# print quotient:
print(quotient)
0.0
if
/elif
/else
blocks:# initialize a numeric value:
value = 100.0
# print out the sign of x:
if value < 0:
print('Value is negative.')
elif value == 0:
print('Value is zero.')
else:
print('Value is positive.')
Value is positive.
if
statements:# initialize x
x = 0.0
# check if x lies inside [-1,1]
if -1 < x < 1:
# check if x lies at the origin:
if x == 0:
print('x lies at the origin')
print('x lies within [-1,1]')
x lies at the origin x lies within [-1,1]
while
loops and for
loops.while
loops:¶while
loops execute the body as long as a condition is met# syntax of a while loop:
while condition:
body
while
loop example:# initialize a step counter:
step = 0
# execute the `while` loop:
while step < 5:
# print step:
print('step:', step)
# increment step:
step = step + 1
# print "Done" upon completion of the loop:
print('Done')
step: 0 step: 1 step: 2 step: 3 step: 4 Done
for
loops:¶range
keyword:# syntax of a while loop:
for item in sequence:
body
for
loops with the range
functionrange(n)
iterates over the sequence 0,1,2,...,n-2,n-1range(m,n)
iterates over the sequence m,m+1,...,n-2,n-1# iterate over i = 1,2,...,4
for i in range(5):
print('value of i:',i)
value of i: 0 value of i: 1 value of i: 2 value of i: 3 value of i: 4
[
...]
)(
...)
)# example of a list:
my_list = [ 1, 2, 3, 4, 5 ]
# example of a tuple:
my_tuple = (1, 2, 3, 4, 5)
Lists and tuples can be constructed from a sequence of values using the list
and tuple
functions:
# construct list and tuple from a `range`:
range_list = list(range(6))
range_tuple = tuple(range(6))
# print list and tuple:
print(range_list)
print(range_tuple)
[0, 1, 2, 3, 4, 5] (0, 1, 2, 3, 4, 5)
n
using the syntax my_list[n]
/my_tuple[n]
0
, the second is 1
, etc.n
start at the end of the list (-1
is the last element)# initialize list/tuple example:
list_1 = [ 0, 1, 2, 3, 4 ]
tuple_1 = ( 0, 1, 2, 3, 4 )
# access tuples and lists:
print(list_1[0], list_1[1], list_1[2])
print(tuple_1[0], tuple_1[1], tuple_1[2])
# access end of tuple:
print(list_1[-1])
print(tuple_1[-1])
0 1 2 0 1 2 4 4
my_list = [1,2,3,4]
# modify the second element of my_list:
my_list[1] = 200
# append a value to my_list:
my_list.append(500)
# print the modified list:
print(my_list)
[1, 200, 3, 4, 500]
{
...}
)# create a set (duplicate elements will be removed):
example_set = {'A', 'B', 'C', 'A', 'F', 'B'}
# print the set:
print(example_set)
{'C', 'A', 'F', 'B'}
union
, intersection
, and difference
operations:union
: values in either setintersection
values in both setsdifference
: values in first set but not in the second set# Create sets of physicists and chemists:
chemists = { 'Bohr', 'Curie', 'Pauling', 'Berzelius' }
physicists = { 'Planck', 'Bohr', 'Pauli', 'Curie', 'Fermi' }
# print out the result of a set union:
# (i.e. people who are chemists or physicists):
print(chemists.union(physicists))
# print the result of a set intersection:
# (i.e. people who are chemists and physicists):
print(chemists.intersection(physicists))
# print the result of a set difference:
# (i.e. people who are chemists but not physicists):
print(chemists.difference(physicists))
{'Planck', 'Pauli', 'Fermi', 'Curie', 'Bohr', 'Berzelius', 'Pauling'} {'Curie', 'Bohr'} {'Berzelius', 'Pauling'}
:
) separating key-value pairs# create a dictionary representing a person:
person = {
'name' : 'John von Neumann',
'age' : 40,
'occupation' : 'Mathematician',
'birthday' : ('March', 15, 1995)
}
# print the dictionary:
print(person)
{'name': 'John von Neumann', 'age': 40, 'occupation': 'Mathematician', 'birthday': ('March', 15, 1995)}
# create a dictionary representing a person:
person = {
'name' : 'John von Neumann',
'age' : 40,
'occupation' : 'Mathematician',
'birthday' : ('March', 15, 1995)
}
# print the value of the 'name' key:
print(person['name'])
# print the value of the 'birthday' key:
print(person['birthday'])
# update the value of the 'occupation key':
person['occupation'] = 'Physicist'
print(person['occupation'])
# add the new key 'city' with value 'New Jersey':
person['city'] = 'New Jersey'
print(person['city'])
John von Neumann ('March', 15, 1995) Physicist New Jersey
If possible, try to do the exercises.
Bring your questions to our next meeting tomorrow!