SlideShare a Scribd company logo
1 of 59
www.SunilOS.com 1
Python
www.sunilos.com
www.raystec.com
Python
Python is an
o interpreted,
o interactive,
o object-oriented,
o and high-level programming language.
It is free
Supported on multiple platforms
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 2
Application
You can develop
o Desktop application
o Web application
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 3
Famous Applications
Instagram
Google
Drop Box
Yahoo Maps
Spotify
Reddit
etc…
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 4
Say “Hello World”
print ("Hello, World!“)
Output at console will be
Hello, World!
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 5
Installation
Python interpreter is free
and available for
o Windows,
o Mac OS,
o Linux
o etc.
Python source and binary
from can be downloaded
from its official web
site www.python.org.
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 6
What it can do ?
 Core Programming
 Exception Handling
 File IO
 Multi Threading
 Database connectivity
 Socket Programming
 Email
 Create Web Server
 Develop Desktop application
 Develop Web application
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 7
Execution Mode
Python source codes are compiled into bytecode
as a .pyc file, and this bytecode can be
interpreted.
.pyc file is just like .class files in Java
There are two modes to execute programs in
Python:
o Interactive Mode
o Script Mode
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 8
Interactive Mode
c:>python
Python 3.6.0a1 (v3.6.0a1:5896da372fb0, May 17 2016, 16:07:03) [MSC v.1900 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> print("Hello")
Hello
>>>
>>> 2+2
4
>>>
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 9
Scripting Mode
In order to execute a logic path, you can store set of
python statements in a file. File will have extension .py.
It is called scripting file.
We are assuming that Hello.py is the script file that has
following statements:
o #Hello.py
print("Hello")
print(" My first program)")
print("File name is Hello.py")
C:> python Hello.py
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 10
Hello.py
Variables
 Variable stores human data like
numbers and alphabets.
 Data type will decide what
values will be stored in
variables.
 You can say data type will
define the structure of your
data.
www.SunilOS.com 11
Variables (Contd.)
 Variables are memory locations which have unique names.
 You can store values like integer, float, Boolean, characters and
strings.
 Memory is reserved by program for a variable with a unique
name in the variable scope.
 Unlike C and Java language, Python do not define data type of a
variable.
 Data type of a variable is decided by its assigned value like
JavaScript.
 Python allocates memory to a variable as per its assigned value.
 Value is assigned to a variable by assignment (=) operator.
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 12
Variables ( Contd.)
name = "Ram" #string value
age = 16 #integer value
amount = 99.99 #decimal value
print(name)
print(age)
print(amount)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 13
Multiple Assignments
a=b=c=10
a,b,c = 1,2,"SunilOS“
a = 1
b = 2
c= “SunilOS”
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 14
Standard Data Types
Numbers
String
List (Arrays)
Tuple
Dictionary
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 15
List is set of elements, it contains comma separated
elements
o list = ['a', 'b', 'c', 'd', 'e' ]
Elements can be of different types
o list = [ 'Ram',’Sharma’, 18, 10000, 6.2 ]
Elements are accessed by index number. Index started
from ‘0’
o list[1] = “BB”
o print(list[1])
o print(list[-2]) # -Ve index is calculated form right to left
List
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 16
List (Cont.)
 Sub list can be retrieved by slice [] operator
o list1 = list[2:3] #values from index# 2 to #3
o list2 = list[2:] #values from index# 2 to till end
 Merge lists
o list3 = list1+ list2 #merger list1 and list2 elements
o list4 = list1*2 #add list1 twice
 Delete list or list elements
o del list[1] #deletes 2nd element of list
o del list #deletes entire list
 Length of List
o print( len(list) )
o l = len(list)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 17
List functions
list.append(obj)
list.count(obj)
list.index(obj)
list.insert(ind,obj)
list.remove(obj)
list.reverse()
list.sort()
cmp(list1, list2 )
# returns -1,0,1
max(list)
#returns max value
min(list)
#returns min value
list(t)
# coverts tuple into list
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 18
Tuple
It is just like List
It is immutable. List elements can not be changed
Defined by comma separated elements and parenthesis
o t = (1, 2, 3, 4, 5 )
Accessing values
o print (t [0])
o print(t[1])
o print(len(t))
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 19
Tuple ( Contd.)
Sub tuple can be retrieved by slice [] operator
o t1 = t[2:3] #values from index# 2to #3
o t2 = t[2:] #values from index# 2 to till end
Merge tuples
o list3 = list1+ list2 #merge list1 and list2 elements
o list4 = list1*2 #add list 1 twice
Delete tuple
o del list #deletes entire list
Length of tuple
o print(len(t))
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 20
Tuple functions
cmp(t1, t2 ) # returns -1,0,1
max(t) #returns max value
min(t) #returns min value
tuple(t) # coverts list into tuple
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 21
List Vs Tuple
List is mutable and Tuple is immutable
Slicing is applied in List but not in Tuple
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 22
Dictionary
 It contains key value pairs
o d= {'name': 'Ram','surname':’Sharma’, 'age': 18}
 Elements are accessed by key
o n = d[‘name’];
o d[name] = “Shyam”
 Key should be a number or string
 Delete element or dictionary
o del d[‘name’]
o del d
 Get all keys and values
o kys = d.keys()
o vls = d.values()
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 23
Dictionary Functions
d.clear() #remove all elements
d.get(‘key’)# get the value
d.keys() #get list of keys
d.values() #get list of values
d.copy() #make copy of dictionary
len(d) #length of dictionary
cmp(d1,d2) #Compare two dictionaries
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 24
www.SunilOS.com 25
What’s an operator?
Operators are tokens that trigger some
computation when applied to variables and other
objects.
It can be categorized into:
o Arithmetic
o logical
o bit-level and
o Membership operators.
Operator Precedence
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 26
Arithmetic Operators
Arithmetic operators are used with numeric values to perform
common mathematical operations:
 num1 = 10
 num2 = 20
 sum=num1 + num2 #30
 diff=num1 - num2 #-10
 mult= num1 * num2 #200
 div=num1 / num2 #0.5
 pow=5**3 # 125
 rem=20%3 #2
 intdiv=22 // 7 #3
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 27
Assignment Operators
Python assignment operator is used for assigning
the value of the right operand to the left operand.
x = 5
x *= 3 #15
a,b,c=1,2,3
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 28
Comparison Operators
These operators compare the values of two operands and return
True or False based on condition. They are also called Relational
operators.
 a = 10
 b = 20
 x= a>b #false
 y= a==b #false
 z= a!=b #true
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 29
Logical operators
Logical operators in Python are used for conditional statements are
true or false. Logical operators in Python are AND, OR and NOT.
For logical operators following condition are applied.
 x=True
 y=False
 a=x and y #false
 b=x or y #true
 c= not x #false
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 30
Identity Operators
To compare the memory location of two objects,
Identity Operators are used. The two identify
operators used in Python are (is, is not).
a = 20
b = 20
x= a is b #true
y= a is not b #false
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 31
Bitwise Operators
Bitwise operators are used to perform bit
operations. All the decimal values will be
converted into binary values (sequence of bits i.e
0100, 1100, 1000, 1001 etc.).
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 32
www.SunilOS.com 33
Bitwise operators
<<
>>
&
^
Shift left
Shift right
Bitwise AND
Bitwise XOR
Bitwise OR
unary bitwise complement
|
~
These operators are less commonly used.
www.SunilOS.com 34
Unary bitwise complement
1 1 11 0 101
~
1 Byte
0 0 00 1 010
byte a = 10;
byte b = ~a;
www.SunilOS.com 35
Left Shift <<
1 1 01 0 101
<<
1 Byte
1 0 10 0 001
byte a = 10;
b = a<<2;
www.SunilOS.com 36
Right Shift >>
1 1 01 0 101
>>
1 Byte
1 0 10 1 100
byte a = 10;
b = a>>2;
www.SunilOS.com 37
And bitwise &
1 1 01 0 101
&
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a & b;
0 0 01 0 100
www.SunilOS.com 38
OR bitwise |
1 1 01 0 101
|
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a | b;
1 1 11 1 101
www.SunilOS.com 39
XOR bitwise ^
1 1 01 0 101
^
1 Byte
0 0 11 1 100
byte a = 10;
b = 20;
c = a ^ b;
1 1 10 1 001
Membership Operators
Python membership operators are used to check
the membership of value inside a data structure. If
the value is present in the data structure, then the
resulting value is true otherwise it returns false.
list=[1,2,3,4,5]
a=3
c=a in list #true
d=a not in list # false
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 40
www.SunilOS.com
Control Statements
if-else
while
for
do-while
GOTO
41
If condition
#If statement example
price = 110
money = 99
if ( money < price ):
 print("I can not buy Pizza")
 print("Feeling bad :(")
else :
 print("I can buy Pizza")
 print("Yum Yum :)")
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 42
www.SunilOS.com
While Loop
43
www.SunilOS.com
While Loop
जबतकहेजान = True
 round = 0
while जबतकहेजान :
o print(“मै बसंती नाचंगी !!!")
o round+=1
o if(round > 500 ):
 जबतकहेजान = False
44
While Loop
#While loop example
i = 0
while i<5:
 print ( ' Hello ',i)
 i += 1
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 45
www.SunilOS.com
For Loop
₹10 for 5 shots
How Much?
Okay!!
46
for shot in range(1, 5):
o print(“Shot Balloon” ,shot)
www.SunilOS.com
For Loop – Five shots
47
For Loop
#For loop example
for i in range (2,5) :
 print ( ' Hello ',i)
 print ('---')
#Loop a list /string
for c in "Hello Rays" :
 print (c)
 print ('---')

8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 48
Break & Continue
#Break:
str = "python"
for i in str:
 if i == 'o':
 break
 print(i);
#Continue:
for i in range(1,11):
 if i==5:
 continue;
 print(i);

8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 49
Date and Time
Date time module contains functions to handle
dates
You can import datetime module
o import datetime
Get current datetime
o x=datetime.datetime.now()
Get object of given date
o x=datetime.datetime(2018, 6, 1)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 50
Format date and time
Method strftime() is used to format date and time
o x=datetime.datetime.now()
o print(x) // 2020-05-19 12:51:31
Get day of week
o print(x.strftime("%A")) // Monday
Get name of month
o print(x.strftime("%B"))
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 51
Define Functions
 A function/method is a block of reusable code. It is used to
perform a single unit of work. Python provides many in build
functions like print()
 Function is defined with the keyword def followed by function
name and parentheses
o def sum(a,b):
 Input parameters are passed in parenthesis (a,b)
 First statement of function is the optional statement contains
docstring.
 code block of function starts with a colon (:) and block is
indented
 Statement ‘return’ returns an optional value
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 52
Sum two numbers
#Function definition
def sum( a,b ):
 "It sums two numbers"
 c = a + b
 return c;
print(sum(5,10))
print(sum(10,20))
d = sum(b=7,a=8)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 53
Default argument
def sum( a, b =0 ):
 "It sums two numbers"
 c = a + b
 return c;
print(sum(5,10))
print(sum(10))
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 54
Pass by reference
All parameters passed in
functions are pass by
reference
Output
o [1, 2, 3, 4, 5]
o [1, 2, 3, 4, 5, 6]
o [1, 2, 3, 4, 5, 6]
def chageList( list ):
 list.append(6);
 print (list)
 return
list = [1,2,3,4,5]
print(list)
chageList(list)
print(list)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 55
Variable arguments
def sumNum( a, *varg ):
 t = a
 for n in varg:
 t+=n
 return t;
total = sumNum(1,2,3,4,5)
print('Total', total)
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 56
Global and Local variables
Variables are defined inside a function body
have a local scope
Variables are defined outside of a function body
have a global scope.
8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 57
Disclaimer
 This is an educational presentation to enhance the skill of
computer science students.
 This presentation is available for free to computer science
students.
 Some internet images from different URLs are used in this
presentation to simplify technical examples and correlate
examples with the real world.
 We are grateful to owners of these URLs and pictures.
www.SunilOS.com 58
Thank You!
www.SunilOS.com 59
www.SunilOS.com

More Related Content

What's hot

Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )Sunil OS
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Hibernate
Hibernate Hibernate
Hibernate Sunil OS
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3Sunil OS
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Exception Handling
Exception HandlingException Handling
Exception HandlingSunil OS
 
Threads V4
Threads  V4Threads  V4
Threads V4Sunil OS
 
Resource Bundle
Resource BundleResource Bundle
Resource BundleSunil OS
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/ServletSunil OS
 

What's hot (20)

Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Collection v3
Collection v3Collection v3
Collection v3
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
Hibernate
Hibernate Hibernate
Hibernate
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
Java Basics
Java BasicsJava Basics
Java Basics
 
JDBC
JDBCJDBC
JDBC
 
C++
C++C++
C++
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Log4 J
Log4 JLog4 J
Log4 J
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Threads V4
Threads  V4Threads  V4
Threads V4
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
 
CSS
CSS CSS
CSS
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
C++ oop
C++ oopC++ oop
C++ oop
 
C Basics
C BasicsC Basics
C Basics
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 

Similar to Python Part 1

Similar to Python Part 1 (20)

Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Python programming
Python  programmingPython  programming
Python programming
 
Python basics
Python basicsPython basics
Python basics
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
See through C
See through CSee through C
See through C
 
Python basics
Python basicsPython basics
Python basics
 
Python
PythonPython
Python
 
c.ppt
c.pptc.ppt
c.ppt
 
Python Basics
Python BasicsPython Basics
Python Basics
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
C program
C programC program
C program
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Introduction of Python
Introduction of PythonIntroduction of Python
Introduction of Python
 
Introduction to phyton , important topic
Introduction to phyton , important topicIntroduction to phyton , important topic
Introduction to phyton , important topic
 

More from Sunil OS

Threads v3
Threads v3Threads v3
Threads v3Sunil OS
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3Sunil OS
 
Python Pandas
Python PandasPython Pandas
Python PandasSunil OS
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays TechnologiesSunil OS
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFCSunil OS
 

More from Sunil OS (10)

OOP v3
OOP v3OOP v3
OOP v3
 
Threads v3
Threads v3Threads v3
Threads v3
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Angular 8
Angular 8 Angular 8
Angular 8
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 

Recently uploaded

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 

Recently uploaded (20)

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 

Python Part 1

  • 2. Python Python is an o interpreted, o interactive, o object-oriented, o and high-level programming language. It is free Supported on multiple platforms 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 2
  • 3. Application You can develop o Desktop application o Web application 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 3
  • 4. Famous Applications Instagram Google Drop Box Yahoo Maps Spotify Reddit etc… 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 4
  • 5. Say “Hello World” print ("Hello, World!“) Output at console will be Hello, World! 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 5
  • 6. Installation Python interpreter is free and available for o Windows, o Mac OS, o Linux o etc. Python source and binary from can be downloaded from its official web site www.python.org. 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 6
  • 7. What it can do ?  Core Programming  Exception Handling  File IO  Multi Threading  Database connectivity  Socket Programming  Email  Create Web Server  Develop Desktop application  Develop Web application 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 7
  • 8. Execution Mode Python source codes are compiled into bytecode as a .pyc file, and this bytecode can be interpreted. .pyc file is just like .class files in Java There are two modes to execute programs in Python: o Interactive Mode o Script Mode 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 8
  • 9. Interactive Mode c:>python Python 3.6.0a1 (v3.6.0a1:5896da372fb0, May 17 2016, 16:07:03) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> print("Hello") Hello >>> >>> 2+2 4 >>> 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 9
  • 10. Scripting Mode In order to execute a logic path, you can store set of python statements in a file. File will have extension .py. It is called scripting file. We are assuming that Hello.py is the script file that has following statements: o #Hello.py print("Hello") print(" My first program)") print("File name is Hello.py") C:> python Hello.py 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 10 Hello.py
  • 11. Variables  Variable stores human data like numbers and alphabets.  Data type will decide what values will be stored in variables.  You can say data type will define the structure of your data. www.SunilOS.com 11
  • 12. Variables (Contd.)  Variables are memory locations which have unique names.  You can store values like integer, float, Boolean, characters and strings.  Memory is reserved by program for a variable with a unique name in the variable scope.  Unlike C and Java language, Python do not define data type of a variable.  Data type of a variable is decided by its assigned value like JavaScript.  Python allocates memory to a variable as per its assigned value.  Value is assigned to a variable by assignment (=) operator. 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 12
  • 13. Variables ( Contd.) name = "Ram" #string value age = 16 #integer value amount = 99.99 #decimal value print(name) print(age) print(amount) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 13
  • 14. Multiple Assignments a=b=c=10 a,b,c = 1,2,"SunilOS“ a = 1 b = 2 c= “SunilOS” 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 14
  • 15. Standard Data Types Numbers String List (Arrays) Tuple Dictionary 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 15
  • 16. List is set of elements, it contains comma separated elements o list = ['a', 'b', 'c', 'd', 'e' ] Elements can be of different types o list = [ 'Ram',’Sharma’, 18, 10000, 6.2 ] Elements are accessed by index number. Index started from ‘0’ o list[1] = “BB” o print(list[1]) o print(list[-2]) # -Ve index is calculated form right to left List 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 16
  • 17. List (Cont.)  Sub list can be retrieved by slice [] operator o list1 = list[2:3] #values from index# 2 to #3 o list2 = list[2:] #values from index# 2 to till end  Merge lists o list3 = list1+ list2 #merger list1 and list2 elements o list4 = list1*2 #add list1 twice  Delete list or list elements o del list[1] #deletes 2nd element of list o del list #deletes entire list  Length of List o print( len(list) ) o l = len(list) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 17
  • 18. List functions list.append(obj) list.count(obj) list.index(obj) list.insert(ind,obj) list.remove(obj) list.reverse() list.sort() cmp(list1, list2 ) # returns -1,0,1 max(list) #returns max value min(list) #returns min value list(t) # coverts tuple into list 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 18
  • 19. Tuple It is just like List It is immutable. List elements can not be changed Defined by comma separated elements and parenthesis o t = (1, 2, 3, 4, 5 ) Accessing values o print (t [0]) o print(t[1]) o print(len(t)) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 19
  • 20. Tuple ( Contd.) Sub tuple can be retrieved by slice [] operator o t1 = t[2:3] #values from index# 2to #3 o t2 = t[2:] #values from index# 2 to till end Merge tuples o list3 = list1+ list2 #merge list1 and list2 elements o list4 = list1*2 #add list 1 twice Delete tuple o del list #deletes entire list Length of tuple o print(len(t)) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 20
  • 21. Tuple functions cmp(t1, t2 ) # returns -1,0,1 max(t) #returns max value min(t) #returns min value tuple(t) # coverts list into tuple 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 21
  • 22. List Vs Tuple List is mutable and Tuple is immutable Slicing is applied in List but not in Tuple 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 22
  • 23. Dictionary  It contains key value pairs o d= {'name': 'Ram','surname':’Sharma’, 'age': 18}  Elements are accessed by key o n = d[‘name’]; o d[name] = “Shyam”  Key should be a number or string  Delete element or dictionary o del d[‘name’] o del d  Get all keys and values o kys = d.keys() o vls = d.values() 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 23
  • 24. Dictionary Functions d.clear() #remove all elements d.get(‘key’)# get the value d.keys() #get list of keys d.values() #get list of values d.copy() #make copy of dictionary len(d) #length of dictionary cmp(d1,d2) #Compare two dictionaries 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 24
  • 25. www.SunilOS.com 25 What’s an operator? Operators are tokens that trigger some computation when applied to variables and other objects. It can be categorized into: o Arithmetic o logical o bit-level and o Membership operators.
  • 26. Operator Precedence 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 26
  • 27. Arithmetic Operators Arithmetic operators are used with numeric values to perform common mathematical operations:  num1 = 10  num2 = 20  sum=num1 + num2 #30  diff=num1 - num2 #-10  mult= num1 * num2 #200  div=num1 / num2 #0.5  pow=5**3 # 125  rem=20%3 #2  intdiv=22 // 7 #3 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 27
  • 28. Assignment Operators Python assignment operator is used for assigning the value of the right operand to the left operand. x = 5 x *= 3 #15 a,b,c=1,2,3 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 28
  • 29. Comparison Operators These operators compare the values of two operands and return True or False based on condition. They are also called Relational operators.  a = 10  b = 20  x= a>b #false  y= a==b #false  z= a!=b #true 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 29
  • 30. Logical operators Logical operators in Python are used for conditional statements are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied.  x=True  y=False  a=x and y #false  b=x or y #true  c= not x #false 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 30
  • 31. Identity Operators To compare the memory location of two objects, Identity Operators are used. The two identify operators used in Python are (is, is not). a = 20 b = 20 x= a is b #true y= a is not b #false 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 31
  • 32. Bitwise Operators Bitwise operators are used to perform bit operations. All the decimal values will be converted into binary values (sequence of bits i.e 0100, 1100, 1000, 1001 etc.). 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 32
  • 33. www.SunilOS.com 33 Bitwise operators << >> & ^ Shift left Shift right Bitwise AND Bitwise XOR Bitwise OR unary bitwise complement | ~ These operators are less commonly used.
  • 34. www.SunilOS.com 34 Unary bitwise complement 1 1 11 0 101 ~ 1 Byte 0 0 00 1 010 byte a = 10; byte b = ~a;
  • 35. www.SunilOS.com 35 Left Shift << 1 1 01 0 101 << 1 Byte 1 0 10 0 001 byte a = 10; b = a<<2;
  • 36. www.SunilOS.com 36 Right Shift >> 1 1 01 0 101 >> 1 Byte 1 0 10 1 100 byte a = 10; b = a>>2;
  • 37. www.SunilOS.com 37 And bitwise & 1 1 01 0 101 & 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a & b; 0 0 01 0 100
  • 38. www.SunilOS.com 38 OR bitwise | 1 1 01 0 101 | 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a | b; 1 1 11 1 101
  • 39. www.SunilOS.com 39 XOR bitwise ^ 1 1 01 0 101 ^ 1 Byte 0 0 11 1 100 byte a = 10; b = 20; c = a ^ b; 1 1 10 1 001
  • 40. Membership Operators Python membership operators are used to check the membership of value inside a data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false. list=[1,2,3,4,5] a=3 c=a in list #true d=a not in list # false 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 40
  • 42. If condition #If statement example price = 110 money = 99 if ( money < price ):  print("I can not buy Pizza")  print("Feeling bad :(") else :  print("I can buy Pizza")  print("Yum Yum :)") 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 42
  • 44. www.SunilOS.com While Loop जबतकहेजान = True  round = 0 while जबतकहेजान : o print(“मै बसंती नाचंगी !!!") o round+=1 o if(round > 500 ):  जबतकहेजान = False 44
  • 45. While Loop #While loop example i = 0 while i<5:  print ( ' Hello ',i)  i += 1 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 45
  • 46. www.SunilOS.com For Loop ₹10 for 5 shots How Much? Okay!! 46
  • 47. for shot in range(1, 5): o print(“Shot Balloon” ,shot) www.SunilOS.com For Loop – Five shots 47
  • 48. For Loop #For loop example for i in range (2,5) :  print ( ' Hello ',i)  print ('---') #Loop a list /string for c in "Hello Rays" :  print (c)  print ('---')  8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 48
  • 49. Break & Continue #Break: str = "python" for i in str:  if i == 'o':  break  print(i); #Continue: for i in range(1,11):  if i==5:  continue;  print(i);  8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 49
  • 50. Date and Time Date time module contains functions to handle dates You can import datetime module o import datetime Get current datetime o x=datetime.datetime.now() Get object of given date o x=datetime.datetime(2018, 6, 1) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 50
  • 51. Format date and time Method strftime() is used to format date and time o x=datetime.datetime.now() o print(x) // 2020-05-19 12:51:31 Get day of week o print(x.strftime("%A")) // Monday Get name of month o print(x.strftime("%B")) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 51
  • 52. Define Functions  A function/method is a block of reusable code. It is used to perform a single unit of work. Python provides many in build functions like print()  Function is defined with the keyword def followed by function name and parentheses o def sum(a,b):  Input parameters are passed in parenthesis (a,b)  First statement of function is the optional statement contains docstring.  code block of function starts with a colon (:) and block is indented  Statement ‘return’ returns an optional value 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 52
  • 53. Sum two numbers #Function definition def sum( a,b ):  "It sums two numbers"  c = a + b  return c; print(sum(5,10)) print(sum(10,20)) d = sum(b=7,a=8) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 53
  • 54. Default argument def sum( a, b =0 ):  "It sums two numbers"  c = a + b  return c; print(sum(5,10)) print(sum(10)) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 54
  • 55. Pass by reference All parameters passed in functions are pass by reference Output o [1, 2, 3, 4, 5] o [1, 2, 3, 4, 5, 6] o [1, 2, 3, 4, 5, 6] def chageList( list ):  list.append(6);  print (list)  return list = [1,2,3,4,5] print(list) chageList(list) print(list) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 55
  • 56. Variable arguments def sumNum( a, *varg ):  t = a  for n in varg:  t+=n  return t; total = sumNum(1,2,3,4,5) print('Total', total) 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 56
  • 57. Global and Local variables Variables are defined inside a function body have a local scope Variables are defined outside of a function body have a global scope. 8/22/2019 Copyright (c) SunilOS ( RAYS ) Technologies 57
  • 58. Disclaimer  This is an educational presentation to enhance the skill of computer science students.  This presentation is available for free to computer science students.  Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world.  We are grateful to owners of these URLs and pictures. www.SunilOS.com 58