Monday, May 7, 2018

Udemy: Python for finance and trading alogrithms: syntax


https://www.anaconda.com/download/
download 3.6

https://conda.io/docs/user-guide/tasks/manage-environments.html

conda env create -f environment.yml

activate pyfinance

jupyter notebook

http://localhost:8888/?token=8353cd76ab1525e6aab4b0cf408cac74bf56a05589c68e71


Shift + enter : run
Shift + Tab : doc

define x='12'
x. +tab see all method

name="greg"
print("hi, {}".format(name))
number=12
print("hi, {}, no {}".format(name,number))

print("hi, {x}, {y}".format(y=x,x=no))

#nested list
nested =[1,2,["a","b"]]

#dictionary
d={'key':10, 'key2':'2nd'}
d['key2']

#tuple (can't change items)
t=(1,2,3)

#set
set([1,1])

import math

(1==1) and not (1==2)

# if and else has to on the same column, elif
if 1==2:
    print('hi')
elif 2==2:
    print('2')
else:
    print('3')

#for
seq=[1,2,3,4,5]
for jelly in seq:
   print(jelly)

#while
i=1
while(i<5):
 print('i={}'.format(i))
i=i+1

#range
range(5)

for i in range(0,20,2):
    print(i)

#function
def mF():
  print("hi")

def mF(p):
    print(p)

def mF(p='default'):
    print(p)

def mF(p='default'):
    return(p)

#lambda
lambda var:var*2

seq=[1,2,3]
list(map(lambda var:var*2,seq))

def is_even(num):
    return num%2 ==0
list(filter(is_even,seq))

list(filter(lambda num:num%2==0,seq))

#method
st.lower
st.upper
tweet=" go sp #cool"
tweet,split()
tweet.split('#')[1]
mylist=[1,2,3,4]
mylist.pop()
mylist.pop(1)
2 in mylist

Exercise
prince ** 0.5
import math
math.sqrt(price)

#task 2
stock_index[2:]

task#3
print("the {} is at {}".format(stock_index,price))

task#4
stock_info.keys()
stock_info['sp500']['yesterday']

stock_info['info'][1][2]

task#5
def source_finder(p):
        return  p.split("--")[-1]

def price_finder(p):
    return 'price' in p.lower()


task#6
def count_price(s):
   count = 0
   for word in s.lower().split():
        if 'price' in word:
            count = count + 1
   return count

def count_price(s)
     return s.lower().count('price')

task#7
def avg_price(s):
    return sum(s)/len(s)



No comments:

Post a Comment