How to open a file
open(filename, mode)
-> mode is optinal: r for read, w for write
File Path
absolute -> complete details starting from the root directory through all subdirectories
relative -> starts from the current working directory
and identify relative location of the file
handling file as a sequence
How to get a file in a string
searching through a file
How to get rid of the blanks
write to files
setting the workfing directory
-> getcwd = get current working directory
Strings vs. Lists, both immutable?
-> String: Yes
-> List: No
slicing lists
-> just like we do it in strings
List methods
['append', 'count', 'extend', 'index', 'insert',
'pop', 'remove', 'reverse', 'sort']
del function
-> remove items in list by index
-> [2, 3]
difference append vs extend
append: as one element
extend: as separated entries
Is something in a list?
Sort a list in
alphabetical order
backwards alphabetical order
pop function
pop() removes the last element
removing value when index is unknown
Which methods can we use to delete elements in a list
pop()
del()
remove()
is operator: string vs list
-> True
-> False
(Two lists are equivalent, because they have the same elements, but not identical)
Aliasing
-> [17,2,3]
If the aliased object is mutable, changes made with one alias affect the other
For immutable objects like strings, aliasing is not as much of a problem.
What happens to the list?
When you pass a list to a function, the function gets a reference to the list
functions modifying a list:
append(x)
extend(iterable)
insert(i, x)
remove(x) (by value)
pop([i]) (returns the removed item)
clear()
sort(*, key=None, reverse=False)
reverse()
functions creating a new list:
lst[a:b] (slicing)
lst + other
lst * n
How to initalize a dictionary
dic = {}
dic = dict()
checking if key is in dictionary
ccc = dict()
‘csev‘ in ccc
False
get mehod for dictionaries
-> if there is a value for the key: value = value +1
-> if there is no value for the key: new element added and value = 0 +1
example of how the get metho can be used to count
printing the list
how to get all keys in a dict
how to get all values in a dict
hoew to get the key and value pairs from a list
dictionaries and for loops
tuples
kind of sequence that functions much like a list, they
have elements which are indexed starting at 0
x = ('Glenn', 'Sally', 'Joseph')
print(x[2])
-> Joseph
work with for loops like lists do it
for iter in y:
... print(iter)
-> 1,9,2
Can you change tuples like lists?
No, like strings they are immutable
What you can do with Tuples:
-> always like function(tuple) not tuple.function()
difefrence sort() and sorted()?
list.sort() → method, in place, returns None -> changes list
sorted(iterable) → built-in, returns a new list, works for any iterable (list, tuple, set, …), used for tuples as they are immutable
Can tuples be sliced?
Yes
Why numpy ndarray?
-> characteristics
-> buil in functions
-> 10 to 100 times faster
-> multidimensional container for homogeneous data; that is, all of the elements must be the same type
-> Every array has a shape, a tuple indicating the size of each dimension, and a dtype, an object describing the data type of the array
how will this array look like
-> two rows
-> three columns
3 dimensional arrays:
-> 2,4,3
.
Fancy indexing
-> returns a copy of the array
Reshaping and transposing Array
arr.T
transpose with order = ‘C‘ or ‘F’
-> c for rows
-> F for columns
Universal functions?
A universal function, or ufunc, is a function that performs element-wise operations on data in ndarrays
binary ufunct
• binary ufuncs take two arrays and return a single array as the result
sum and mean functions for ndarray
compute means and sums across axis
-> 0 across rows
-> 1 across columns
Expressing Conditional Logic as Array Operations
numpy.where
sort function for arrays in python
arr.sort()
you can also sort by axis -> automaticcaly ascending
unique function for arrays
np.in1d
tests membership of the values in one array in another, returning a boolean array
matruix multiplication
Last changed14 days ago