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:
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.
Different type variable
Python has three basic data type. To check type of variable use in-built type function.
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 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.
- 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.
- Try to change value of tuple.
- It is mutable and does not allow duplicate.
- It is created by enclosing elements with curly brackets delimited by “,”.
- It is mutable and duplicate is allowed.
- It is created by enclosing the key-value pairs with in curly brackets delimited by “,”.
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:
block of code_1
block of code_2
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
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.
- A function is a block of code that can be reused.
- If is defined using ‘def’ keyword.
- The syntax of function
block of code
return # optional
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.
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.
To view whole code file click here.
Keep learning and be innovative.
Python Tutorial - Learn Python Programming with examples
is a very simple yet very powerful object oriented programming language. The syntax of Python is very simple so a…