14-Python-Concepts for data science.pptx

ShekharBabar1 15 views 40 slides Jun 15, 2024
Slide 1
Slide 1 of 40
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

python


Slide Content

PYTHON SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

What is Python? High Level , general purpose programming language Developed by Guddo Van Rossum – Netherland at National Research Institute(NRI) in 1989 and made it public in 1991. Python is used to develop Web Applications, desktop applications. e.g. e-commerce websites, Network Applications Game Development Data Analysis Machine Learning, Artificial Intelligence, Data science, Internet of Things(IOT), Deep Learning, Neural Network. Companies using Python – Google, fakebook, NASA, Dropbox, Netflix, Spotify, Quora

Why Python is popular Today? Current market Requirement. Simple. Easy to understand and interpret. Simple like reading English statement. Less code / concise code. Huge library support for Machine Learning, Data Science, Data Analysis, Artificial Intelligence, Deep Learning, Internet of Things (IOT) Freeware & open source.

Features of Python Simple and easy to learn. Freeware and opensource. High Level Programing Language. Dynamically typed. Platform independent. Portability General purpose [oop, procedural programming] Interpreted Extensible Embedded Extensive libraries

Limitations of Python No Library support for Mobile Application, Web Application. Performance is Low. Runtime errors – Requires more testing. Database Access - pythons database access layer is bit underdeveloped and primitive. Memory consumption - High memory consumption.

Pass It is used as placeholder for future implementation of function , loops etc. Null statement. Nothing happens when pass is executed. NOP – No Operation. Loop/ functions that are not implemented yet want to implement in future. It cant have empty body.

Iterators in Python It is simply an object that can be iterated upon. Implemented within for loops , comprehensions, generators, but are hidden . Inside loop it calls next() to get next element and executes the body of the loop with this value. After all items exhaust StopIteration is raised means loop ends Iterated protocol _ _iter_ _() _ _next_ _()

Generators in Python Generator is a function that returns an object (iterator) which can be iterate over (one value at a time). Create generators in Python: Define normal function but with yield statement instead of return . return statement terminates function entirely. yield statement pauses the function saving all its states and later continues from there on successive calls. Attributes of generators: Generator function contains one / more yield statements. _ _ iter_ _() & _ _next_ _() methods implemented automatically. Once function yields it pauses and control transfer to caller. Finally when function terminates StopIteration is raised. Generator expression surrounded by parenthesis. Easy to implement. Memory efficient. Generator represents infinite stream of data.

Python reserved words / keywords 'False', 'None', 'True', '__peg_parser__', 'and', 'as’, 'assert', 'async', 'await', 'break', 'class', 'continue’, 'def', 'del', ' elif ', 'else', 'except', 'finally', 'for’, 'from', 'global', 'if', 'import', 'in', 'is', 'lambda’, 'nonlocal', 'not', 'or', 'pass', 'raise', 'return’, 'try', 'while', 'with', 'yield' Python Reserved words/ keywords can not be used as variables or identifiers

Data type in python bytes bytearray Range None int float complex bool Str list tuple dict set Frozenset

Python List SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Remove duplicate elements from Python List SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Merge Two Python Lists SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Extend & Append function In Python Lists SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Flatten the List of lists 1. nested for loop 2. List comprehension 3. itertools.chain() method list1 = [[1,2,3], [‘python’, ‘MachineLearning’, ‘DataScience’],[2.1,4.5,6.7]] SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Python List vs Tuple SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

List Comprehension SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Enumerate in Python SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

break, continue, pass in Python SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

B reak, continue, pass Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. Break statement Continue statement Pass statement

Break statement in Python SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

continue statement in Python SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

pass statement in Python SHEKHAR SHIVAJI BABAR Initiative by Shekhar’s DataFuture Lab

Function decorators It receives input function as an argument and gives output function with extended functionality. Decorator function extend the functionality of existing /input function without modifying the function. Decorator helps to make the code shorter and pythonic. It acts as a wrapper function. Decorators are a very powerful and useful tool in Python. it allows programmers to modify the behavior of a function or class. Decorators allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. Decorator Function Output Function Input Function

pass As the name suggests pass statement simply does nothing . The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is like null operation, as nothing will happen is it is executed. NOP – null operation Pass statement can also be used for writing empty loops . Pass is also used for empty control statement, function and classes .

continue Continue is also a loop control statement just like the break statement. continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Break statement The break statement is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available.

Enumerate W hen dealing with iterators, we also get need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. Enumerate() method adds a counter to an iterable and returns it in a form of enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() method.

What is List Comprehension? Python List comprehension provides a much shorter syntax for creating a new list based on the values of an existing list. Advantages of List Comprehension More time-efficient and space-efficient than loops. Require fewer lines of code. Transforms iterative statement into a formula. Syntax: List1 = [2,3,4,5,12,13,15,17,20] EvenList = [ i for i in list if i%2 == 0 ] EvenList = [2,4,12,20]

List List1 = [‘python’, 12,2.5] Mutable /changeable Slow – implication of iterations is time consuming Consume more memory List is better for insertion and deletion operations List can not be used as keys in dictionary More built in functions List can be copied Tuple Tup1 = (‘python’, 12,2.5) Immutable / Non changeable Fast – implication of iterations is faster than list Consume less memory Tuple is appropriate for accessing the element Tuple can be used as keys in dictionary due to their hashtable & immutable nature . Less built in functions Tuple cant be copied

Python List list1 = [ ‘shekhar’, 100, 2.4, None] Collection of heterogenous elements enclosed in square bracket. Mutable – can be changed Growable in nature Duplicates are allowed Insertion order is preserved. Scalable Slicing is allowed Indexing is allowed Assignment is allowed

Python tuple tup1 = (‘shekhar’, 100, 2.4, None) Collection of heterogenous elements enclosed in round bracket. Immutable – can be changed Growable in nature Duplicates are allowed Insertion is not allowed. Scalable Slicing is allowed Indexing is allowed Assignment is allowed

Python set set1 = {‘shekhar’, 100, 2.4, None} Collection of heterogenous elements enclosed in curly braces. Mutable – can be changed Growable in nature Duplicates are not allowed Insertion order is not preserved. Scalable Slicing is not allowed Indexing is not allowed Assignment is not allowed Empty set s = set()

Python frozenset set1 = {‘shekhar’, 100, 2.4, None} Collection of heterogenous elements enclosed in curly braces. Immutable – can be changed Duplicates are not allowed Insertion order is not preserved. Slicing is not allowed Indexing is not allowed Assignment is not allowed

Python dictionary dict1 ={‘Name’: ‘Shekhar’, ‘Weight’ : 60} Collection of key value pair enclosed in curly braces. Mutable. Duplicate keys are not allowed. Duplicate values are allowed. Empty dictionary dict2 = { }

Python string str1 = ‘shekhar’ str2 = “Python For Data Science” str3 = “““string is sequence of characters””” String is sequence of characters enclosed in single / double / triple quotes.

Functions vs Methods in Python Functions are declared outside of the class. Functions which are declared inside the class are called Methods Python Method Method is called by its name, but it is associated to an object (dependent). A method definition always includes ‘self’ as its first parameter. A method is implicitly passed the object on which it is invoked. It may or may not return any data. A method can operate on the data (instance variables) that is contained by the corresponding class Functions Function is block of code that is also called by its name. (independent) The function can have different parameters or may not have any at all. If any data (parameters) are passed, they are passed explicitly. It may or may not return any data. Function does not deal with Class and its instance concept.

Python Functions is a block of code that return the specific task defined with name. Python function only runs when it is called. You can pass data known as parameters into functions. Functions are used to perform specific actions and they are also known as methods. commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, Functions can be classified into three types: 1. Built-in functions : These are functions that are already predefined in Python like print(), min(), etc. 2 . User-defined functions : These are the functions that programmers create and use at the places where they need them. 3. Anonymous functions : These are the functions that do not have a name and are defined by the user. These are also called lambda functions. x = max(5, 10) Python functions

Self in python Implicit variable which is pointing towards current object. Self is reference variable internally provided by python. For every instance method and constructor, the first argument should be self. Self keyword used to represent an instance (object) of given class. Self allows access to the attributes & methods of each object in python.

What does the if __name__ == “__main__”: do? Python files are called modules and they are identified by the . py file extension. A module can define functions, classes, and variables. Python files are called modules and they are identified by the . py file extension. A module can define functions, classes, and variables. So when the interpreter runs a module, the __name__ variable will be set as __main__ if the module that is being run is the main program. But if the code is importing the module from another module, then the __name__ variable will be set to that module’s name. If the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name. Module’s name is available as value to __name__ global variable. Advantages : Every Python module has it’s __name__ defined and if this is ‘__main__’, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions. If you import this script as a module in another script, the __name__ is set to the name of the script/module. Python files can act as either reusable modules, or as standalone programs. if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported.
Tags