python Flashcards

1
Q

Find the last char in str

A

str[-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Find the second to last char in str

A

str[-2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Find the whole string apart for the last two chars

A

str[ :-2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Find the whole string apart for the first two chars

A

str[ 2: ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

iterate over str

A

for i in range(len(str)):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Find the end of [ ] of numbers then check ==9

A

end = len(nums)
for i in range (end):
if nums [ i ] == 9:
return True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Look for one number in an array of numbers

A

for number in nums:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

a function with if and then else if and else to find your fashion level.

A
def date_fashion(you, date):
    if you < 3 or date < 3:
        return 0
    elif you >= 8 or date >=8 :
        return 2
    else:
        return 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find the position of w

A

astring = “Hello world!”

print(astring.index(‘w’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Put a string in reverse

A

print(astring[::-1])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

String to upper then lower

A

print(astring.upper())

print(astring.lower())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The environment

A

consists of Python standard libraries and pre-installed packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does a package always have

A

__init__.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What error do you get if the package is missing in your system?

A

ModuleNotFoundError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List

A

[1,2,3,4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

List properties

A
  1. They are ordered.
  2. They can contain any objects.
  3. Their size can be varied.
  4. They are nestable that is
    they can contain other lists
    as elements.
  5. Their elements can be accessed
    by index.
  6. They are mutable that is
    you can perform various functions
    on it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

The keys of the dictionary are

A

immutable , unordered and Unique

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Tuples

A

immutable list.
heterogeneous sequence of elements,
impossible to append, edit or remove any individual elements within a tuple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

while Panel data

A

are observations over time, of the same characteristic for multiple entities

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Using the map function

A

anw = list(map(lambda x,y: x+y , list_1, list_3 ))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

An array ?

A

Array is it a list of related data types, [ [] [] [] ], used to multidimensional array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Numpy arrays advantage ?

A

Numpy arrays are helpful due to vectorisation and the ability to broadcast one row against another row, they can only work Arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

one dimensional arrays’ are know as ?

A

‘vectors’. ‘Scalars

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

NumPy array

A

np.array( [ [] [] [] ] )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

print (A2[2:4:1,2:7:2]) what will happen

A

Brings row 2 & every digit

and brings columb 2, 6 every other digit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Make a few lists of the same type in np

A

V2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

to make up a 50 item array and then reshape it to 10 rows by 5 columns ?

A

np.linspace(1,50).reshape(10,5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Example of a tuple ?

A

Tuples have no append or extend method.
Elements cannot be removed from a tuple.
(2,3,4,5,6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Change a row to a column

A

B = np.array([1, 2, 3])
output = B[ : , np.newaxis ]
print (output)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Used to delaet column

A

DataFrame.drop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Which of the following functions is used for indexing within a dataframe?

A

DataFrame.iloc()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does the series constructor look like

A

pd.Series([10, 20, 30, 40, 50, 60])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

how to times Series data with tan

A

My_Series = (S3+S4).apply(np.tan)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

DataFrame drop index rows

A

shop = shop.drop(shop.index[0 : 247])

shop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

out of dataframe slice first 3 lines and first 3 columns

A

df.iloc[:2,:3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

group items ?

A

df.groupby( [‘Market’, ‘Sector’] ).groups

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Loop

A

for i in range(len(shop)):
if shop.iloc[i][“Close”] > 1420 :
print( shop.iloc[ i ] [ “Close” ] )

38
Q

Work out percent change on row and column

A

shop.iloc[ 1 : 4 , 3 : 4 ].pct_change()

39
Q

To create event driven trading

A

schedule strategy
stream data
place orders

40
Q

How do you change from the ‘base’ environment to an environment named ‘quantra_py’?

A

. conda activate quantra_py

41
Q

VWAP

A

Volume weighted average price

42
Q

FInd help in Pythong

A

Help( ) or ? after item

43
Q

print statement

A

print ( “ {0:1s} “ has {1 : .1 f} outstanding “ . format( name, number ) )

44
Q

magic commands

A

%lmagic , %magic , %who

45
Q

add list item

A

list . append (“jonathon”)

list . insert (0,”jon” )

46
Q

ballian check in list

A

print ( “jon” in family_list)

True

47
Q

Index using np

A

np.argmax(list)

48
Q

Looking for items in a list > 200

A

np.where( (list >200) & (list <300) )

49
Q

What is a set

A

set = {‘a’,’a’,’u’}

50
Q

a = a + 1`

A

a += 1

51
Q

how to get the current working dir

A

import os

os.getcwd()

52
Q

FInd if about data frame

A

df.info() df.describe()

53
Q

read in csv set index

A

data3 = pd.read_csv( “ shop.csv”, index_col=”Date”, parse_dates=True, dayfirst=True)

54
Q

make a data frame

A

close_open = df [ [ ‘Close’, ‘Open’] ]

55
Q

to delete column

A

del df[‘New’]

56
Q

sum a whole column

A

df[‘Volume’].sum()

57
Q

df open <1250

A

df [ ( df[‘Open’] < 1250 ) ]

58
Q

selecting rows

A

df.iloc[ [ 4, 8, 20] ]

59
Q

Selecting two rows and two columns

A

df.iloc[[4, 5], [0,3]]

60
Q

closing change for day before

A

df[‘Close_to_Close’] = 100 * df[‘Close’] . pct_change()

61
Q

Creating a new column called ‘Previous_Close’ by using the shift operator on Close column.

A

df[‘Previous_Close’] = df[ ‘Close’ ].shift(1)

df.head()

62
Q

Creating a new column called ‘MA’ containing 5 day Moving Average of Close prices.

A

df[‘MA_5’] = df[ ‘Close’ ] . rolling (window=n).mean()

63
Q

finding missing values

A

df.isnull().sum()

64
Q

drop nana rows

A

df.dropna(axis=0).head()

65
Q

fill forward

A

df.fillna( method=’ffill’ ).head()

66
Q

function to find range then populate

A
def daily_range(x):
    return x['Close']-x['Open']
df['daily_range'] = df.apply
( daily_range, axis=1 )
67
Q

supervised learning example

A

classification or regression learning

68
Q

unsupervised learning

A

clustering

69
Q

a set order doesn’t matter

A

{ , , ,}

70
Q

what is the difference in continue and break

A

continue is more lie skip

break

71
Q

Mak 1 col all 0

A

df.iloc { 10:50 , 6 } = 0

72
Q

find out directories in package

A

dir( read_csv )

73
Q

show all variables in memory

A

%%whos

74
Q

work out average over 5 days

A

df{new_col} = df[ ‘close” ].rolling(5).mean()

75
Q

Sum of all null values

A

df . isnull() . sum()

76
Q

Change to datatime

A

df = pd.to_datetime( df.index )

77
Q

logical condition

A

con = (df1_msft[“p”] > 0)

df1_msft.loc[ con ]

78
Q

Make the first col an index

A

index_col= [ 0 ] , parse_dates=True

79
Q

Help on function

A

yf . download?

80
Q

today’s date

A

today = datetime.date.today()

81
Q

to get all instance attributes

A

__dict__

82
Q

family tree

A

__mro__

83
Q

pass attributes and methods to child class

A

cls pass

84
Q

Inherit methods from parents

A

Use supper

parent1.__init__(self,age) / parent2.__init__(self,age)

85
Q

Find to days date

A

pd.datetime.now()

86
Q

Date in the past

A

pd.Timedelta(days=30)

87
Q

make a df

A

list1 =[ 1,2,3,4,5,6,7]

test_df = pd.DataFrame( { “name” : list1 } )

88
Q

add to list

A

stock_list.extend( )

89
Q

How to plot scatter

A

DataFrame.plot.scatter(test_df.plot.scatter( x=”name”,y=”name”,c=”red”, figsize=(12,5) ) )

90
Q

work out log returns for stock

A

stock_df[stock] = np.log( [‘Close’] / stock_df[‘Open’] )