Professional Documents
Culture Documents
https://github.com/Pierian-Data/Complete-Python-3-Bootcamp
github- webpage to put series of our code/projects and share with th public
Python accepts Dynamic Typing
Eg-
Mydog = 2
Mydog = “Sammy”
Last = name[1:]
Mystr = ‘z’
Strings-
Strings are not mutable! (meaning you can't use indexing to change individual
elements of a string)
R:10.3f – 10 adds whitespace , .3f is precision
{value:width.precisionf}
f-strings
LISTS
Ordered sequences
List similar to string but mutable- meaning u cant change an element in string but can do with list
POP
SORT
L1 is inplace sorting
L1.sort()
Reverse
Dictionary- Unordered Key Value
Tuples similar to List but IMMUTABLE and USE parenthesis NOT square brackets
Mytuple = (1,2,3)
Mylist = [1,2,3]
Len(mytuple)
T = (‘one’,2)
T[0] – indexing
T[-1] = 2
T = (‘a’, ‘a’, 2)
t.count(‘a’) -----2
t.index(‘a’) ------0
t.index(‘a’) -1
Tuples imp for Data integrity so u don modify or reassign elements in large code
Myset = set()
Myset.add(1)
Myset.add(2)
Myset
{1,2}
Myset.add(2)
Myset
{1,2}
UNIQUE values!
MYLIST =[ 1,1,1,1,1,3,3,3,3,3,3,2,2,2,2]
SET(MYLIST)
{1,3,2}
You append to a list and you add to a set
Or
List.sort() – inplace sorting wont return result but just sorts list
You cannot sort dictionaries coz they r mappings not sequence and not ordered
4**0.5 = 2
FOR Loop
Tuples in a list-
Tuple unpacking
Tuple unpacking- *
Dic large are not in order
WHILE Loops
Range
Generators -generate data instead of storing it like Range
List(range(0.11.2))
[0,2,4,6,8,10]
ENUMERATE Function
ZIP – Opposite of Enumerate
Min(list)
Max(list)
From random import shuffle – random is library, shuffle -function in random library
-- Shuffle – in place shuffling like sort
Input
Input accepts any string- you can ask number and it can accept a name string
Type is a string
List Comprehensions
Instead of creating empty string and appending --- which takes lots of space-computation time is same
for both methods but that’s only one line..
We can do –
If else –
Nested loop-
Test
Methods and Objects
Functions
Docstring—
Default input
TEST-
Convert word to lower n upper cases- odd letters lower, even upper
2 other solutions-
Count primes to 1000
Map
Filter- returns iterator yielding items that are true
To create a Package-
Create a file -empty file and save it as __init__.py in both main and sub package folders – this tells folder
is not just any directory but a package
Create a .py file inside each package—ex- some_mainscript.py under myMainPackage and
mysubscript.py under subpackage
One.py
Two.py
Result- CMD-
Its mainly for organizing code—
U define functions at the top and then u check if __name__ == “__main__” – if true you run ur functions
in order.
Python Decorators
Mysterious oneliner on top of function
--Decorators—
You can pass function to another function- even if u deleted original function the new one still works
Define function inside a function and be able to return the function based on some conditions
YIELD
Result is not stored in memory -it simply returns one by one—Memory efficient
Fibonacci series-
Print(next(genfunc))
Next operator
Iterator-
Iter()
You cant iterate on any list like a string
U need a generator
Weakness –
*args
**kwargs
Decorator
Error handling
One dimen array is vector
2 d array is matrix
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]
runfile('C:/Users/Maddy/.spyder-py3/temp.py', wdir='C:/Users/Maddy/.spyder-py3')
hello spyder
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[6 7 8]]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[7 8 9]]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[7 8 9]]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[7 8 9]]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[7 8 9]]
np.arange(0,10)
np.arange(0,10,2)
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
[1 2 3 4]
[[1 2 3]
[4 5 6]
[7 8 9]]
runfile('C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py',
wdir='C:/Users/Maddy/Desktop/Python/Python ML')
File "C:\Users\Maddy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
line 704, in runfile
execfile(filename, namespace)
File "C:\Users\Maddy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",
line 108, in execfile
np.zeroes(3)
np.zeroes(3)
np.zeroes(3)
np.zeros(3,3)
np.zeros(3,3)
np.zeros((3,3))
Out[19]: