python Flashcards
(90 cards)
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( [ [] [] [] ] )