Docsity

Python programming notes.docx, Exams of Nursing

Partial preview of the text

Download Python programming notes.docx and more Exams Nursing in PDF only on Docsity!

PYTHON PROGRAMMING III YEAR/II SEM

PYTHON PROGRAMMING

LECTURE NOTES

DEPARTMENT OF

COMPUTER SCIENCE AND

ENGINEERING

MALLA REDDY COLLEGE OF ENGINEERING &

TECHNOLOGY WITH CORRECT ANSWERS 2026/

PYTHON PROGRAMMING III YEAR/II SEM

SYLLADUS

MALLA REDDY COLLEGE OF ENGINEERING AND TECHNOLOGY III Year D. Tech CSE -II SEM L T/P/D C 3 - / - / - 3 OPEN ELECTIVE III (R17A0554) PYTHON PROGRAMMING ODJECTIVES:  To read and write simple Python programs.  To develop Python programs with conditionals and loops.  To define Python functions and call them.  To use Python data structures –- lists, tuples, dictionaries.  To do input/output with files in Python. UNIT I INTRODUCTION DATA, EXPRESSIONS, STATEMENTS Introduction to Python and installation , data types: Int, float, Doolean, string, and list; variaDles, expressions, statements, precedence of operators, comments; modules, functions--- function and its use, flow of execution, parameters and arguments. UNIT II CONTROL FLOW, LOOPS Conditionals: Doolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-else); Iteration: while, for, Dreak, continue. UNIT III FUNCTIONS, ARRAYS Fruitful functions: return values, parameters, local and gloDal scope, function composition, recursion; Strings: string slices, immutaDility, string functions and methods, string module; Python arrays, Access the Elements of an Array, array methods. UNIT IV LISTS, TUPLES, DICTIONARIES Lists: list operations, list slices, list methods, list loop, mutaDility, aliasing, cloning lists, list parameters, list comprehension; Tuples: tuple assignment, tuple as return value, tuple comprehension; Dictionaries: operations and methods, comprehension; UNIT V FILES, EXCEPTIONS, MODULES, PACKAGES Files and exception: text files, reading and writing files, command line arguments, errors and exceptions, handling exceptions, modules (datetime, time, OS , calendar, math module), Explore packages.

PYTHON PROGRAMMING III YEAR/II SEM

INDEX

UNIT TOPIC PAGE NO I INTRODUCTION DATA, EXPRESSIONS, STATEMENTS

Introduction to Python and installation (^) 1 data types: Int (^6) float (^7) Doolean (^8) string (^8) List (^10) variaDles (^11) expressions (^13) statements (^16) precedence of operators (^17) comments 18 modules 19 functions-----function and its use (^20) flow of execution (^21) parameters and arguments (^26) II

CONTROL FLOW, LOOPS 35

Conditionals : Doolean values and operators,

conditional (if) (^36) alternative (if-else) (^37) chained conditional (if-elif-else) 39 Iteration: while, for, Dreak, continue. 41 III

FUNCTIONS, ARRAYS 55

Fruitful functions: return values (^55) parameters (^57) local and gloDal scope (^59) function composition (^62) recursion (^63) Strings: string slices (^64) immutaDility (^66) string functions and methods (^67) string module (^72) Python arrays (^73) Access the Elements of an Array 75 Array methods 76 LISTS, TUPLES, DICTIONARIES (^) 78

PYTHON PROGRAMMING III YEAR/II SEM IV Lists (^78) list operations (^79) list slices (^80) list methods (^81) list loop (^83) mutaDility (^85) aliasing (^87) cloning lists (^88) list parameters (^89) list comprehension (^90) Tuples (^91) tuple assignment (^94) tuple as return value (^95) tuple comprehension (^96) Dictionaries (^97) operations and methods (^97) comprehension (^102) V FILES, EXCEPTIONS, MODULES, PACKAGES

Files and exception: text files (^103) reading and writing files (^104) command line arguments (^109) errors and exceptions (^112) handling exceptions (^114) modules (datetime, time, OS , calendar, math module)

Explore packages (^134)

PROGRAMMING SEM ET Statement

PROGRAMMING SEM ET Statement Statement 3 # Script Ends Differences Detween scripting language and programming language: Why to use Python: The following are the primary factors to use python in day-to-day life:

1. Python is oDject-oriented Structure supports such concepts as polymorphism, operation overloading and multiple inheritance.

PROGRAMMING SEM ET

3. It’s free (open source) Downloading python and installing python is free and easy 4. It’s Powerful  Dynamic typing  Duilt-in types and tools  LiDrary utilities  Third party utilities (e.g. Numeric, NumPy, sciPy)  Automatic memory management 5. It’s PortaDle  Python runs virtually every major platform used today  As long as you have a compaitaDle python interpreter installed, python programs will run in exactly the same manner, irrespective of platform. 6. It’s easy to use and learn  No intermediate compile  Python Programs are compiled automatically to an intermediate form called Dyte code, which the interpreter then reads.  This gives python the development speed of an interpreter without the performance loss inherent in purely interpreted languages.  Structure and syntax are pretty intuitive and easy to grasp. 7. Interpreted Language Python is processed at runtime Dy python Interpreter 8. Interactive Programming Language Users can interact with the python interpreter directly for writing the programs 9. Straight forward syntax The formation of python syntax is simple and straight forward which also makes it popular. Installation: There are many interpreters availaDle freely to run Python scripts like IDLE (Integrated Development Environment) which is installed when you install the python software from http://python.org/downloads/ Steps to De followed and rememDered: Step 1: Select Version of Python to Install. Step 2: Download Python ExecutaDle Installer. Step 3: Run ExecutaDle Installer.

PROGRAMMING SEM ET Step 4: Verify Python Was Installed On Windows.

PROGRAMMING SEM ET

  • Script Mode

PROGRAMMING SEM ET Running Python in interactive mode: Without passing python script file to the interpreter, directly execute code to Python prompt. Once you’re inside the python interpreter, then you can start. >>> print("hello world") hello world

Relevant output is displayed on suDsequent lines without the >>> symDol

>>> x=[0,1,2]

Quantities stored in memory are not displayed Dy default.

>>> x #If a quantity is stored in memory, typing its name will display it. [0, 1, 2] >>> 2+ 5 The chevron at the Deginning of the 1st line, i.e., the symDol >>> is a prompt the python interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter replies

Running Python in script mode:

PROGRAMMING SEM ET >>> print(0D10) 2 >>> print(0X20) 32 >>> 20 20 >>> 0 D 10 2 >>> a= >>> print(a) 10 # To verify the type of any oDject in Python, use the type() function: >>> type(10)

>>> a= >>> print(type(a))

Float: Float, or "floating point numDer" is a numDer, positive or negative, containing one or more decimals. Float can also De scientific numDers with an "e" to indicate the power of 10. >>> y=2. >>> y

>>> y=2. >>> print(type(y))

>>> type(.4)

PROGRAMMING SEM ET >>> 2.

PROGRAMMING SEM ET

PROGRAMMING SEM ET >>> print('mrcet college') mrcet college >>> " " ' ' If you want to include either type of quote character within the string, the simplest way is to delimit the string with the other type. If a string is to contain a single quote, delimit it with douDle quotes and vice versa: >>> print("mrcet is an autonomous (') college") mrcet is an autonomous (') college >>> print('mrcet is an autonomous (") college') mrcet is an autonomous (") college Suppressing Special Character: Specifying a Dackslash () in front of the quote character in a string “escapes” it and causes Python to suppress its usual special meaning. It is then interpreted simply as a literal single quote character: >>> print("mrcet is an autonomous (') college") mrcet is an autonomous (') college >>> print('mrcet is an autonomous (") college') mrcet is an autonomous (") college The following is a taDle of escape sequences which cause Python to suppress the usual special interpretation of a character in a string: >>> print('a ....D') a ... D >>> print('a D\