for i in range(3):
Code can be put between two ticks (key above tab).
To make a bullet list, use the star *
, plus +
, or minus -
.
For more markdown such as formatting tables, check out the markdown cheatsheet.
To render math in LaTeX, use the dollar sign $
3x+47=7 vs. $3x+4y=7$
There are two ways to render an equation:
Fractions using \frac{numerator}{denominator}
Square root using \sqrt{}
For more, check out the LaTeX symbols list. If you are having a hard time figuring out how to do something with LaTeX, Google it (it's what I do) or ask me.
3+4
3*4
3*(4+7)
3**2
12/3
There are two types of numerical data in Python, integers int
and floating point number float
. Different data types are treated differently by the code.
type(4.0)
type(4)
You can convert between data types using specialized functions.
int(4.0)
float(4)
When it comes to division, it really matters if you are working with integers or floats.
12//3
17/4
17//4
17%4
Check if a number is even by calculating the remainder when you divide by 2 (modulo 2)
20%2
19%2
If the remainder is 0 then the number is even. This can be generalized to check if a number $ n $ is divisible by a number $ d $ by checking if n%d
is zero.
Data can be stored as a variable.
x = 3
2*x**2 - 4*x - 8
Three rules for naming variables
_
Not a rule but advice
growth_rate = 0.045
# Examples of words that can't be used for variable names
# don't try to run this cell
sum
list
int
Loops tell Python to run the same code over and over again based on a changing variable.
for i in range(3):
print(i)
total = 0
for i in range(10):
total = total + i
print(total)
total = 0
for i in range(1001):
total = total + i
print(total)
Sum of even numbers only
total = 0
for i in range(0,1001,2):
total = total + i
print(total)
Sum of odd numbers only
total = 0
for i in range(1,1001,2):
total = total + i
print(total)
list1 = [1,3,7,18,4.92]
list1[3]
list1[0]
len(list1)
list1[1:3]
list1[:3]
list1[3:]
list1[-1]
list1[-2]
list2 = [5,7,9]
list1+list2
list2*3
list1.append(86)
print(list1)
list1.insert(0,38)
print(list1)
list1.pop(0)
print(list1)
tup = (5,7,3,38)
tup[0]
tup[:2]
tup[-1]
tup.append(4)
tup = ()
4 == 4.0
x=568
if x%2 == 0:
print('It is even')
x < 500
x >= 568
x%2 == 1 and x < 500
x%2 == 1 or x < 600
not x < 600
x=668
if x%2 == 0 and x < 600:
print('It is an even number less than 600')
else:
print('It is either odd or larger than 600')
x=667
if x%2 == 0:
print('The number is even')
elif x < 600:
print('The number is less than 600')
else:
print('It is either odd and larger than 600')
total = 0
for i in range(0,1001,7):
total = total + i
print(total)
total = 0
for i in range(0,1001):
if i%7 == 0:
total = total + i
print(total)
Exercise Write a conditional statement that indicates True
if the number 567 is divisible by 21 and False
if it is not.
Exercise Modify your code so that is checks the number stored in the variable num
(you may want to store something in num
first).
Exercise Write code that creates a list of all of the divisors of 567.
def Divisors(num):
list_of_divisors = []
for div in range(1,num+1):
if num%div == 0:
list_of_divisors.append(div)
return list_of_divisors
Divisors(567)
Exercise Write a function that returns True
if a given number is prime and False
if it is not.
What is the sum of the first 100 square integers?
result = 0
for i in range(101):
result += i**2
result
Write a loop that creates a list of 100 odd integers.
nums = []
for n in range(1,201,2):
nums.append(n)
print(nums)
len(nums)
nums = []
for n in range(201):
if n%2 == 1:
nums.append(n)
print(nums)
nums = []
for n in range(100):
nums.append(2*n+1)
print(nums)
Write a loop that creates a list of 100 numbers that are congruent to 2 modulo 5.
nums = []
for n in range(2,501,5):
nums.append(n)
print(nums)
len(nums)
nums = []
for n in range(501):
if n%5 == 2:
nums.append(n)
print(nums)
def sum_of_squares(n):
result = 0
for i in range(n+1):
result += i**2
return result
sum_of_squares(100)
sum_of_squares(357)
x = 5280
q = 0
while x >= 13:
x -= 13
q +=1
q
406*13
5280//13
Write a function that takes an integer $ n $ as input and returns $ \sum_{k=0}^n \frac{1}{k} $ as output.
Write a function that takes an integer $ n $ and input and returns a list of its divisors as output.
Write a function that determines if an input integer $ n $ is a prime number, returning True if it is and False if its not.
Write a function that take an integer $ k $ as input and returns a list of all the prime numbers less than or equal to $ k $ as output.
Write a function that takes an integer $ n $ as input and returns a list containing it's prime factorization as output.
Write a function that checks if two integers $ m $ and $ n $ are coprime, returning True if they are and False if they aren't.