SlideShare a Scribd company logo
P.INDURANI
ASSISTANT PROFESSOR & HEAD
DEPARTMENT OF COMPUTER SCIENCE
PARVATHY’S ARTS AND SCIENCE COLLEGE
DINDIGUL
11
PYTHON
PROGRAMMING
UNIT I
Introduction – Interactive programming
language.
IDLE – Integrated Development and Learning
Environment.
• >>> - To indicate waiting for a user command.
• To be printed text is enclosed between
apostrophe marks.
eg: >>> print(‘ Hello World’)
2
Example
3
Operators
Precedence of arithmetic operators
• () Parentheses
• ** Exponentiation
• - Negation
• / division // integer division * multiplication %
modulus
• + addition – subtraction
4
Error Message
>>> 10+4(3+3)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
10+4(3+3)
TypeError: 'int' object is not callable
>>> 10/0
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
10/0
ZeroDivisionError: integer division or modulo by zero
>>> 5
Python String
Enclosed within single, double or triple quotes.
6
Relational Operators
• To comparing expression values.
• Yield values: true, false
• ASCII values are used for string comparison
• Does not allow string values to be compared with
numeric values.
Operators are: ==, <, >, <=, >=, !=
7
Operators
Logical Operators
• Combining expressions
• Yield values: true, false
• Involving arithmetic, relational and logical
operators.
• Evaluation using precedence rules.
– Logical: not, and, or
– Operators: arithmetic, relational, logical
Bitwise Operators – Operate bit by bit
8
Variables and Assignment
Statements
• Variables are often called names.
• Binds a variable to an object
• Syntax: variable = expression
Rules:
• Must begin with letter or _ underscore
• Contain any number of letters, digits or underscore.
• Python is case sensitive
• More than one variable may refer to the same
object.
• Shorthand Notation: >> a=a+5
9
Statements
Keywords
• Have special meaning.
• Cannot be used for naming objects.
• Eg: continue, for, while, del, true, def, except,
if, return, and, else, nonlocal, etc.,
Script Mode
• Instruction are written in a file.
• Should have extension .py or .pyw
10
Example
11
Functions
To solve a problem divide it into simpler sub programs.
Built-In Functions
• Input – To get user input.
• Eval – evaluating a string
• Composition – Inner function serves as input argument to
outer function.
• Print – printing multiple values in a single call
• Type – determining data type
• Round – rounding to nearest values
• Min and max – Operands must be compatible for comparison
• Pow – Computing power
• Math – import a module before using it.
12
Function Definition and call
• Comments enhance readability of the code
• Single line comments start with #
• Syntax: def function_name(list_of_parameters):
Eg: def add(a):
• Run - Click run module
• Invoking the function main in the script
Eg: if_name_==‘_main_’:
main()
• The definition of function main
• An if statement
13
Example
14
• for loop: Repeats a set of statements over a group of values.
– Syntax:
for variableName in groupOfValues:
statements
• We indent the statements to be repeated with tabs or spaces.
• variableName gives a name to each value, so you can refer to it in
the statements.
• groupOfValues can be a range of integers, specified with the range
function.
Eg: for x in range(1, 6):
print x, "squared is", x * x
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Control Structures
15
The range function specifies a range of integers:
• range(start, stop) - the integers between start (inclusive)
and stop (exclusive)
– It can also accept a third value specifying the change between values.
• range(start, stop, step) - the integers between start (inclusive)
and stop (exclusive) by step
– Example:
for x in range(5, 0, -1):
print x
print "Blastoff!"
Output:
5
4
3
2
1
Blastoff!
16
For loop
• Some loops incrementally compute a value that is
initialized outside the loop. This is sometimes called
a cumulative sum.
sum = 0
for i in range(1, 11):
sum = sum + (i * i)
print "sum of first 10 squares is", sum
Output:
sum of first 10 squares is 385
17
Cumulative loop
• if statement: Executes a group of statements only if a
certain condition is true. Otherwise, the statements
are skipped.
– Syntax:
if condition:
statements
• Example:
gpa = 3.4
if gpa > 2.0:
print "Your application is accepted."
18
If statement
• if/else statement: Executes one block of statements if a certain condition is True, and a
second block of statements if it is False.
– Syntax:
if condition:
statements
else:
statements
• Example:
gpa = 1.4
if gpa > 2.0:
print "Welcome to Mars University!"
else:
print "Your application is denied."
• Multiple conditions can be chained with elif ("else if"):
if condition:
statements
elif condition:
statements
else:
statements
19
If…else… statement
• while loop: Executes a group of statements as long as a condition is
True.
– good for indefinite loops (repeat an unknown number of times)
• Syntax:
while condition:
statements
• Example:
number = 1
while number < 200:
print number,
number = number * 2
– Output:
1 2 4 8 16 32 64 128
20
While

More Related Content

What's hot

03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Python advance
Python advancePython advance
Python advance
Deepak Chandella
 
4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Python modules
Python modulesPython modules
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
John(Qiang) Zhang
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
Faisal Shahzad Khan
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
Vishalini Mugunen
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
John(Qiang) Zhang
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Ch7 C++
Ch7 C++Ch7 C++
Function overloading
Function overloadingFunction overloading
Function overloading
Sudeshna Biswas
 

What's hot (20)

03 function overloading
03 function overloading03 function overloading
03 function overloading
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
This pointer
This pointerThis pointer
This pointer
 
Python advance
Python advancePython advance
Python advance
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
Python modules
Python modulesPython modules
Python modules
 
Python advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structuresPython advanced 3.the python std lib by example –data structures
Python advanced 3.the python std lib by example –data structures
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Function C++
Function C++ Function C++
Function C++
 
Python libraries
Python librariesPython libraries
Python libraries
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor Python advanced 1.handle error, generator, decorator and decriptor
Python advanced 1.handle error, generator, decorator and decriptor
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar to PYTHON PROGRAMMING

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
MuskanSony
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Python overview
Python   overviewPython   overview
Python overview
Hemant Kumar Tiwary
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
Helpmeinhomework
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
lailoesakhan
 
Python basics
Python basicsPython basics
Python basics
Hoang Nguyen
 
Python basics
Python basicsPython basics
Python basics
Young Alista
 
Python basics
Python basicsPython basics
Python basics
Fraboni Ec
 
Python basics
Python basicsPython basics
Python basics
Harry Potter
 
Python basics
Python basicsPython basics
Python basics
James Wong
 
Python basics
Python basicsPython basics
Python basics
Tony Nguyen
 
Python basics
Python basicsPython basics
Python basics
Luis Goldster
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
Sudharsan S
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
Learning c++
Learning c++Learning c++
Learning c++
Bala Lavanya
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
sheibansari
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 

Similar to PYTHON PROGRAMMING (20)

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Python overview
Python   overviewPython   overview
Python overview
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Learning c++
Learning c++Learning c++
Learning c++
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 

Recently uploaded

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Leena Ghag-Sakpal
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 

Recently uploaded (20)

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
Bed Making ( Introduction, Purpose, Types, Articles, Scientific principles, N...
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 

PYTHON PROGRAMMING

  • 1. P.INDURANI ASSISTANT PROFESSOR & HEAD DEPARTMENT OF COMPUTER SCIENCE PARVATHY’S ARTS AND SCIENCE COLLEGE DINDIGUL 11 PYTHON PROGRAMMING
  • 2. UNIT I Introduction – Interactive programming language. IDLE – Integrated Development and Learning Environment. • >>> - To indicate waiting for a user command. • To be printed text is enclosed between apostrophe marks. eg: >>> print(‘ Hello World’) 2
  • 4. Operators Precedence of arithmetic operators • () Parentheses • ** Exponentiation • - Negation • / division // integer division * multiplication % modulus • + addition – subtraction 4
  • 5. Error Message >>> 10+4(3+3) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> 10+4(3+3) TypeError: 'int' object is not callable >>> 10/0 Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> 10/0 ZeroDivisionError: integer division or modulo by zero >>> 5
  • 6. Python String Enclosed within single, double or triple quotes. 6
  • 7. Relational Operators • To comparing expression values. • Yield values: true, false • ASCII values are used for string comparison • Does not allow string values to be compared with numeric values. Operators are: ==, <, >, <=, >=, != 7
  • 8. Operators Logical Operators • Combining expressions • Yield values: true, false • Involving arithmetic, relational and logical operators. • Evaluation using precedence rules. – Logical: not, and, or – Operators: arithmetic, relational, logical Bitwise Operators – Operate bit by bit 8
  • 9. Variables and Assignment Statements • Variables are often called names. • Binds a variable to an object • Syntax: variable = expression Rules: • Must begin with letter or _ underscore • Contain any number of letters, digits or underscore. • Python is case sensitive • More than one variable may refer to the same object. • Shorthand Notation: >> a=a+5 9
  • 10. Statements Keywords • Have special meaning. • Cannot be used for naming objects. • Eg: continue, for, while, del, true, def, except, if, return, and, else, nonlocal, etc., Script Mode • Instruction are written in a file. • Should have extension .py or .pyw 10
  • 12. Functions To solve a problem divide it into simpler sub programs. Built-In Functions • Input – To get user input. • Eval – evaluating a string • Composition – Inner function serves as input argument to outer function. • Print – printing multiple values in a single call • Type – determining data type • Round – rounding to nearest values • Min and max – Operands must be compatible for comparison • Pow – Computing power • Math – import a module before using it. 12
  • 13. Function Definition and call • Comments enhance readability of the code • Single line comments start with # • Syntax: def function_name(list_of_parameters): Eg: def add(a): • Run - Click run module • Invoking the function main in the script Eg: if_name_==‘_main_’: main() • The definition of function main • An if statement 13
  • 15. • for loop: Repeats a set of statements over a group of values. – Syntax: for variableName in groupOfValues: statements • We indent the statements to be repeated with tabs or spaces. • variableName gives a name to each value, so you can refer to it in the statements. • groupOfValues can be a range of integers, specified with the range function. Eg: for x in range(1, 6): print x, "squared is", x * x Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25 Control Structures 15
  • 16. The range function specifies a range of integers: • range(start, stop) - the integers between start (inclusive) and stop (exclusive) – It can also accept a third value specifying the change between values. • range(start, stop, step) - the integers between start (inclusive) and stop (exclusive) by step – Example: for x in range(5, 0, -1): print x print "Blastoff!" Output: 5 4 3 2 1 Blastoff! 16 For loop
  • 17. • Some loops incrementally compute a value that is initialized outside the loop. This is sometimes called a cumulative sum. sum = 0 for i in range(1, 11): sum = sum + (i * i) print "sum of first 10 squares is", sum Output: sum of first 10 squares is 385 17 Cumulative loop
  • 18. • if statement: Executes a group of statements only if a certain condition is true. Otherwise, the statements are skipped. – Syntax: if condition: statements • Example: gpa = 3.4 if gpa > 2.0: print "Your application is accepted." 18 If statement
  • 19. • if/else statement: Executes one block of statements if a certain condition is True, and a second block of statements if it is False. – Syntax: if condition: statements else: statements • Example: gpa = 1.4 if gpa > 2.0: print "Welcome to Mars University!" else: print "Your application is denied." • Multiple conditions can be chained with elif ("else if"): if condition: statements elif condition: statements else: statements 19 If…else… statement
  • 20. • while loop: Executes a group of statements as long as a condition is True. – good for indefinite loops (repeat an unknown number of times) • Syntax: while condition: statements • Example: number = 1 while number < 200: print number, number = number * 2 – Output: 1 2 4 8 16 32 64 128 20 While