0% found this document useful (0 votes)
4 views26 pages

Attachment 2 Python Introduction Lyst9139

The document is an introductory guide to Python, covering its basics such as data types, variables, conditional statements, loops, functions, file I/O, and exception handling. It highlights Python's versatility as a general-purpose programming language and provides examples of code snippets for better understanding. The author, a Data Engineer and mentor, emphasizes the importance of Python in various fields including web development, data analysis, and artificial intelligence.

Uploaded by

kalpeshboratkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Attachment 2 Python Introduction Lyst9139

The document is an introductory guide to Python, covering its basics such as data types, variables, conditional statements, loops, functions, file I/O, and exception handling. It highlights Python's versatility as a general-purpose programming language and provides examples of code snippets for better understanding. The author, a Data Engineer and mentor, emphasizes the importance of Python in various fields including web development, data analysis, and artificial intelligence.

Uploaded by

kalpeshboratkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

SKILLATHON.

CO

INTRODUCTION
TO
PYTHON
© www.skillathon.co
Oh, yes! About me

1. Data Engineer @Redhat


2. Data and Engineering Mentor
@Mentorcruise
3. Worked in couple of AI based Startups
4. Loves Python!

© www.skillathon.co
© www.skillathon.co
What we cover & How far?

What you will learn in Basics?

✔ 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

❑ Python is not new

First appeared in 1991, where as java 1.0 was launched in 1995

❑ Python supports both Procedural & Object oriented approach

❑ Python is general purpose. Can be used for anything, few are..

✔ Web Development Backend


✔ Scientific computing
✔ Scripting the jobs
✔ Data Analysis
✔ Artificial intelligence

❑ Python is slow in performance when compared to Java


Because, it’s too flexible & dynamic typed, it has to reference a lot of things

❑ For more reading - documentation, tutorials, beginners guide, core distribution…


http://python.org
© www.skillathon.co
How/Where to write python code?

Python interactive shell

Enter python on command prompt

Popular Python Editors

pycharm

© www.skillathon.co
Writing the first python code

Python interactive shell

Enter python on command prompt


>>>print(“Hello world”)

print() is python’s inbuilt function

Couple of more samples, you can try..

>>> print (2 + 5 + 9) >>> print (1,2,3,4,5,6,7,8,9,0)


16 1234567890

>>> 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.

✔ we don’t have to declare a variable nor its data type.


© www.skillathon.co
Data types - Numbers

Python’s built in data type lists are..

Numbers, Strings, Lists, Tuples, Set, Dictionary

Numbers:

❑ Integers Assigning numbers to a variable


12 -34
>>> x = 10 #assigning integer
>>> y = 7.0 #assigning floats
❑ Floats – Values with fractional part
>>> z = 2 + 3j #assigning complex number
>>> p = True #assigning Boolean
3.12 123.34523

❑ Complex numbers Note:


To check the type of variable, give
4+3j
type(variable-name) built in function
❑ Booleans
True ,False
© www.skillathon.co
Data types - Strings

Strings are sequence of characters.

✔ Strings can be represented in single or double quotes


a = “Hello” or ‘Hello’

✔ Each character in string can be referenced by an index number

✔ String characters can be accessed with their indices.

Print (var[0])
Print (var[-2])
Print (var[1:5])

© www.skillathon.co
Data types - Lists

List is the ordered sequence of items.

Declaring a list,
Items are enclosed in square brackets [ ] and separated by commas.

Example of a simple list:


>>> a = [ 1 , 2.0 , “jon”]

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.

Tuples are defined within parenthesis ( ) and separated by commas.

>>> a = (1,2,3)

Accessing tuple elements,

>>> Primes = (2, 3, 5, 7)

>>> Print Primes[0] , Primes [-2]

© www.skillathon.co
Tuples & Lists

❑ Lists are mutable, so they are slower in performance

❑ Lists can be modified, so its more powerful than tuples.

❑ Where as tuples values can be accessed but can’t be changed.


tu = (1, 5, 13, 14)

tu[2] = “38” ### this will throw error


❑ Elements cannot be removed from a
tuple.
Conversion between tuples & lists:

Lists 🡺 Tuples Tuples 🡺 Lists

Li = [1, 5, 13, 14] tu = (1, 5, 13, 14)

Tuple1 = tuple(Li) List1 = list(tu)


© www.skillathon.co
Data type : Set

❑ Unordered collection of unique items.

Defined inside braces { } and values separated by commas .


>>> a = { 1, 2 ,3, 3}
>>> print ( a )
{ 1, 2, 3}
Notes:
✔ They eliminate duplicates.
✔ Slicing has no meaning, since it’s unordered.
✔ Set operations like union , intersection etc. can be performed .

© www.skillathon.co
Data type: Dictionary

❑ Unordered collection of Key-Value pairs.


❑ Optimized for retrieving data. We must know the key to retrieve it’s value.

Defined within braces { }, each item being in a pair of key : value


>>> d = { ‘city’:’london’ , ‘country’:’uk’}

❑ 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

❑ Conditions usually uses comparisons and arithmetic expressions with variables.

❑ In python we use conditional statements like:

❑ 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

❑When we have multiple conditions , we can use 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

❑ Loop is a sequence of instructions that are continually repeated until a certain


condition is reached.

❑Blocks of code , executed numbers of times.

❑Eliminates the repeating code

❑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:

### print all 7 continents


>>> continents = [‘Africa’, ‘Antarctica’, ‘Asia’, ’Australia’, ’Europe’, ’North America’, ’South America’]
>>> for continent in continents:
… print( continent)
© www.skillathon.co
Conditional Statement : while loop

❑ It’s used when the range is not defined but a condition is specified.

❑The loop will iterate until that condition is true.

❑Syntax:
>>while condition
… #code

Example:

### this code will run until i is positive


>>>while ( i > 0):
… ###code

© www.skillathon.co
Functions

❑Portion of code set-out to do certain task.

Example :

>>>def area_of_sq (side): #defining function


… area = side*side #calculating area
… return area #returning area

.>>>area_of_sq(4) #calling function

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

❑ We can open,close or write any file in python.

open() function is used to open a file in python.


>>> open ( ‘file_path’, mode , buffering)
write() is used to write a file in python.
close() is used to close a file .

❑ We can take input directly from keyboard using raw_input() function.


>>> a = raw_input() ###takes input from keyboard and save it to “a” variable

❑ To print anything on the screen, print() function is used.


>>> print ( … )

© 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

❑ To avoid this kind of error, try blocks are used

❑ 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

You might also like