Python Programming (22CS104)
Department of Advanced Computer
Science and Engineering
MODULE-1 - UNIT-1
INTRODUCTION:
1.Introduction to python, Variables, Assignment, Keywords
2.Built-in functions, Indentation, Comments
3.Basic data types - integers, float, complex, Boolean, strings
4.Python program development, running python using REPL shell, Python scripts.
5.Operators and Expressions: Operators- arithmetic operators, comparison (relational)
operators, assignment operators, logical operators, bitwise operators, membership
operators, identity operators; Expressions and order of evaluations.
6.Control Structures: Conditional control structures - if, elsif, else; Loop control
structures - for, while, for... else, while…else, nested loops, break, continue, pass .
MODULE-1 - UNIT-2
PYTHON DATA STRUCTURES AND FUNCTIONS:
Data Structures: Lists, Tuples, Sets, Strings, Dictionary - creation, accessing, basic operators and methods; List
comprehensions.
Functions: Defining functions, calling functions,
Passing arguments - keyword arguments,
default arguments,
positional arguments,
variable-length arguments;
Types of functions- anonymous functions,
fruitful function,
recursive functions;
Scope of the variables- global and local variables,
Development of recursive and non-recursive functions.
MODULE-2 - UNIT-1
MODULES:
Creating modules,
Import Statement, From...Import Statement, Name Spacing,
Creating user defined modules.
Standard Modules: sys, math, date, time, os, random and itertools modules.
Packages: Numpy,
Pandas,
Matplotlib,
Requests,
Nltk.
File Processing: Reading and writing files -creating a new file, writing to a file, reading text files,
opening and closing files,
reading, writing, tell (), seek (), rename ().
MODULE-2 - UNIT-2
ERRORS AND EXCEPTIONS:
Introduction to Exceptions,
Handling Exception,
Try Except Else and Finally Block,
Raising Exceptions.
Simple Graphics and Image Processing: Overview of Turtle Graphics,
Two Dimensional Shapes,
Colours and RBG System and Image Processing.
6
WHAT IS PYTHON ?
• Python is a programming language, which is
object oriented, high level, interpreted,
multipurpose and extremely user friendly
• Python is a very popular, simple and easy to
learn programming language
• Can be used to handle big data and perform
complex mathematics
7
Why is Python Popular?
Allows to
write
Easy to Easy programs in
learn Syntax fewer lines of
code
Open
Access
8
(b) Example code: Java
(a) Example code: C++
(d) Example code: Python
(c) Example code: C
History of
Python
• Invented in the Netherlands by Guido Van Rossum
• Python was coined and conceived in the late 1980s
• Implementation was started in December 1989
• Named after Monty Python in 1991
• Guido Van Rossum(the founder) was a fan of Monty Python’s
‘Flying Circus’ – a famous TV show in the
Netherlands then.
VERSIO
NS
How Python
works
1. Easy to Read, Learn and Use
2. Interpreted Language
3. Interactive Language
4. Portable
5. Free and Open Source
6. Embeddable
7. Huge Library
8. Cross-Compilation
9. Object Oriented
10. Extensible
11. Scalable
12. Database Interfaces
13. GUI programming
14. Automatic Memory Management-
APPL I CAT I ONs
OF PYTHON
WEB D ATA
A P P L I C AT I O N s SCIENCE
D E S KT O P IMAGE
A P P L I C AT I O N s PROCEssING
D ATA B A S E
GAME MANAGEME
DEV ELOP M EN T NT
W E B A P P L I C AT I O N
DEVELO PM ENT
D E S KT O P A P P L I C AT I O N
GAME
DEVELO PM ENT
D ATA B A S E
D ATA S C I E N C E
IMAGEPROCESSING
24
Python is used by :
Google, Bit-Torrent, YouTube,
Pinterest, Instagram, Intel, IBM,
NASA, Pixar, Netflix, Facebook, JP
Morgan Chase, Spotify etc.
25
Identifiers I
Identifiers are names used to identify a variable, function, or other entities in a
program.
The rules for naming an identifier in Python are as follows:
• The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_).
• This may be followed by any combination of characters a–z, A–Z, 0–9 or underscore (_).
• An identifier cannot start with a digit.
• It can be of any length.
• It should not be a keyword or reserved word
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers
26 Identifiers II
_acse Time_table
Acse_marks _students_acse12$
Days_in_week 2024_acse_students
Class1_heights acse2024students
1_year acse_2024#students
221_name acse@firstyear_students
Harsha_workhour! _marks_acsestudents1styear
Table: Examples of correct/incorrect identifiers
27 Identifiers II
_acse Time_table
Acse_marks _students_acse12$
Days_in_week 2024_acse_students
Class1_heights acse2024students
1_year acse_2024#students
221_name acse@firstyear_students
Harsha_workhour! _marks_acsestudents1styear
Table: Examples of correct/incorrect identifiers
Assignmen
t•
An assignment statement is used to assign a value to a variable.
• Once a variable is assigned a value, the variable can be used in place
of that value.
• The equal sign is symbol for assignment.
• Ex : a = 5 ; a = “FIVE”
a = ‘five’
Python allows you to assign values to multiple variables in one line:
• x, y, z = "Orange", "Banana", "Cherry"
• x = y = z = "Orange"
print(x)
print(y) print(x)
print(z) print(y)
print(z)
M Bhargavi|AsstProf|CSE
29 Assignments
A=25
B=35
C=A+B
Name=‘Harry’; Pi=3.14 ; B=45
B, Pi, Name=45, 3.14, ‘Harry’
a=b=c=d=67
Python Keywords
30
• Each keyword has a specific meaning to the Python
interpreter (It can be used in programs only for the purpose
for which it has been defined)
• Keywords are reserved words.
• Python is case sensitive. (Name, name, naMe, NamE,
are not same)
Input-Output
32
# Taking input from the user
name = input("Enter your
name: ") print("Hello, " +
name)
print(type(name)
)
Output
Enter your
name: vignan
Hello , vignan
M Bhargavi|AsstProf|CSE
Indentation
Definition
• Indentation refers to the spaces at the beginning of a code line (The
first line of the for loop must end with a colon, and the body must
be indented)
• Where in other programming languages the indentation in code is
for readability only, the indentation in Python is very important.
Example
• if(5>2):
print("5 is greater than 2")
Output: 5 is greater than 2
• a = 33
b = 200
if b > a:
print("b is greater than a")
Output: b is greater than a
• if(5>2):
print("5 is greater than 2")
Output: Indentation Error
Comments
Definition
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a # for single line
Comments starts with a ’’’ …….. ‘’’ for multiple line
and Python will ignore them.
Example
• #This is a comment
print("Hello, World!")
Output: Hello, World!
• #This is a comment
print("Hello, World!")
Output: Hello, World!