PYTHON PROGRAMMING
T.Y.BSC(CS) SEM-V
[COURSE CODE VS 3510: Credits-2]
By- Prof. Sangeeta M. Borde (Computer Science Department)
SUBJECT SYLLABUS
Chapter 1: An Introduction to Python
▪ Introduction to Python
▪ The Python Programming Language
▪ History of the Language, features Applications
▪ Installing Python, Running Simple Python program Basics of Python Standard data types
- basic, none, Boolean (true & False), numbers, Variables, Constants
▪ Python identifiers and reserved words,
▪ Lines and indentation,
▪ multi-line statements and Comments,
▪ Input/output with print and input ,functions Declaration,
▪ Operations on Data such as assignment, arithmetic, relational, logical and bitwise
operations, dry run, Simple Input and output etc.
SUBJECT SYLLABUS Continue…
Chapter 2: Control Statements
Sequence Control – Precedence of operators
Type conversion Conditional Statements: if, if-else, nested if-else, Looping- for, while,
nested loops, loop control statements (break, continue, pass).
Strings: declaration, manipulation, special operations, escape character, string formatting
operator, Raw String, Unicode strings, Built-in String methods.
Chapter 3 Lists, functions, tuples and dictionaries, Sets
Python Lists: Concept, creating and accessing elements, updating & deleting lists, traversing a List,
reverse Built-in List Operators,
Concatenation, Repetition, In Operator, Built-in List functions and methods. Functions: Definitions and
Uses, Function Calls, Type Conversion Functions, Math Functions, Composition, Adding New Functions,
Flow of Execution, Parameters and Arguments, Variables and Parameters, Stack Diagrams,
Void Functions, Anonymous functions Importing with from, Return Values, Boolean Functions,
More Recursion, Functional programming tools - filter(), map(), and reduce(),recursion, lambda forms.
SUBJECT SYLLABUS Continue…
Chapter 3:
Lists, functions, tuples and dictionaries, Sets
Continue…
Tuples and Dictionaries: Tuples, Accessing values in Tuples, Tuple Assignment,
Tuples as return values, Variable-length argument tuples, and Basic tuples
operations, Concatenation, Repetition, in Operator, Iteration, Built-in tuple functions,
indexing, slicing and matrices.
Creating a Dictionary, Accessing Values in a dictionary, Updating Dictionary,
Deleting Elements from Dictionary, Properties of Dictionary keys, Operations in
Dictionary, Built-In Dictionary Functions, Built-in Dictionary Methods.
Sets- Definition, transaction of set(Adding, Union, intersection), working with sets
SUBJECT SYLLABUS Continue…
▪ Chapter 4 Modules ,Working with files, Exception
handling
▪ Modules: Importing module, Creating & exploring modules, Math module,
Random module, Time module Packages: Importing package, creating package,
examples
▪ Working with files: Creating files and Operations on files (open, close, read,
write), File object attributes, file positions, Listing Files in a Directory, Testing File
Types, Removing files and directories, copying and renaming files, splitting
pathnames, creating and moving directories
▪ Regular Expression: Concept of regular expression, various types of regular
expressions, using match function.
▪ Exception Handling: Built-in Exceptions, Handling Exceptions, Exception with
Arguments, User-defined Exceptions.
SUBJECT SYLLABUS Continue…
▪ Chapter 4 Modules ,Working with files, Exception
handling
▪ Modules: Importing module, Creating & exploring modules, Math module,
Random module, Time module Packages: Importing package, creating package,
examples
▪ Working with files: Creating files and Operations on files (open, close, read,
write), File object attributes, file positions, Listing Files in a Directory, Testing File
Types, Removing files and directories, copying and renaming files, splitting
pathnames, creating and moving directories
▪ Regular Expression: Concept of regular expression, various types of regular
expressions, using match function.
▪ Exception Handling: Built-in Exceptions, Handling Exceptions, Exception with
Arguments, User-defined Exceptions.
SUBJECT SYLLABUS Continue…
Programming Assignments
▪ Assignment 1 - Python Basics
▪ Assignment 2 – Arrays, Strings, and Functions Assignment 3 - List, Tuples, Sets, and Dictionary
▪ Assignment 4 - File Handling and Date-Time
▪ Assignment 5 - Exception handling and Regular expression
Reference Books:
▪ 1. An Introduction to Computer Science using Python 3 by Jason Montojo, Jennifer Campbell, Paul Gries, The
pragmatic bookshelf-2013
▪ 2. James Payne, “Beginning Python: Using Python and Python 3.1,Wrox Publication
▪ 3. Introduction to Computer Science Using Python- Charles Dierbach, Wiley Publication Learning with Python
“, Green Tea Press, 2002
▪ 4. Introduction to Problem Solving with Python by E balguruswamy,TMH publication- 2016
▪ 5. Beginning Programming with Python for Dummies Paperback – 2015 by John Paul Mueller
▪ 6. Object-oriented Programming in Python, Michael H. Goldwasser, David Letscher, Pearson Prentice Hall-
2008
CH.1 INTRODUCTION TO
PYTHON (3)
INTRODUCTION:
What is Python Programming
Language?
▪ Python is an object-oriented
programming language created by
Guido Rossum in 1989. It is ideally
designed for rapid prototyping of
complex applications.
▪ It has interfaces to many OS
system calls and libraries and is
extensible to C or C++.
▪ Many large companies use the
Python programming language,
including NASA, Google, YouTube,
BitTorrent, etc.
Applications of Python
Programming:
1. Web Applications.
2. GUI based desktop Applications.
3. Scientific and Numeric Applications.
4. Software Development.
5. Business Applications.
6. Console based Applications.
7. Audio or Video based Applications.
8. 3D CAD Applications.
9. Enterprise Applications.
10. Applications for images.
11. Education.
About Python Programming Language:
▪ Python is a high-level, interpreted, interactive and object-oriented scripting
language. Python is designed to be highly readable. It uses English keywords
frequently whereas the other languages use punctuations. It has fewer syntactical
constructions than other languages.
▪ Python is Interpreted: Python is processed at runtime by the interpreter. You
do not need to compile your program before executing it. This is similar to PERL
and PHP.
▪ Python is Interactive: You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
▪ Python is Object-Oriented: Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
▪ Python is a Beginner's Language: Python is a great language for the
beginner level programmers and supports the development of a wide range of
applications from simple text processing to WWW browsers to games.
Python's features:
▪ Python's features include-
▪ Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows a student to pick up the language quickly.
▪ Easy-to-read: Python code is more clearly defined and visible to the eyes.
▪ Easy-to-maintain: Python's source code is fairly easy-to-maintain.
▪ A broad standard library: Python's bulk of the library is very portable and cross
platform compatible on UNIX, Windows, and Macintosh.
▪ Interactive Mode: Python has support for an interactive mode, which allows
interactive testing and debugging of snippets of code.
▪ 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. These
modules enable programmers to add to or customize their tools to be more efficient.
Python's features: continue…
▪ Databases: Python provides interfaces to all major commercial databases.
▪ GUI Programming: Python supports GUI applications that can be created and
ported to many system calls, libraries and windows systems, such as Windows MFC,
Macintosh, and the X Window system of Unix.
▪ Scalable: Python provides a better structure and support for large programs than
shell scripting. Apart from the above-mentioned features, Python has a big list of good
features.
▪ A few are listed below-
▪ It supports functional and structured programming methods as well as OOP.
▪ It can be used as a scripting language or can be compiled to byte-code for building
large applications.
▪ It provides very high-level dynamic data types and supports dynamic type checking.
▪ It supports automatic garbage collection. It can be easily integrated with C, C++,
COM, ActiveX, CORBA, and Java.
PYTHON PROGRAMMING LANGUAGE
Installing Python
Python editors for doing python programming:
▪ IDLE
▪ PyCharm
▪ Spyder
▪ PyDev
▪ Jupiter Notebook.
Python Basic Modes:
▪ Interactive Mode: Sybmol-- “>>>”
▪ It means ready to accept interactive commands.
▪ For Ex: >>> print(“interactive mode”)
▪ Script Mode:
File->New File to open blank script to write the code. Then save the script with .py
extension.
To run the script, Select Run->”Run Module”
BASICS OF PYTHON
CONTENTS OVERVIEW
▪ Standard datatypes
▪ Variables
▪ Constants
▪ Identifiers
▪ Reserved Words
▪ Lines, Indentation and Multiline Statements
▪ Input Output Functions
▪ Operations on Data Using Operators
Structure of Python Program-
Documentation Section
Import Statement
Global Declaration Section
Class Section
Subprogram Section
Play Ground Section
Data Members
Members Functions
Optional
Essential
STANDARD DATA TYPES
LIST OF DATA TYPES USED IN PYTHON PROGRAMMING LANGUAGE:
1.Numbers—Integers, floats, fractions, Complex Numbers
2.Strings—Text characters, special symbols or Alphanumeric data
3.List—Sequential data
4.Tuple—like list with little difference.
5.Dictionary—Unordered collection of key value pairs.
6.Boolean– The simplest build-in type in python is the bool data type, it represents
the two values, True and False.
7.None-Python defines a special variable None denoting a “Null object". None in
python is used for defining null variables and objects.
2 ] Variables
▪ Variable is like a container that stores values that we can access or change. It is a
way of pointing to a memory location used by a program.
▪ Syntax : variable=value
3] Constants
A constant is a type of variable whose value cannot be changed during the
execution of program. constants are usually declared and assigned in a module.
For eg: PI=3.14
4] Literals in python
5] Identifiers
6] Reserved words eg: and,as ,assert,break 7] Comments eg: #
Input / Output Functions:
▪ Input function helps to enter data at run time by the user and the output function.
▪ Input() : Input function reads a line of input from the keyboard. In python input() is
used to take input from user.
▪ Syntax:- variable_name=input() //withput argument
▪ Variable_name=input(‘string’) //with argument
▪ Operations on Data using Operators
1. Unary 5.Bitwise operators
2. Binary 6.Relational or comparison operators
3. Arithmetic Operators 7.Identity operators
4. Logical 8.Membership operators
INPUT AND OUTPUT FUNCTION
▪ Input(): reads a line of input from the keyboard. In python, input() function is used
to take the input from the user.
▪ Print(): print() function is used to output data to the standard output device.
▪ Syntax: input()-input(“string”)
▪ print()---print(“string”)
PRECEDENCE AND ASSOCIATIVITY OF
OPERATORS IN PYTHON
11-NOV-2021
The operator precedence in Python is listed in the following table. It is
in descending order (upper group has higher precedence than the
lower ones).
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR
Standard Data Types
▪ Dictionary Data Type:
▪ Represents a collection of data that associate a unique key with each value.
▪ SETS: A set is an unordered collection of items. Every element is unique(no
duplicates) and must be immutable(which can not be changed.
CH.2 CONTROL STATEMENTS
▪ 1.TYPE CONVERSION
▪ 2.CONDITIONAL STATEMENTS
1. If statement:
If condition:
statements(s)
2. If else statement:
If condition:
statement(s)
else:
statement(s)
CH.2 CONTROL STATEMENTS
▪ 3. if elif else statement:
If(condition 1):
statements
elif(condition 2):
statements
…..
elif(condition-n)
Statements
else:
statements
CH.2 CONTROL STATEMENTS continue…
Nested if statement:
▪ Syntax:
▪ If condition1:
If condition2:
statement1
else:
statement2
else:
statement3
.
LOOPING:
▪ While Loop:
Syntax:
while condition:
statement(s)
CH.2 CONTROL STATEMENTS continue…
▪ For Loop:
▪ For iterating_var in sequence :
▪ Statement(s)
Loop Control Statements:
▪ 1.break
▪ 2. continue
▪ 3.pass

2024-25 TYBSC(CS)-PYTHON_PROG_ControlStructure.pptx

  • 1.
    PYTHON PROGRAMMING T.Y.BSC(CS) SEM-V [COURSECODE VS 3510: Credits-2] By- Prof. Sangeeta M. Borde (Computer Science Department)
  • 2.
    SUBJECT SYLLABUS Chapter 1:An Introduction to Python ▪ Introduction to Python ▪ The Python Programming Language ▪ History of the Language, features Applications ▪ Installing Python, Running Simple Python program Basics of Python Standard data types - basic, none, Boolean (true & False), numbers, Variables, Constants ▪ Python identifiers and reserved words, ▪ Lines and indentation, ▪ multi-line statements and Comments, ▪ Input/output with print and input ,functions Declaration, ▪ Operations on Data such as assignment, arithmetic, relational, logical and bitwise operations, dry run, Simple Input and output etc.
  • 3.
    SUBJECT SYLLABUS Continue… Chapter2: Control Statements Sequence Control – Precedence of operators Type conversion Conditional Statements: if, if-else, nested if-else, Looping- for, while, nested loops, loop control statements (break, continue, pass). Strings: declaration, manipulation, special operations, escape character, string formatting operator, Raw String, Unicode strings, Built-in String methods. Chapter 3 Lists, functions, tuples and dictionaries, Sets Python Lists: Concept, creating and accessing elements, updating & deleting lists, traversing a List, reverse Built-in List Operators, Concatenation, Repetition, In Operator, Built-in List functions and methods. Functions: Definitions and Uses, Function Calls, Type Conversion Functions, Math Functions, Composition, Adding New Functions, Flow of Execution, Parameters and Arguments, Variables and Parameters, Stack Diagrams, Void Functions, Anonymous functions Importing with from, Return Values, Boolean Functions, More Recursion, Functional programming tools - filter(), map(), and reduce(),recursion, lambda forms.
  • 4.
    SUBJECT SYLLABUS Continue… Chapter3: Lists, functions, tuples and dictionaries, Sets Continue… Tuples and Dictionaries: Tuples, Accessing values in Tuples, Tuple Assignment, Tuples as return values, Variable-length argument tuples, and Basic tuples operations, Concatenation, Repetition, in Operator, Iteration, Built-in tuple functions, indexing, slicing and matrices. Creating a Dictionary, Accessing Values in a dictionary, Updating Dictionary, Deleting Elements from Dictionary, Properties of Dictionary keys, Operations in Dictionary, Built-In Dictionary Functions, Built-in Dictionary Methods. Sets- Definition, transaction of set(Adding, Union, intersection), working with sets
  • 5.
    SUBJECT SYLLABUS Continue… ▪Chapter 4 Modules ,Working with files, Exception handling ▪ Modules: Importing module, Creating & exploring modules, Math module, Random module, Time module Packages: Importing package, creating package, examples ▪ Working with files: Creating files and Operations on files (open, close, read, write), File object attributes, file positions, Listing Files in a Directory, Testing File Types, Removing files and directories, copying and renaming files, splitting pathnames, creating and moving directories ▪ Regular Expression: Concept of regular expression, various types of regular expressions, using match function. ▪ Exception Handling: Built-in Exceptions, Handling Exceptions, Exception with Arguments, User-defined Exceptions.
  • 6.
    SUBJECT SYLLABUS Continue… ▪Chapter 4 Modules ,Working with files, Exception handling ▪ Modules: Importing module, Creating & exploring modules, Math module, Random module, Time module Packages: Importing package, creating package, examples ▪ Working with files: Creating files and Operations on files (open, close, read, write), File object attributes, file positions, Listing Files in a Directory, Testing File Types, Removing files and directories, copying and renaming files, splitting pathnames, creating and moving directories ▪ Regular Expression: Concept of regular expression, various types of regular expressions, using match function. ▪ Exception Handling: Built-in Exceptions, Handling Exceptions, Exception with Arguments, User-defined Exceptions.
  • 7.
    SUBJECT SYLLABUS Continue… ProgrammingAssignments ▪ Assignment 1 - Python Basics ▪ Assignment 2 – Arrays, Strings, and Functions Assignment 3 - List, Tuples, Sets, and Dictionary ▪ Assignment 4 - File Handling and Date-Time ▪ Assignment 5 - Exception handling and Regular expression Reference Books: ▪ 1. An Introduction to Computer Science using Python 3 by Jason Montojo, Jennifer Campbell, Paul Gries, The pragmatic bookshelf-2013 ▪ 2. James Payne, “Beginning Python: Using Python and Python 3.1,Wrox Publication ▪ 3. Introduction to Computer Science Using Python- Charles Dierbach, Wiley Publication Learning with Python “, Green Tea Press, 2002 ▪ 4. Introduction to Problem Solving with Python by E balguruswamy,TMH publication- 2016 ▪ 5. Beginning Programming with Python for Dummies Paperback – 2015 by John Paul Mueller ▪ 6. Object-oriented Programming in Python, Michael H. Goldwasser, David Letscher, Pearson Prentice Hall- 2008
  • 8.
  • 9.
    INTRODUCTION: What is PythonProgramming Language? ▪ Python is an object-oriented programming language created by Guido Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. ▪ It has interfaces to many OS system calls and libraries and is extensible to C or C++. ▪ Many large companies use the Python programming language, including NASA, Google, YouTube, BitTorrent, etc. Applications of Python Programming: 1. Web Applications. 2. GUI based desktop Applications. 3. Scientific and Numeric Applications. 4. Software Development. 5. Business Applications. 6. Console based Applications. 7. Audio or Video based Applications. 8. 3D CAD Applications. 9. Enterprise Applications. 10. Applications for images. 11. Education.
  • 10.
    About Python ProgrammingLanguage: ▪ Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently whereas the other languages use punctuations. It has fewer syntactical constructions than other languages. ▪ Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP. ▪ Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs. ▪ Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects. ▪ Python is a Beginner's Language: Python is a great language for the beginner level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.
  • 11.
    Python's features: ▪ Python'sfeatures include- ▪ Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This allows a student to pick up the language quickly. ▪ Easy-to-read: Python code is more clearly defined and visible to the eyes. ▪ Easy-to-maintain: Python's source code is fairly easy-to-maintain. ▪ A broad standard library: Python's bulk of the library is very portable and cross platform compatible on UNIX, Windows, and Macintosh. ▪ Interactive Mode: Python has support for an interactive mode, which allows interactive testing and debugging of snippets of code. ▪ 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. These modules enable programmers to add to or customize their tools to be more efficient.
  • 12.
    Python's features: continue… ▪Databases: Python provides interfaces to all major commercial databases. ▪ GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix. ▪ Scalable: Python provides a better structure and support for large programs than shell scripting. Apart from the above-mentioned features, Python has a big list of good features. ▪ A few are listed below- ▪ It supports functional and structured programming methods as well as OOP. ▪ It can be used as a scripting language or can be compiled to byte-code for building large applications. ▪ It provides very high-level dynamic data types and supports dynamic type checking. ▪ It supports automatic garbage collection. It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
  • 13.
    PYTHON PROGRAMMING LANGUAGE InstallingPython Python editors for doing python programming: ▪ IDLE ▪ PyCharm ▪ Spyder ▪ PyDev ▪ Jupiter Notebook.
  • 14.
    Python Basic Modes: ▪Interactive Mode: Sybmol-- “>>>” ▪ It means ready to accept interactive commands. ▪ For Ex: >>> print(“interactive mode”) ▪ Script Mode: File->New File to open blank script to write the code. Then save the script with .py extension. To run the script, Select Run->”Run Module”
  • 15.
    BASICS OF PYTHON CONTENTSOVERVIEW ▪ Standard datatypes ▪ Variables ▪ Constants ▪ Identifiers ▪ Reserved Words ▪ Lines, Indentation and Multiline Statements ▪ Input Output Functions ▪ Operations on Data Using Operators
  • 16.
    Structure of PythonProgram- Documentation Section Import Statement Global Declaration Section Class Section Subprogram Section Play Ground Section Data Members Members Functions Optional Essential
  • 17.
    STANDARD DATA TYPES LISTOF DATA TYPES USED IN PYTHON PROGRAMMING LANGUAGE: 1.Numbers—Integers, floats, fractions, Complex Numbers 2.Strings—Text characters, special symbols or Alphanumeric data 3.List—Sequential data 4.Tuple—like list with little difference. 5.Dictionary—Unordered collection of key value pairs. 6.Boolean– The simplest build-in type in python is the bool data type, it represents the two values, True and False. 7.None-Python defines a special variable None denoting a “Null object". None in python is used for defining null variables and objects.
  • 18.
    2 ] Variables ▪Variable is like a container that stores values that we can access or change. It is a way of pointing to a memory location used by a program. ▪ Syntax : variable=value 3] Constants A constant is a type of variable whose value cannot be changed during the execution of program. constants are usually declared and assigned in a module. For eg: PI=3.14 4] Literals in python 5] Identifiers 6] Reserved words eg: and,as ,assert,break 7] Comments eg: #
  • 19.
    Input / OutputFunctions: ▪ Input function helps to enter data at run time by the user and the output function. ▪ Input() : Input function reads a line of input from the keyboard. In python input() is used to take input from user. ▪ Syntax:- variable_name=input() //withput argument ▪ Variable_name=input(‘string’) //with argument ▪ Operations on Data using Operators 1. Unary 5.Bitwise operators 2. Binary 6.Relational or comparison operators 3. Arithmetic Operators 7.Identity operators 4. Logical 8.Membership operators
  • 20.
    INPUT AND OUTPUTFUNCTION ▪ Input(): reads a line of input from the keyboard. In python, input() function is used to take the input from the user. ▪ Print(): print() function is used to output data to the standard output device. ▪ Syntax: input()-input(“string”) ▪ print()---print(“string”)
  • 21.
    PRECEDENCE AND ASSOCIATIVITYOF OPERATORS IN PYTHON 11-NOV-2021
  • 22.
    The operator precedencein Python is listed in the following table. It is in descending order (upper group has higher precedence than the lower ones). Operators Meaning () Parentheses ** Exponent +x, -x, ~x Unary plus, Unary minus, Bitwise NOT *, /, //, % Multiplication, Division, Floor division, Modulus +, - Addition, Subtraction <<, >> Bitwise shift operators & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators not Logical NOT and Logical AND or Logical OR
  • 23.
    Standard Data Types ▪Dictionary Data Type: ▪ Represents a collection of data that associate a unique key with each value. ▪ SETS: A set is an unordered collection of items. Every element is unique(no duplicates) and must be immutable(which can not be changed.
  • 24.
    CH.2 CONTROL STATEMENTS ▪1.TYPE CONVERSION ▪ 2.CONDITIONAL STATEMENTS 1. If statement: If condition: statements(s) 2. If else statement: If condition: statement(s) else: statement(s)
  • 25.
    CH.2 CONTROL STATEMENTS ▪3. if elif else statement: If(condition 1): statements elif(condition 2): statements ….. elif(condition-n) Statements else: statements
  • 26.
    CH.2 CONTROL STATEMENTScontinue… Nested if statement: ▪ Syntax: ▪ If condition1: If condition2: statement1 else: statement2 else: statement3 .
  • 27.
  • 28.
    CH.2 CONTROL STATEMENTScontinue… ▪ For Loop: ▪ For iterating_var in sequence : ▪ Statement(s)
  • 29.
    Loop Control Statements: ▪1.break ▪ 2. continue ▪ 3.pass