python Flashcards
Find the last char in str
str[-1]
Find the second to last char in str
str[-2]
Find the whole string apart for the last two chars
str[ :-2]
Find the whole string apart for the first two chars
str[ 2: ]
iterate over str
for i in range(len(str)):
Find the end of [ ] of numbers then check ==9
end = len(nums)
for i in range (end):
if nums [ i ] == 9:
return True
Look for one number in an array of numbers
for number in nums:
a function with if and then else if and else to find your fashion level.
def date_fashion(you, date): if you < 3 or date < 3: return 0 elif you >= 8 or date >=8 : return 2 else: return 1
Find the position of w
astring = “Hello world!”
print(astring.index(‘w’))
Put a string in reverse
print(astring[::-1])
String to upper then lower
print(astring.upper())
print(astring.lower())
The environment
consists of Python standard libraries and pre-installed packages
What does a package always have
__init__.py
What error do you get if the package is missing in your system?
ModuleNotFoundError
List
[1,2,3,4]
List properties
- They are ordered.
- They can contain any objects.
- Their size can be varied.
- They are nestable that is
they can contain other lists
as elements. - Their elements can be accessed
by index. - They are mutable that is
you can perform various functions
on it.
The keys of the dictionary are
immutable , unordered and Unique
Tuples
immutable list.
heterogeneous sequence of elements,
impossible to append, edit or remove any individual elements within a tuple.
while Panel data
are observations over time, of the same characteristic for multiple entities
Using the map function
anw = list(map(lambda x,y: x+y , list_1, list_3 ))
An array ?
Array is it a list of related data types, [ [] [] [] ], used to multidimensional array
Numpy arrays advantage ?
Numpy arrays are helpful due to vectorisation and the ability to broadcast one row against another row, they can only work Arrays.
one dimensional arrays’ are know as ?
‘vectors’. ‘Scalars
NumPy array
np.array( [ [] [] [] ] )
print (A2[2:4:1,2:7:2]) what will happen
Brings row 2 & every digit
and brings columb 2, 6 every other digit
Make a few lists of the same type in np
V2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
to make up a 50 item array and then reshape it to 10 rows by 5 columns ?
np.linspace(1,50).reshape(10,5)
Example of a tuple ?
Tuples have no append or extend method.
Elements cannot be removed from a tuple.
(2,3,4,5,6)
Change a row to a column
B = np.array([1, 2, 3])
output = B[ : , np.newaxis ]
print (output)
Used to delaet column
DataFrame.drop()
Which of the following functions is used for indexing within a dataframe?
DataFrame.iloc()
What does the series constructor look like
pd.Series([10, 20, 30, 40, 50, 60])
how to times Series data with tan
My_Series = (S3+S4).apply(np.tan)
DataFrame drop index rows
shop = shop.drop(shop.index[0 : 247])
shop
out of dataframe slice first 3 lines and first 3 columns
df.iloc[:2,:3]
group items ?
df.groupby( [‘Market’, ‘Sector’] ).groups