SlideShare a Scribd company logo
1 of 41
Python Programming
Python is a high-level, interpreted, interactive and object-oriented
scripting language.
●

●

●

Python is Interpreted: This means that it is processed at runtime
by the interpreter and you do not need to compile your program
before executing it.
Python is Interactive: This means that you can actually sit at a
Python prompt and interact with the interpreter directly to write
your programs
Python is Object-Oriented: This means that Python supports
Object-Oriented style
History of Python

Python was developed by Guido van Rossum in the late
eighties and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
C++...etc

Python is derived from many other languages, including C,
Python Features
➢ Easy-to-learn: Python has relatively few keywords, simple structure,

and a clearly defined syntax. This allows the student to pick up the
language in a relatively short period of time.

➢ Easy-to-read: Python code is much more clearly defined and visible

to the eyes.

➢ Easy-to-maintain: Python's success is that its source code is fairly

easy-to-maintain.

➢ Portable: Python can run on a wide variety of hardware platforms and

has the same interface on all platforms.

➢ Extendable: You can add low-level modules to the Python interpreter.
Python Basic Syntax
The Python language has many similarities to C and Java. However, there are
some definite differences between the languages like in syntax also.

Python Identifiers:
A Python identifier is a name used to identify a variable, function, class, module
or other object. An identifier starts with a letter A to Z or a to z or an underscore
(_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $ and % within
identifiers. Python is a case sensitive programming language. Thus, Manpower
and manpower are two different identifiers in Python.
Reserved Words:
The following list shows the reserved words in Python.
These reserved words may not be used as constant or variable or any
other identifier names. All the Python keywords contain lowercase
letters only.
and
assert
break
class
continue
def
del
elif
else
except

exec
finally
for
from
global
if
import
in
is
lambda

not
or
pass
print
raise
return
try
while
with
yield
Lines and Indentation:

In python there are no braces to indicate blocks of code for
class and function definitions or flow control. Blocks of code are
denoted by line indentation.
The number of spaces in the indentation is variable, but all
statements within the block must be indented the same amount. Both
blocks in this example are fine:
Example:
if True:
print "True"
else:
print "False"
However, the second block in this example will generate an error:
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False" <----Error
Thus, in Python all the continous lines indented with similar number of spaces would form a block.
Multi-Line Statements:
Statements in Python typically end with a new line. Python does, however, allow the use of the
line continuation character () to denote that the line should continue.
Example:
total = item_one + 
item_two + 
item_three

<----New line charactor

Statements contained within the [], {} or () brackets do not need to use the line continuation character.
Example:
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
Quotation in Python:

Python accepts single ('), double (") and triple (''' or """) quotes to denote string
literals, as long as the same type of quote starts and ends the string.The triple quotes can be used
to span the string across multiple lines

Example:
word = 'word'

<--------------- single

sentence = "This is a sentence."

<------------- double

paragraph = """This is a paragraph. It is
made up of multiple lines and sentences.""" <------------ triple
Python Variable Types
Variables are nothing but reserved memory locations to store values. This means that
when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides
what can be stored in the reserved memory. Therefore, by assigning different data types to
variables, you can store integers, decimals or characters in these variables.
Assigning Values to Variables:
Python variables do not have to be explicitly declared to reserve memory space.
The declaration happens automatically when you assign a value to a variable. The equal
sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand
to the right of the = operator is the value stored in the variable.
Example:
counter = 100
miles = 1000.0
name = "John"

# An integer assignment
# A floating point
# A string

print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles and name variables, respectively.
While running this program, this will produce the following result:
Output:
100
1000.0
John
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously.
Example:
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to
the same memory location.
You can also assign multiple objects to multiple variables. For example:
a, b, c = 1, 2, "john"

Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one
string object with the value "john" is assigned to the variable c.
Python Numbers:
Number data types store numeric values. They are immutable data types which means that changing
the value of a number data type results in a newly allocated object.
Number objects are created when you assign a value to them.
For example:
var1 = 1
var2 = 10
You can delete a single object or multiple objects by using the del statement.
For example:
del var
del var_a, var_b
Python supports four different numerical types:


int



long



float



complex

int : 10 , -786
long : 51924361L , -0x19323L
float : 0.0 , -21.9
complex : 3.14j
Python Strings:
Strings in Python are identified as a contiguous set of characters in between quotation
marks. Python allows for either pairs of single or double quotes. Subsets of strings can be
taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of
the string and working their way from -1 at the end.
The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the
repetition operator.
Example:
str = 'Hello World!'
print str
print str[0]

# Prints complete string
# Prints first character of the string
print str[2:5]

# Prints characters starting from 3rd to 5th

print str[2:]

# Prints string starting from 3rd character

print str * 2

# Prints string two times

print str + "TEST" # Prints concatenated string
Output:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python Lists:
Lists are the most versatile of Python's compound data types. A list contains items separated by
commas and enclosed within square brackets ([]).
Example:
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list

# Prints complete list

print list[0]

# Prints first element of the list

print list[1:3]

# Prints elements starting from 2nd till 3rd

print list[2:]

# Prints elements starting from 3rd element

print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
Python Tuples:
A tuple is another sequence data type that is similar to the list. A
tuple consists of a number of values separated by commas. Unlike lists,
however, tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are
enclosed in brackets ( [ ] ) and their elements and size can be changed,
while tuples are enclosed in parentheses ( ( ) ) and cannot be updated.
Example:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple

# Prints complete list

print tuple[0]

# Prints first element of the list

print tuple[1:3]

# Prints elements starting from 2nd till 3rd

print tuple[2:]

# Prints elements starting from 3rd element

print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
Output:
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
Following is invalid with tuple, because we attempted to update a tuple, which is
not allowed. Similar case is possible with lists:
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000

# Valid syntax with list
Python Dictionary:
Python's dictionaries are kind of hash table type. They work like associative arrays
or hashes found in Perl and consist of key-value pairs. A dictionary key are usually
numbers or strings.
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and
accessed using square braces ( [] ). For example:
dict = {}
dict['one'] = "This is one"
dict[2]

= "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one']

# Prints value for 'one' key

print dict[2]

# Prints value for 2 key

print tinydict

# Prints complete dictionary

print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
Output:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Operators
Simple answer can be given using expression 4 + 5 is equal to 9. Here, 4 and 5 are
called operands and + is called operator. Python language supports the following types
of operators.
➢ Arithmetic Operators
➢ Comparison (i.e., Relational) Operators
➢ Assignment Operators
➢ Logical Operators
➢ Bitwise Operators
➢ Membership Operators
➢ Identity Operators
Python Arithmetic Operator
a = 21
b = 10
C=0
c=a+b
print "Line 1 - Value of c is ", c
c=a-b
print "Line 2 - Value of c is ", c
c=a*b
print "Line 3 - Value of c is ", c
c=a/b
print "Line 4 - Value of c is ", c
c=a%b
print "Line 5 - Value of c is ", c
a=2
b=3
c = a**b
print "Line 6 - Value of c is ", c
a = 10
b=5
c = a//b
print "Line 7 - Value of c is ", c
Output:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
Python Comparison Operator
Python Assignment Operator
Bitwise Operator
Logical Operator
Membership Operator
Example:
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "Line 1 - a is available in the given list"
else:
print "Line 1 - a is not available in the given list"
if ( b not in list ):
print "Line 2 - b is not available in the given list"
else:
print "Line 2 - b is available in the given list"
a=2
if ( a in list ):
print "Line 3 - a is available in the given list"
else:
print "Line 3 - a is not available in the given list"
Output:
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
Line 3 - a is available in the given list
Identity Operators
a = 20
b = 20
if ( a is b ):
print "Line 1 - a and b have same identity"
else:
print "Line 1 - a and b do not have same identity"
b = 30
if ( a is b ):
print "Line 3 - a and b have same identity"
else:
print "Line 3 - a and b do not have same identity"
if ( a is not b ):
print "Line 4 - a and b do not have same identity"
else:
print "Line 4 - a and b have same identity"
Output:
Line 1 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity
Python slide.1
Python slide.1

More Related Content

What's hot

Python - Introdução Básica
Python - Introdução BásicaPython - Introdução Básica
Python - Introdução Básica
Christian Perone
 

What's hot (20)

Python
PythonPython
Python
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Input and Output
Input and OutputInput and Output
Input and Output
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Python list
Python listPython list
Python list
 
Python - Introdução Básica
Python - Introdução BásicaPython - Introdução Básica
Python - Introdução Básica
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
python into.pptx
python into.pptxpython into.pptx
python into.pptx
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
Python - the basics
Python - the basicsPython - the basics
Python - the basics
 

Viewers also liked

Pp65tahun2005
Pp65tahun2005Pp65tahun2005
Pp65tahun2005
frans2014
 
Analisa ecotects
Analisa ecotectsAnalisa ecotects
Analisa ecotects
frans2014
 
Tugassejaraharsitektur 131112083046-phpapp01
Tugassejaraharsitektur 131112083046-phpapp01Tugassejaraharsitektur 131112083046-phpapp01
Tugassejaraharsitektur 131112083046-phpapp01
frans2014
 
Perencanaan sambungan-profil-baja
Perencanaan sambungan-profil-bajaPerencanaan sambungan-profil-baja
Perencanaan sambungan-profil-baja
frans2014
 
P.o.m project report
P.o.m project reportP.o.m project report
P.o.m project report
mounikapadiri
 
Ppipp copy-130823044640-phpapp02
Ppipp copy-130823044640-phpapp02Ppipp copy-130823044640-phpapp02
Ppipp copy-130823044640-phpapp02
frans2014
 
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
інна гаврилець
 
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
frans2014
 

Viewers also liked (18)

πετρινα γεφυρια (4)
πετρινα γεφυρια (4)πετρινα γεφυρια (4)
πετρινα γεφυρια (4)
 
Pp65tahun2005
Pp65tahun2005Pp65tahun2005
Pp65tahun2005
 
Analisa ecotects
Analisa ecotectsAnalisa ecotects
Analisa ecotects
 
Tugassejaraharsitektur 131112083046-phpapp01
Tugassejaraharsitektur 131112083046-phpapp01Tugassejaraharsitektur 131112083046-phpapp01
Tugassejaraharsitektur 131112083046-phpapp01
 
Python session3
Python session3Python session3
Python session3
 
Html
HtmlHtml
Html
 
Perencanaan sambungan-profil-baja
Perencanaan sambungan-profil-bajaPerencanaan sambungan-profil-baja
Perencanaan sambungan-profil-baja
 
P.o.m project report
P.o.m project reportP.o.m project report
P.o.m project report
 
Emcdevteam com topbostoncriminallawyer_practice_theft-crimes
Emcdevteam com topbostoncriminallawyer_practice_theft-crimesEmcdevteam com topbostoncriminallawyer_practice_theft-crimes
Emcdevteam com topbostoncriminallawyer_practice_theft-crimes
 
Ppipp copy-130823044640-phpapp02
Ppipp copy-130823044640-phpapp02Ppipp copy-130823044640-phpapp02
Ppipp copy-130823044640-phpapp02
 
Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...
Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...
Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...
 
ნარჩენების სრულად დაშლის დროNew microsoft office power point presentation
ნარჩენების სრულად დაშლის დროNew microsoft office power point presentationნარჩენების სრულად დაშლის დროNew microsoft office power point presentation
ნარჩენების სრულად დაშლის დროNew microsoft office power point presentation
 
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
 
μυστράς στοιχεία αρχιτεκτονικής
μυστράς στοιχεία αρχιτεκτονικήςμυστράς στοιχεία αρχιτεκτονικής
μυστράς στοιχεία αρχιτεκτονικής
 
δ.θ
δ.θδ.θ
δ.θ
 
Tradie Exchange Jobs Australia
Tradie Exchange Jobs AustraliaTradie Exchange Jobs Australia
Tradie Exchange Jobs Australia
 
μυστράς (2)
μυστράς (2)μυστράς (2)
μυστράς (2)
 
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
 

Similar to Python slide.1

CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
faithxdunce63732
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
shailaja30
 

Similar to Python slide.1 (20)

CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
 
1. python programming
1. python programming1. python programming
1. python programming
 
Python basics
Python basicsPython basics
Python basics
 
Python quick guide
Python quick guidePython quick guide
Python quick guide
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
 
Python interview questions
Python interview questionsPython interview questions
Python interview questions
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
Python standard data types
Python standard data typesPython standard data types
Python standard data types
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Python PPT2
Python PPT2Python PPT2
Python PPT2
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 

More from Aswin Krishnamoorthy

More from Aswin Krishnamoorthy (6)

Http request&response
Http request&responseHttp request&response
Http request&response
 
Ppt final-technology
Ppt final-technologyPpt final-technology
Ppt final-technology
 
Windows 2012
Windows 2012Windows 2012
Windows 2012
 
Virtualization session3
Virtualization session3Virtualization session3
Virtualization session3
 
Virtualization session3 vm installation
Virtualization session3 vm installationVirtualization session3 vm installation
Virtualization session3 vm installation
 
Python session3
Python session3Python session3
Python session3
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Python slide.1

  • 1.
  • 2. Python Programming Python is a high-level, interpreted, interactive and object-oriented scripting language. ● ● ● Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. Python is Interactive: This means that you can actually sit at a Python prompt and interact with the interpreter directly to write your programs Python is Object-Oriented: This means that Python supports Object-Oriented style
  • 3. History of Python Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. C++...etc Python is derived from many other languages, including C,
  • 4. Python Features ➢ Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language in a relatively short period of time. ➢ Easy-to-read: Python code is much more clearly defined and visible to the eyes. ➢ Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain. ➢ Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. ➢ Extendable: You can add low-level modules to the Python interpreter.
  • 5. Python Basic Syntax The Python language has many similarities to C and Java. However, there are some definite differences between the languages like in syntax also. Python Identifiers: A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $ and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.
  • 6. Reserved Words: The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.
  • 8. Lines and Indentation: In python there are no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Both blocks in this example are fine:
  • 9. Example: if True: print "True" else: print "False" However, the second block in this example will generate an error: if True: print "Answer" print "True" else: print "Answer" print "False" <----Error Thus, in Python all the continous lines indented with similar number of spaces would form a block.
  • 10. Multi-Line Statements: Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character () to denote that the line should continue. Example: total = item_one + item_two + item_three <----New line charactor Statements contained within the [], {} or () brackets do not need to use the line continuation character. Example: days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
  • 11. Quotation in Python: Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as long as the same type of quote starts and ends the string.The triple quotes can be used to span the string across multiple lines Example: word = 'word' <--------------- single sentence = "This is a sentence." <------------- double paragraph = """This is a paragraph. It is made up of multiple lines and sentences.""" <------------ triple
  • 12. Python Variable Types Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. Assigning Values to Variables: Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.
  • 13. Example: counter = 100 miles = 1000.0 name = "John" # An integer assignment # A floating point # A string print counter print miles print name Here, 100, 1000.0 and "John" are the values assigned to counter, miles and name variables, respectively. While running this program, this will produce the following result: Output: 100 1000.0 John
  • 14. Multiple Assignment: Python allows you to assign a single value to several variables simultaneously. Example: a=b=c=1 Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example: a, b, c = 1, 2, "john" Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c.
  • 15. Python Numbers: Number data types store numeric values. They are immutable data types which means that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example: var1 = 1 var2 = 10 You can delete a single object or multiple objects by using the del statement. For example: del var del var_a, var_b
  • 16. Python supports four different numerical types:  int  long  float  complex int : 10 , -786 long : 51924361L , -0x19323L float : 0.0 , -21.9 complex : 3.14j
  • 17. Python Strings: Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. Example: str = 'Hello World!' print str print str[0] # Prints complete string # Prints first character of the string
  • 18. print str[2:5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "TEST" # Prints concatenated string Output: Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST
  • 19. Python Lists: Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). Example: list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print list # Prints complete list print list[0] # Prints first element of the list print list[1:3] # Prints elements starting from 2nd till 3rd print list[2:] # Prints elements starting from 3rd element print tinylist * 2 # Prints list two times print list + tinylist # Prints concatenated lists
  • 20. Python Tuples: A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Example: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john')
  • 21. print tuple # Prints complete list print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd element print tinytuple * 2 # Prints list two times print tuple + tinytuple # Prints concatenated lists Output: ('abcd', 786, 2.23, 'john', 70.200000000000003) abcd (786, 2.23)
  • 22. (2.23, 'john', 70.200000000000003) (123, 'john', 123, 'john') ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john') Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is possible with lists: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
  • 23. Python Dictionary: Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key are usually numbers or strings. Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ( [] ). For example: dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
  • 24. print dict['one'] # Prints value for 'one' key print dict[2] # Prints value for 2 key print tinydict # Prints complete dictionary print tinydict.keys() # Prints all the keys print tinydict.values() # Prints all the values Output: This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
  • 25. Operators Simple answer can be given using expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + is called operator. Python language supports the following types of operators. ➢ Arithmetic Operators ➢ Comparison (i.e., Relational) Operators ➢ Assignment Operators ➢ Logical Operators ➢ Bitwise Operators ➢ Membership Operators ➢ Identity Operators
  • 26. Python Arithmetic Operator a = 21 b = 10 C=0 c=a+b print "Line 1 - Value of c is ", c c=a-b print "Line 2 - Value of c is ", c c=a*b print "Line 3 - Value of c is ", c
  • 27. c=a/b print "Line 4 - Value of c is ", c c=a%b print "Line 5 - Value of c is ", c a=2 b=3 c = a**b print "Line 6 - Value of c is ", c a = 10 b=5 c = a//b print "Line 7 - Value of c is ", c
  • 28. Output: Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 8 Line 7 - Value of c is 2
  • 34. Example: a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "Line 1 - a is available in the given list" else: print "Line 1 - a is not available in the given list" if ( b not in list ): print "Line 2 - b is not available in the given list" else: print "Line 2 - b is available in the given list"
  • 35. a=2 if ( a in list ): print "Line 3 - a is available in the given list" else: print "Line 3 - a is not available in the given list" Output: Line 1 - a is not available in the given list Line 2 - b is not available in the given list Line 3 - a is available in the given list
  • 37. a = 20 b = 20 if ( a is b ): print "Line 1 - a and b have same identity" else: print "Line 1 - a and b do not have same identity"
  • 38. b = 30 if ( a is b ): print "Line 3 - a and b have same identity" else: print "Line 3 - a and b do not have same identity"
  • 39. if ( a is not b ): print "Line 4 - a and b do not have same identity" else: print "Line 4 - a and b have same identity" Output: Line 1 - a and b have same identity Line 3 - a and b do not have same identity Line 4 - a and b do not have same identity