0% found this document useful (0 votes)
2 views

Intro_to_Python

The document is an introductory guide to Python programming, covering its advantages, basic syntax, data types, operators, conditional statements, loops, functions, and object-oriented programming concepts. It highlights Python's simplicity, flexibility, and extensive libraries, making it suitable for tasks like machine learning. Key topics include variable declaration, data type categorization, control flow structures, and class inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Intro_to_Python

The document is an introductory guide to Python programming, covering its advantages, basic syntax, data types, operators, conditional statements, loops, functions, and object-oriented programming concepts. It highlights Python's simplicity, flexibility, and extensive libraries, making it suitable for tasks like machine learning. Key topics include variable declaration, data type categorization, control flow structures, and class inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Indian Institute of Technology Madras

Intro to
PYTHON
Table of
CONTENTS
01 02 03 04
Intro Basics Datatypes Operators

05 06 07 08
Conditional Loops Functions OOPs
Statements
INTRODUCTION
Python is a programming language created by Guido Van Rossum

ADVANTAGES OF PYTHON:
1. Simple Syntax : Easy to code, read and understand
2. Has a lot of libraries and modules created by third parties ,
extending its functionality for various tasks
3. Dynamically typed - no need to declare data types explicitly,
making it flexible
4. Object Oriented Programming Language

BUT WHY USE FOR ML?


Python has libraries like NumPy (for numerical calculations) and
Pandas (for analyzing data)
Basics :
Variables: storage containers for values.
This is how we declare variables:

We can display these variables using the print statement:


The syntax is : print(<output you want to be displayed>)
Keywords: reserved words (that have a specific meaning), can not be
used as a variable name, function name.
Formatted Strings:

Comments:

Displays no output, when we run the cell.


DATA TYPES
Datatypes are the categorization of data items. Variables can store data of
different types
Operations that are performed on the data depend on the data type

BUILT-IN DATA TYPES

Int --- integers


Float -- decimal values
String -- characters, words or sentences
Boolean - True or false
None Type
DATA TYPES...

Using the type function we can see the data


type of the variable
DATA TYPES...

Type Casting: Converting data from one type to another

Input statement: Used when we want to take input from the user
Syntax: input(“<Enter message to be displayed>”). Default data type (of input) : str
>, <, ==, >=, <=
Comparison
Logical
Arithmetic Operators
Operators
Operators AND, OR, NOT

+, -, /, *,**,%
Operators
Assignment
Perform different processes on variables and constant
Operators
OPERATORS: These are the special symbols. Eg- + , * , /, etc. =, +=, -=, %=
OPERAND: It is the value on which the operator is applied.
None Data type: None Keyword is used to define a null value, or no value at all.
None is not the same as 0, False, or an empty string. None is a data type of its own
(NoneType) and only None can be None.
Syntax and structure:

‘if’ <condition>:
Conditional If the following condition is true the block under it gets

STATEMENTS exectuted

‘elif’ <condition> :
Used to make decisions in
python Comes to this only if the if statement is false
There can be multiple elif blocks
Keywords:
if, elif, else
we can use either “ if statement” or ‘else’ :
“if..elif.. else” or “if...else” It accounts for the remaining possibilities
Sometimes there can be conditional If none of the above conditions are true then none of the
statements inside conditional statements:: above blocks are executed and the else block executes

Nested Conditional blocks


Nested Conditional Statements:
LOOPING statements
Looping is to repeatedly execute a block of statements until a
particular condition is satisfied.
Iteration: Go through the loop
Nested Loops: Are loops within loops .

For loop While loop


01 02 Lorem ipsum dolor sit amet, consectetur
When thedonumber of
When the number of adipiscing elit, sed eiusmod tempor
iterations
incididunt ut labore etisdolore
not magna
known.
aliqua.
iterations are known. Ut enimThe loop is terminated
ad minim
SYNTAX: based on a given criteria.
SYNTAX:
for < condition>: while < condition>:
<statements to execute> <statements to execute>
FOR loop:

Break statement is used to break out of the loop:

o/p:
While loop:
FUNCTIONS
Some important aspects of the
code:
def ---> first we have to define the Functions are used to accomplish
name of the function along with specific tasks.
the parameters involved They are defined by the user .
arguments------> values given in
Executed only when they are
place of parameters by user, while called.
calling the function Can be called multiple times
Takes in input through
return --------> Optional, if used
then the function returns the parameters, processes it and
output of the return statement gives o/p.
Calling the function: function_name(pass in the parameters)
What is the output of the above code?

6215 648
What is the output of the above code?

This is the
answer:
Object Oriented Programming
As there are only a few built in data types, we can
create new objects (or new data types) as per our
wish using OOP aspect of python
Data type gives a set of properties to an instance of
that data type, so similarly we can give new
properties to our data type
CLASSES
Classes contain blueprints that are used to make objects.

ATTRIBUTES METHODS CLASS

Features of an object are Different functions/


called Attributes/ Fields . operators of the class

E.g:
Called like :
A car will have engine,
Myobj.function()
color, brand

Objects are the instance of a class.


Each class instance can have attributes attached to it for maintaining its state. Class instances can
also have methods (defined by their class) for modifying their state.
SOME MORE INFO ON CLASSES:
Class and instance variables:
Class Variable: These variables are shared by all instances of the class, their value is assigned in the class
Instance Variable: Unique to each instance, their value is assigned inside a constructor or a method with self

__init__ method:
Used to initializing the object’s state.
It runs as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do
with your object.
We must pass the parameters of the init method, else it raises an error

self parameter:
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs
to the class.
It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any
method in the class.
Inheritance:
It is a mechanism that allows you to create a hierarchy of classes that share a set
of properties and methods by deriving a class from another class.

Syntax:
Class BaseClass: # parent class
{Body}
Class DerivedClass(BaseClass): #child class
{Body}

You might also like