Attachment 2 Python Introduction Lyst9139
Attachment 2 Python Introduction Lyst9139
CO
INTRODUCTION
TO
PYTHON
© www.skillathon.co
Oh, yes! About me
© www.skillathon.co
© www.skillathon.co
What we cover & How far?
✔ Introduction
Python basics. ✔ Data types
✔ Variables
✔ Conditional statements
✔ Loops
✔ Functions
✔ File I/O
Python For EDA ✔ Exception handling
© www.skillathon.co
Few things to note about Python
pycharm
© www.skillathon.co
Writing the first python code
>>> x = "Jenny"
>>> print ("Hello John, " + "How are you?“)
>>> print ("%s is my best friend!" %(x))
Hello John, How are you?
Jenny is my best friend!
© www.skillathon.co
Python Variables
Variable:
A named piece of memory that can store a value.
Note:
✔ In python, every variable is an object.
Numbers:
Print (var[0])
Print (var[-2])
Print (var[1:5])
© www.skillathon.co
Data types - Lists
Declaring a list,
Items are enclosed in square brackets [ ] and separated by commas.
Note:
✔ As with the above example, a list can contain items of different data-types.
✔ A list can be nested , i.e. it can also contain another list.
>>> b = [ 1 , 2.0, [ 3 , 3.1 ] ]
✔ To extract an item or a range of items from a list using slicing operation.
>>> b[1]
✔ To delete an item del() function is used.
>>> del ( b [ index ] )
© www.skillathon.co
Data types - tuple
Same as list , i.e ordered sequence of items but unlike lists , tuples
are immutable .
Immutable??
once created , cannot be modified.
>>> a = (1,2,3)
© www.skillathon.co
Tuples & Lists
© www.skillathon.co
Data type: Dictionary
❑ We can use dictionaries to store several entities under same key like
>>> d = {‘phone’:[9923443110,9435431321] , ‘name’: ‘raman’ }
❑ Keys are used to retrieve values.
>>>print ( d[‘name’] )
‘raman’
Note:
✔ Generally used when we have a huge amount of data.
✔ Keys and values can be of any type.
© www.skillathon.co
Conditional Statements
❑ Used to perform different computation or actions depending on whether a condition evaluates to be True or
False
❑ if
❑ else
❑ elif
© www.skillathon.co
Conditional statement : if
❑ if statements are used to gather condition and determine which part of the
code will get executed and which will not.
❑ Syntax:
if condition:
###body
###rest of the code
❑ Example:
>>> a = 4
>>> if ( a % 2 ):
… print ( “ %d is divisible by 2” %(a))
© www.skillathon.co
Conditional Statement : else
❑ If the condition in if is false then the code in else statement gets executed.
❑ Syntax:
if condition:
#code_1
else:
#code_2
❑ Example :
>>> a = 4
>>> if ( a % 2 ):
… print ( “ %d is divisible by 2” %(a))
… else:
… print (“ %d is not divisible by 2 “ %(a))
© www.skillathon.co
Conditional Statement : elif
❑ Syntax:
if condition_1:
#code
elif condition_2:
#code
else:
#code
❑Example :
>>> a = 4
>>> if ( a % 2 ):
… print ( “ %d is divisible by 2” %(a))
… elif ( a % 3 ) :
… print( “ %d is divisible by 3 “ %(a))
… else:
… print (“ %d is neither divisible by 2 nor by 3 “ %(a))
Note :
✔Only one of the block will execute when their condition is true
© www.skillathon.co
Loops
❑Loops in python:
for loop
while loop
© www.skillathon.co
Loops : for loop
❑ for loop iterates a given block of codes for a defined number of times.
Example:
#this code prints numbers from 1 to 10
>>for i in range(1,10):
… print ( i )
Note:
✔ range() is a function that defines the range of loop to iterate.
✔Syntax:
range ( start , stop , step )
Another example:
❑ It’s used when the range is not defined but a condition is specified.
❑Syntax:
>>while condition
… #code
Example:
© www.skillathon.co
Functions
Example :
Output : 16
Note:
✔The body of the function is intended .
✔We call the defined function simply by it’s name.
© www.skillathon.co
File I/O
© www.skillathon.co
Exception handling
❑ Exception : An error that occurs when a program , during it’s execution , runs into a condition that’s not
defined.
❑ Example:
>>print(5/0) #will generate an error
❑ Syntax:
>>>try:
… #code that may raise an exception
>>>except Exception as e: #throwing the exception
… print(e)
© www.skillathon.co
Thanks!
Does anyone have any questions?
© www.skillathon.co