0% found this document useful (0 votes)
2 views

PYTHON NOTES

This document contains Python notes covering file directory operations using the os module, including changing directories and managing files. It also discusses iterators with a custom class for powers of two and provides NumPy notes on creating and manipulating arrays, including methods for generating, sorting, and concatenating arrays. Key functions and their usage are highlighted for quick reference.

Uploaded by

Keya Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PYTHON NOTES

This document contains Python notes covering file directory operations using the os module, including changing directories and managing files. It also discusses iterators with a custom class for powers of two and provides NumPy notes on creating and manipulating arrays, including methods for generating, sorting, and concatenating arrays. Key functions and their usage are highlighted for quick reference.

Uploaded by

Keya Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PYTHON NOTES:

FILE DIRECTORIES:
Import os
Print(os.getcwd()) {current working directory)
Print(os.getcwdb()) {current working directory in bytes)

os.chdir(‘…’) {changes directory to the one mentioned in single quotes}


print(os.listdir()) {prints the list of available files and subfiles}

os.mkdir(‘..’) {makes a new directory at that location with the given name}
os.rename(‘..’, ‘..’)
os.rmdir(‘..’) {to remove a directory}
os.remove(‘..’) {to remove a file}

enumerate forms a list


list(enumerate(…)) will enumerate it
ITERS:

list = [1,2,3,4,5]
our_iter = iter(list)
print(next(our_iter))
print(our_iter.__next__())

class powoftwo():
'''the power of 2 is as follows'''
def __init__(self, max=0):
self.max = max
def __iter__(self):
self.num = 0
return self
def __next__(self):
if self.num <= self.max:
result = 2**self.num
self.num += 1
return result
else:
raise StopIteration
print(powoftwo.__doc__)
a = powoftwo(16)
i = iter(a)
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))

NUMPY NOTES:
A = np.array([1,2,3]) to make a one dimensional array.
C = np.array([[1,2,3], [4,5,6], [7,8,9]]) to make a two dimensional array
a.ndim to get the dimension
a.size to find the number of elements
a.shape to know the shape
a.dtype to know the datatype of the elements
np.zeros(b) to create an array with b zeroes
np.ones(b) to create an array with b ones.
Np.empty(b) to create an array with random b elements.
Np.arange(b) creates an array with elements from 0 to b-1.
Np.arange(a,b,c) a is first number, b is last and c is the common difference
between the elements of the array to be created.
Np.linspace(a,b,c) c signifies the number of elements u need within the range a
and b(included) all equally spaced from each other.
Np.sort(a) to sort the elements of the array a in ascending order.
Np.concatenate((a,b)) to join the arrays a and b.
Np.newaxis will increase the dimensions of your array by one dimension when
used once

You might also like