SlideShare a Scribd company logo
Python slide.1
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 (20)

PPTX
JAVA AWT
shanmuga rajan
 
PDF
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
PPTX
Python basics
Hoang Nguyen
 
PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PPTX
Generators In Python
Simplilearn
 
PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PDF
Overview of python 2019
Samir Mohanty
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPT
Java operators
Shehrevar Davierwala
 
PDF
Problem solving using computers - Chapter 1
To Sum It Up
 
PDF
Constants, Variables and Data Types in Java
Abhilash Nair
 
PDF
Variables & Data Types In Python | Edureka
Edureka!
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
PPTX
Array in c#
Prem Kumar Badri
 
PDF
Python Collections Tutorial | Edureka
Edureka!
 
PPTX
Operators in Python
Anusuya123
 
PDF
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
PDF
Introduction to python programming
Srinivas Narasegouda
 
JAVA AWT
shanmuga rajan
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Python basics
Hoang Nguyen
 
Python basics
RANAALIMAJEEDRAJPUT
 
Generators In Python
Simplilearn
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Overview of python 2019
Samir Mohanty
 
Python libraries
Prof. Dr. K. Adisesha
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java operators
Shehrevar Davierwala
 
Problem solving using computers - Chapter 1
To Sum It Up
 
Constants, Variables and Data Types in Java
Abhilash Nair
 
Variables & Data Types In Python | Edureka
Edureka!
 
Python functions
Prof. Dr. K. Adisesha
 
Array in c#
Prem Kumar Badri
 
Python Collections Tutorial | Edureka
Edureka!
 
Operators in Python
Anusuya123
 
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Introduction to python programming
Srinivas Narasegouda
 

Viewers also liked (18)

PPTX
πετρινα γεφυρια (4)
Theod13
 
PDF
Pp65tahun2005
frans2014
 
PDF
Analisa ecotects
frans2014
 
PPTX
Tugassejaraharsitektur 131112083046-phpapp01
frans2014
 
PPTX
Python session3
Aswin Krishnamoorthy
 
PDF
Perencanaan sambungan-profil-baja
frans2014
 
DOCX
P.o.m project report
mounikapadiri
 
PDF
Emcdevteam com topbostoncriminallawyer_practice_theft-crimes
Laurena Campos
 
PPTX
Ppipp copy-130823044640-phpapp02
frans2014
 
PDF
Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...
Clipping Path India
 
PPTX
ნარჩენების სრულად დაშლის დროNew microsoft office power point presentation
Nato Khuroshvili
 
PDF
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
інна гаврилець
 
PPTX
μυστράς στοιχεία αρχιτεκτονικής
Theod13
 
PPTX
δ.θ
Theod13
 
PDF
Tradie Exchange Jobs Australia
TradieExchange
 
PPTX
μυστράς (2)
Theod13
 
PPT
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
frans2014
 
πετρινα γεφυρια (4)
Theod13
 
Pp65tahun2005
frans2014
 
Analisa ecotects
frans2014
 
Tugassejaraharsitektur 131112083046-phpapp01
frans2014
 
Python session3
Aswin Krishnamoorthy
 
Perencanaan sambungan-profil-baja
frans2014
 
P.o.m project report
mounikapadiri
 
Emcdevteam com topbostoncriminallawyer_practice_theft-crimes
Laurena Campos
 
Ppipp copy-130823044640-phpapp02
frans2014
 
Photoshop Tutorial Clipping path service, Photoshop clipping path. photo clip...
Clipping Path India
 
ნარჩენების სრულად დაშლის დროNew microsoft office power point presentation
Nato Khuroshvili
 
[S hegda a.v.]_ekonomіka_pіdpriєmstva._zbіrnik_(book_fi.org)
інна гаврилець
 
μυστράς στοιχεία αρχιτεκτονικής
Theod13
 
δ.θ
Theod13
 
Tradie Exchange Jobs Australia
TradieExchange
 
μυστράς (2)
Theod13
 
S1 teoper-2-konsepdasarperencanaanpembangunan-130614084228-phpapp02
frans2014
 
Ad

Similar to Python slide.1 (20)

DOCX
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
faithxdunce63732
 
PPTX
1. python programming
sreeLekha51
 
PPTX
introduction to python programming concepts
GautamDharamrajChouh
 
PPTX
Python basics
Manisha Gholve
 
PDF
Python quick guide
Hasan Bisri
 
PPSX
Programming with Python
Rasan Samarasinghe
 
PPTX
Python 3.pptx
HarishParthasarathy4
 
PPTX
Introduction on basic python and it's application
sriram2110
 
PPTX
1-Introduction to Python, features of python, history of python(1).pptx
MAHESWARIS55
 
PPTX
Python ppt
Anush verma
 
DOCX
unit 1.docx
ssuser2e84e4
 
PPT
python1.ppt
arivukarasi2
 
PDF
CS-XII Python Fundamentals.pdf
Ida Lumintu
 
PPTX
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
PDF
Python interview questions
Pragati Singh
 
PPTX
Introduction to learn and Python Interpreter
Alamelu
 
PDF
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
PDF
Python standard data types
Learnbay Datascience
 
PPTX
Learn about Python power point presentation
omsumukh85
 
PPTX
Basic data types in python
sunilchute1
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
faithxdunce63732
 
1. python programming
sreeLekha51
 
introduction to python programming concepts
GautamDharamrajChouh
 
Python basics
Manisha Gholve
 
Python quick guide
Hasan Bisri
 
Programming with Python
Rasan Samarasinghe
 
Python 3.pptx
HarishParthasarathy4
 
Introduction on basic python and it's application
sriram2110
 
1-Introduction to Python, features of python, history of python(1).pptx
MAHESWARIS55
 
Python ppt
Anush verma
 
unit 1.docx
ssuser2e84e4
 
python1.ppt
arivukarasi2
 
CS-XII Python Fundamentals.pdf
Ida Lumintu
 
DATA TYPES IN PYTHON jesjdjdjkdkkdk.pptx
rajvishnuf9
 
Python interview questions
Pragati Singh
 
Introduction to learn and Python Interpreter
Alamelu
 
23UCACC11 Python Programming (MTNC) (BCA)
ssuser7f90ae
 
Python standard data types
Learnbay Datascience
 
Learn about Python power point presentation
omsumukh85
 
Basic data types in python
sunilchute1
 
Ad

More from Aswin Krishnamoorthy (6)

PPT
Http request&response
Aswin Krishnamoorthy
 
PPT
Ppt final-technology
Aswin Krishnamoorthy
 
PPT
Windows 2012
Aswin Krishnamoorthy
 
PPT
Virtualization session3
Aswin Krishnamoorthy
 
PPT
Virtualization session3 vm installation
Aswin Krishnamoorthy
 
PPT
Python session3
Aswin Krishnamoorthy
 
Http request&response
Aswin Krishnamoorthy
 
Ppt final-technology
Aswin Krishnamoorthy
 
Windows 2012
Aswin Krishnamoorthy
 
Virtualization session3
Aswin Krishnamoorthy
 
Virtualization session3 vm installation
Aswin Krishnamoorthy
 
Python session3
Aswin Krishnamoorthy
 

Recently uploaded (20)

PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Controller Request and Response in Odoo18
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
infertility, types,causes, impact, and management
Ritu480198
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 

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