Python Tutorial for Data Science — Best Practice from easy to complex problem

Ashish Patel
ML Research Lab
Published in
8 min readApr 5, 2020
Photo by Shahadat Rahman on Unsplash

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

The Python language’s core philosophy is summarized in the document The Zen of Python (PEP 20), which includes aphorisms such as:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Readability counts.

Because of this python is become first choice of Data Scientist. So, Today we are discussing about the python learning easy to complex problem code. So let’s get started.

1. Anaconda & Jupyter Notebook Installation for Python

  • In this Video, you can find step by step installation for python and jupyter notebook.
  • If you don’t want to setup environment, you can use google colaboratory

2. Input and Output

We can give the input value and it’s return the output. with help of input function.

3. Import the Modules

In python, we have multiple module. So here you will learn how to import and use that module.

Three way we can import module

  • import …
  • from … import …
  • import …. as …

4. Basic Grammer of Python

4.1. Identifier

  • Identifier consists of letters, numbers and underscores, in case sensitive, and cannot begin with number
  • Identifiers beginning with an underscore have special meaning. A Single underscore(_foo) represent class attribute that can’t be accessed directly.
  • Double underscore(__foo) represents a private member of the class; a double underscore (__foo__) represents a special method specific identifier in python, such as __init__() represents the class constructor.

4.2. Indent

  • Indent Python code blocks do not use curly brackets to control classes, functions, and other logical judgments, but use indentation to write code groupings. 4 spaces are usually used for indentation.

4.3. Notes

  • Single line comments in Python start with #. Multi-line comments use three single quotation marks (‘‘‘ ’’’) or three double quotation marks (“““ ”””).

4.4. A statement is displayed on multiple lines

In Python statements, the new line is generally used as the end of the statement. But we can use a slash (\) to split a line of statements into multiple lines, as shown below:

If you include [], {} or () brackets in the statement, you do not need to use multi-line connectors.

4.5. Display multiple statements on the same line

Python can use multiple statements on the same line, using semicolons to separate statements.

5. Python Data Types

Python’s built-in data types are lists, tuples, strings, dictionaries, collections, etc. Also commonly used are arrays in numpy, and dataframes and series in pandas.

5.1 Ordered data type:

  • List (list) : is an ordered collection, there is no fixed size, you can modify the size of the list by offset and other methods. The basic form of the list is as follows: [1,2,5,10]
  • Tuple (tuple): is an ordered set, is immutable, can be combined and copied operations will generate a new tuple. The basic form of tuples is as follows: (1,3,6,10)
  • String (string): is also an ordered collection, the basic form of the string such as: ‘hello’.

5.2 Unordered data type:

  • Set (set) : is an unordered set of non-repeating elements. Basic functions include relational operations and elimination of duplicate elements. The basic form of collection is as follows: {‘apple’, ‘orange’, ‘banana’}
  • Dictionary (dictionary) : is an unordered collection of key : value pairs (key: value pairs). The keys must be different from each other (within the same dictionary). The basic form of the dictionary is: {‘jack’: 4098, ‘sape’: 4139}

6. List

6.1 Create a List

6.2. Access List

Subscript access and slice access can be used

6.3 Modify the list

6.4 List commonly used functions

6.5 Commonly used String method

7. Dictionary

7.1 Create Dictionary

7.2. Common dictionary operation methods

8. Tuple

8.1 Create the tuple

8.2 Modify the tuple

9. String

9.1 Create String

  • You can use single quotes, double quotes, or triple quotes to create a string. (\ n means newline, \ t means tab)

9.2 Basic String operations

9.3 Format String

10. Condition Statements

10.1 Multi Branch Structure

  • Switch statements are not supported in Python, and elif can only be used to implement multi-branch selection structures. In addition, the multi-branch selection structure can also be skillfully realized through the dictionary data structure

10.2 & (AND) and | (OR) Conditions

  • And, or, and not in Python are represented by the keywords and, or, not respectively. Python uses logical lists, tuples, sets, etc. as false when doing logical operations. For or, Python will calculate the operation object from left to right and then return the first operation object that is true. Python will stop at the first truth value operand it finds, usually called short-circuit calculation. and will stop on the first fake object.

11. Loop Statements

  • Python provides for loop and while loop (there is no do … while loop in Python).
  • The for loop generally runs faster than the while counter loop.
  • Break statement, terminate the loop during the execution of the statement block, and jump out of the entire loop.
  • The continue statement terminates the current loop during the execution of the statement block, jumps out of the loop, and executes the next loop.
  • The pass statement is an empty statement to maintain the integrity of the program structure. Do nothing, generally used as placeholder statements. Commonly known as code pile.

12. Functions

12.1 Define the function

Use the def keyword to define a function, use return to return the function value, If there is no return, the return value is None.

12.2 Default Parameter

  • Default parameters are placed after common parameters

12.3 Variable Parameter

  • Variable parameters means that the number of incoming parameters is variable, from 1, 2 to any number, or 0.
  • Variable parameters can be defined in * k way. Variable parameters are automatically assembled into a tuple after being passed into the function.

12.4 Keyword Parameters

  • Keyword parameters allow you to pass in 0 or any number of parameters with parameter names.
  • These keyword arguments are automatically assembled into a dict inside the function.

12.5 Mixed parameter

13. Lambda Function(Anonymous Function)

  • Lambda(λ) is just an expression, suitable for defining simpler functions.
  • The lambda(λ) function has its own namespace and cannot access parameters outside its own parameter list or in the global namespace.
  • The lambda(λ) function definition syntax is:

fun = lamda parameter sequence: return value expression

  • Generally speaking, the use of lambda(λ) function can save program expenses and speed up the running speed.

14. iterator

  • Iterators run at the speed of C in Python. Commonly used iterators are as follows.

15. Python Derivation

  • The Derivation in Python is the biggest egg among all the grammatical rules of the Python language.
  • It is simply a super syntactic sugar.
  • It vividly reflects the characteristics of Python language simple, readable and powerful.
  • Once you master it, you can’t put it down, you can’t stop it, and you will never forget it. Python derivation can generate lists, sets and dictionaries.

15.1 List Comprehension

15.2. Dictionary derivation

15.3 Set Comprehension

16. Python Class and Object

  • In Python, everything is an object. Objects are created from classes, and all classes are subclasses of the base class of object.

16.1. Basic concepts of classes and objects

  • Class: class, abstract data structure, encapsulation of data and algorithms. Such as: define a class, dog.
  • Object: object, an instance of a class. Such as: an instance of the dog class, little dot.
  • Attributes: properties, the data part associated with the object. Such as: weight weight, breed breed.
  • Method: methods, the algorithm part associated with the object. Such as: run (), eat (), bark ().

16.2 Create Classes and Objects

16.3 Get object information

Github Notebook : Python Notebook

I hope you have enjoyed this article…!!!

Thanks for reading…!!! share with your friend if you enjoyed this…!!! Comment if you have any doubt on this…!!!

--

--

Ashish Patel
ML Research Lab

LLM Expert | Data Scientist | Kaggle Kernel Master | Deep learning Researcher