You are on page 1of 72

1) Open Anaconda Navigator

2) launch Jupyter Notebook

and write code

or USe text editor

Sublime Text Editor on PC

Git- distributed version control system

open sourse- small to big projects

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”

No need to write data type and can change type dynamically


Strings are immutable—Meaning-
You cant do the above.

Instead u can concat-

Last = name[1:]

Name = ‘P’ + last

Name gives you ‘Pam’

Mystr = ‘z’

Mystr * 3 gives ‘zzz’

Strings-

Are strings mutable?

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

If u say s1 = l1.sort() – s1 has type none

L1 is inplace sorting

L1.sort()

S1 = l1 will put sorted list in s1

Reverse
Dictionary- Unordered Key Value

Flexible data types- can hold lists or other dicts

Items are tuples

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

t[0] = ‘new’ –type error coz immutable unlike List

Tuples imp for Data integrity so u don modify or reassign elements in large code

SET – you can cast a lit to a set to get unique vaLUES

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

I/O -File read write

Seek to get back to beginning


Do pwd to find present directory and modify it to find ut full file path

Mf.close() ---always close file

Or
List.sort() – inplace sorting wont return result but just sorts list

Sorted(list1) == return sorted list

You cannot sort dictionaries coz they r mappings not sequence and not ordered

Ordered dictionaries are different

Python data tyope numbers = float and integers

So 3.0 == 3 returns true

Square root = ** 0.5

4**0.5 = 2
FOR Loop
Tuples in a list-

Tuple unpacking

Tuple unpacking- *
Dic large are not in order

WHILE Loops

If you go into infinite loop – Restart Kernel

Interrupt Kernel may not work

Break, Continue, Pass


Useful Operators

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

Zips 2 or more lists together

Even if one list islonger same result-


In keyword – to check if exists in a list or dic keys or values in d.values

Min and max

Min(list)

Max(list)

Import functions from a library

From random import shuffle – random is library, shuffle -function in random library
-- Shuffle – in place shuffling like sort

Randint – to generate random int

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

Mylist. And press tab u see list of methods available

Mylist.inser( press shift tab – u see help instructions on method

Help(mylist.insert) – helps too

Whats new in python 3.6

Python library reference

Functions
Docstring—
Default input

Here default n1=0 n2 = 0 so if no parameters given to add it wont throw error

TEST-

Convert word to lower n upper cases- odd letters lower, even upper

-- INCORRECT COZ index brings only first


index of element- if there are duplicates it errors like above result

2 other solutions-
Count primes to 1000

Lambda expressions are one time functions

Map
Filter- returns iterator yielding items that are true

Func returns true or false


Pangram
OOP
Inheritance & Polymorphism
Overwriting methods
Polymorphism- same method name different functions for each object tye-
Abstract class & Inheritance

Abstract class is only a base class- Never to be instantiated


On cmd – if u a;ready have it above result
Google python packages

Always look for pythobn packages on google

Like excel packages for python..


Calling myModule.py ’s myfunc from myProgram.py

To create a Package-

Create a Folder – MyMainPackage and a subfolder in it called MySubPackage

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

Single module is .py script

More modules have to be organized as packages – a folder with __init__.py is a package

You can have many modules in packages.


Create 2 simple py scripts n test

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

Pass func as argument-


You can return functions and also use them as arguments- using these we create decorator
Instead of created new func and assigning it newdecorated(func_needs_decorator)

We can assign @new_decoratoron top of func that needs decorators as follows-


Generators-
Create own generator
#instead of above list of cubes- I need only one element at a time--- so use yield

YIELD
Result is not stored in memory -it simply returns one by one—Memory efficient

Although output looks similar

Fibonacci series-
Print(next(genfunc))

Next operator

Iterator-

Iter()
You cant iterate on any list like a string

U need a generator

U can convert objects that are iterable to use iterator like—


Generator Comprehension = instead of square brackets u use () coverts list comprehension to generator
comprehension

Eg- gencomp = (item for item in squares if item>3)

Weakness –

*args

**kwargs

Map filter lambda

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)]

Type "copyright", "credits" or "license" for more information.

IPython 7.2.0 -- An enhanced Interactive Python.

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)

Out[13]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.arange(0,10,2)

Out[14]: array([0, 2, 4, 6, 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]]

Traceback (most recent call last):


File "<ipython-input-15-4f667ccfe653>", line 1, in <module>

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

exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Maddy/Desktop/Python/Python ML/numpy1.py", line 25, in <module>

np.zeroes(3)

AttributeError: module 'numpy' has no attribute 'zeroes'

np.zeroes(3)

Traceback (most recent call last):

File "<ipython-input-16-9a5b8c55d02e>", line 1, in <module>

np.zeroes(3)

AttributeError: module 'numpy' has no attribute 'zeroes'


np.zeros(3)

Out[17]: array([0., 0., 0.])

np.zeros(3,3)

Traceback (most recent call last):

File "<ipython-input-18-a1af5744063d>", line 1, in <module>

np.zeros(3,3)

TypeError: data type not understood

np.zeros((3,3))

Out[19]:

array([[0., 0., 0.],

[0., 0., 0.],

[0., 0., 0.]])

You might also like