Python for AI | Data Science

Python for AI | Data Science

Python is developed by Guido van Rossum. It is very powerful object oriented programming language. The syntax of python is very simple.

Let’s Learn the basic construct of python programming language:

Variable

A variable is a reserved memory location to store values. A variable name must begin with alphabet/underscore followed by alphabets /numbers/ underscores. you can not use keywords as variable, keywords are reserved word used which convey special meaning to python compiler like class, for, if, else etc. Comment improve the readability of a code. We use # character to start the comment.

tcs-1.png

Different type variable

Python has three basic data type. To check type of variable use in-built type function.

tcs-2.png

isinstance function: checks whether a variable belongs to a particular class and return true and False based on the result.

Python Built in Data Structures

List

  • List is an collection of elements of different type.
  • It is mutable and allow duplicates.
  • To create list pass elements in square brackets and separate elements with comma(‘,’).
  • To access elements in list use index and index start with 0.

tcs-3.png

tcs-4.png

Tuple

  • It is immutable and allow duplicates.
  • It is created by storing elements in round brackets and delimited by “,”.
  • Access elements by index and index start with zero.

tcs-5.png

  • Try to change value of tuple.

tcs-6.png

Set

  • It is mutable and does not allow duplicate.
  • It is created by enclosing elements with curly brackets delimited by “,”.

tcs-7.png

Dictionary

  • It is mutable and duplicate is allowed.
  • It is created by enclosing the key-value pairs with in curly brackets delimited by “,”.

tcs-8.png

Control Structure

if….else statement: are control flow statement which run particular code only when a certain condition is satisfied otherwise another set of statements. The syntax of if else statement:

if condition:
   block of code_1
else:
   block of code_2

tcs-9.png

For statement: is a looping statement in which set of statement execute repeatedly. The syntax of for statement:

for variable in sequence:
    block of code

tcs-10.png

Break statement: is used to terminate the loop when a certain condition is met.

Continue statement: is used inside a loop to skip the rest of the statements and jump to the beginning of the loop.

Functions

  • A function is a block of code that can be reused.
  • If is defined using ‘def’ keyword.
  • The syntax of function
    def function_name(function_parameter):
        block of code
        return # optional
    

tcs-11.png

OOPs(object oriented programming language)

  • OOPs means we can solve problem by creating object.
  • Class is a blueprint for the object. It is defined with keyword class. For example iphone, oppo are object of class phone. The class phone has common attribute color, length, breath, battery.
  • Object is an entity that has attributes and behavior. It is created by assigning classname as function to an variable. Now that variable will become object of class to access class attributes and behavior. For example iphone is object who has attributes such as color, length, breadth etc.
  • Methods: are the behavior of object. Fox example iphone used to capture good picture.

tcs-12.png

Yield: statement halt function’s execution and return a value to caller, after returning value function resumed where it left off. We should use this in place of return, when data is large and memory less as yield don’t store the entire sequence in memory.

tcs-13.png

Keep learning and be innovative.