




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Various topics in python programming, including basic data types, list operations, list slices, list methods, list loops, mutability, aliasing, cloning lists, list parameters, installation of python, iterating over different data structures like lists, tuples, dictionaries, and strings, nested for loops, break and continue statements, functions, default arguments, recursive functions, command line arguments, errors and exceptions, file handling, modules, and numpy arrays. It also introduces object-oriented programming concepts in python, such as classes, objects, inheritance, polymorphism, data abstraction, and encapsulation. Additionally, the document explores the django web framework with an example. A comprehensive overview of python programming, covering both fundamental concepts and advanced topics, making it a valuable resource for students and learners.
Typology: Lecture notes
1 / 183
This page cannot be seen from the preview
Don't miss anything!
Python Programming
Recognized under 2(f) and 12 (B) of UGC ACT 1956 (Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – „A‟ Grade - ISO 9001:2015Certified) Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India
Python Programming SYLLABUS MALLA REDDY COLLEGE OF ENGINEERING AND TECHNOLOGY III Year B. Tech IT - I SEM L T/P/D C 3 - / - / - 3 OBJECTIVES:
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, variables, expressions, statements, Numeric data types: Int, float, Boolean, string. Basic data types: list--- list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters. Tuple --- tuple assignment, tuple as return value, tuple methods. Sets: operations and methods. Dictionaries: operations and methods. UNIT II CONTROL FLOW, LOOPS, FUNCTIONS Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if- elif-else); Iteration: statements break, continue.Functions--- function and its use, pass keyword, flow of execution, parameters and arguments. UNIT III ADVANCED FUNCTIONS, ARRAYS Fruitful functions: return values, parameters, local and global scope, function composition, recursion; Advanced Functions: lambda, map, filter, reduce, basic data type comprehensions. Python arrays: create an array, Access the Elements of an Array, array methods. UNIT IV FILES,EXCEPTIONS File I/O, Exception Handling, introduction to basic standard libraries, Installation of pip, Demonstrate Modules: Turtle, pandas, numpy, pdb, Explore packages. UNIT V OOPS,FRAMEWORK : Object,Class,Method,Inheritance, Polymorphism,Data Abstraction, Encapsulation, Python Frameworks: Explore django framework with an example
Python Programming INDEX UNIT TOPIC PAGE NO I INTRODUCTION DATA, EXPRESSIONS, STATEMENTS Introduction to Python and installation (^6) - 10 Variables (^11) - 13 Expressions, Statements (^14) - 18 Data types: Int, float, Boolean, string. (^19) - 30 Basic data types: list--- list operations, list slices, list methods,
list loop, mutability, aliasing, cloning lists, list parameters.
Tuple --- tuple assignment, tuple as return value, tuple methods
Dictionaries: operations and methods. (^51) - 56 II CONTROL FLOW, LOOPS, FUNCTIONS Conditionals: Boolean values and operators (^57) - 59 Conditional (if), alternative (if-else), Chained conditional (if-elif-else)
Iterations (^64) - 71 Statements break, continue. (^72) - 77 Functions and its use (^78) - 82 Pass keyword, flow of execution, parameters and arguments.
ADVANCED FUNCTIONS, ARRAYS Fruitful functions: return values, parameters, (^93) - 96 Local and Global scope, function Composition
Recursion, lambda, map, filter, reduce, (^100) - 101 Basic data type comprehensions (^102) - 103 Python arrays: create an array, Access the Elements of an Array, array methods.
Python Programming
FILES,EXCEPTIONS , File I/O (^107) - 115 Exception Handling (^116) - 126 introduction to basic standard libraries (^127) - 143 Installation of pip (^144) - 145 Demonstrate Modules: Turtle (^146) - 149 Pandas (^150) - 153 Numpy (^154) - 157 Pdb, Explore packages. (^158) - 160 V OOPS,FRAMEWORK Object,Class,Method (^161) - 163 Inheritance (^163) - 169 Polymorphism (^170) - 171 Data Abstraction (^171) - 173 Encapsulation (^173) - 174 Python Frameworks (^175) - 176 Explore django framework with an example
III YEAR/I SEM Python Programming Page | 7 Statement Statement Statement
Differences between 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 object-oriented Structure supports such concepts as polymorphism, operation overloading and multiple inheritance. 2. Indentation:Indentation is one of the greatest feature in python
III YEAR/I SEM Python Programming Page | 8
3. It‟s free (open source) Downloading python and installing python is free and easy 4. It‟s Powerful Dynamic typing Built-in types and tools Library utilities Third party utilities (e.g. Numeric, NumPy, sciPy) Automatic memory management 5. It‟s Portable Python runs virtually every major platform used today As long as you have a compaitable 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 byte 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 by 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 available 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 be followed and remembered: Step 1: Select Version of Python to Install. Step 2: Download Python Executable Installer. Step 3: Run Executable Installer. Step 4: Verify Python Was Installed On Windows. Step 5: Verify Pip Was Installed.
III YEAR/I SEM Python Programming Page | 10 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
x=[0,1,2]
x #If a quantity is stored in memory, typing its name will display it. [0, 1, 2] 2+ 5 The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter replies 8.
III YEAR/I SEM Python Programming Page | 11 Running Python in script mode: Alternatively, programmers can store Python script source code in a file with the .py extension, and use the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the interpreter the name of the file. For example, if you have a script name MyFile.py and you're working on Unix, to run the script you have to type: python MyFile.py Working with the interactive mode is better when Python programmers deal with small pieces of code as you can type and execute them immediately, but when the code is more than 2 - 4 lines, using the script for coding can help to modify and use the code in future. Example: Variables: 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. Rules for Python variables:
III YEAR/I SEM Python Programming Page | 13 For example − a,b,c = 1,2,"mrcet“ Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c. Output Variables: The Python print statement is often used to output variables. Variables do not need to be declared with any particular type and can even change type after they have been set. x = 5 # x is of type int x = "mrcet " # x is now of type str print(x) Output: mrcet To combine both text and a variable, Python uses the “+” character: Example x = "awesome" print("Python is " + x) Output Python is awesome You can also use the + character to add a variable to another variable: Example x = "Python is " y = "awesome" z = x + y print(z) Output: Python is awesome Expressions:
III YEAR/I SEM Python Programming Page | 14 An expression is a combination of values, variables, and operators. An expression is evaluated using assignment operator. Examples: Y=x + 17
x= z=x+ z 30 x= y= c=x+y c 30 A value all by itself is a simple expression, and so is a variable. y= y 20 Python also defines expressions only contain identifiers, literals, and operators. So, Identifiers: Any name that is used to define a class, function, variable module, or object is an identifier. Literals: These are language-independent terms in Python and should exist independently in any programming language. In Python, there are the string literals, byte literals, integer literals, floating point literals, and imaginary literals. Operators: In Python you can implement the following operations using the corresponding tokens.
III YEAR/I SEM Python Programming Page | 16 Some of the python expressions are: Generator expression: Syntax: ( compute(var) for var in iterable )
x = (i for i in 'abc') #tuple comprehension x <generator object
at 0x033EEC30> print(x) <generator object at 0x033EEC30> You might expect this to print as ('a', 'b', 'c') but it prints as <generator object at 0x02AAD710> The result of a tuple comprehension is not a tuple: it is actually a generator. The only thing that you need to know now about a generator now is that you can iterate over it, but ONLY ONCE. Conditional expression: Syntax: true_value if Condition else false_value c=x+y >>> c 30 A value all by itself is a simple expression, and so is a variable. >>> y= >>> y 20 Python also defines expressions only contain identifiers, literals, and operators. So, Identifiers: Any name that is used to define a class, function, variable module, or object is an identifier. Literals: These are language-independent terms in Python and should exist independently in any programming language. In Python, there are the string literals, byte literals, integer literals, floating point literals, and imaginary literals. Operators: In Python you can implement the following operations using the corresponding tokens. III YEAR/I SEM Python Programming Page | 16 Some of the python expressions are: Generator expression: Syntax: ( compute(var) for var in iterable ) >>> x = (i for i in 'abc') #tuple comprehension >>> x <generator object
at 0x033EEC30> >>> print(x) <generator object at 0x033EEC30> You might expect this to print as ('a', 'b', 'c') but it prints as <generator object at 0x02AAD710> The result of a tuple comprehension is not a tuple: it is actually a generator. The only thing that you need to know now about a generator now is that you can iterate over it, but ONLY ONCE. Conditional expression: Syntax: true_value if Condition else false_value >>> x = "1" if True else "2" x '1' Statements: A statement is an instruction that the Python interpreter can execute. We have normally two basic statements, the assignment statement and the print statement. Some other kinds of statements that are if statements, while statements, and for statements generally called as control flows. Examples: An assignment statement creates new variables and gives them values:
PYTHON PROGRAMMING
x= III YEAR/I SEM MRCET 17 college="mrcet" An print statement is something which is an input from the user, to be printed / displayed on to the screen (or ) monitor. print("mrcet colege") mrcet college Precedence of Operators: Operator precedence affects how an expression is evaluated. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first multiplies 32 and then adds into 7. Example 1: 3+4 11 Multiplication gets evaluated before the addition operation (10+10)* 40 Parentheses () overriding the precedence of the arithmetic operators Example 2: a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print("Value of (a + b) * c / d is ", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print("Value of ((a + b) * c) / d is ", e)
PYTHON PROGRAMMING III YEAR/I SEM MRCET 19 Numeric Data types: The data stored in memory can be of many types. For example, a student roll number is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Int: Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
print(24656354687654+2) 24656354687656 print(20) 20 print(0b10) 2
PYTHON PROGRAMMING III YEAR/I SEM MRCET 20
print(0B10) 2 print(0X20) 32 20 20 0b
print("mrcet colege") mrcet college Precedence of Operators: Operator precedence affects how an expression is evaluated. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first multiplies 32 and then adds into 7. Example 1: >>> 3+4 11 Multiplication gets evaluated before the addition operation >>> (10+10)* 40 Parentheses () overriding the precedence of the arithmetic operators Example 2: a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print("Value of (a + b) * c / d is ", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print("Value of ((a + b) * c) / d is ", e) PYTHON PROGRAMMING III YEAR/I SEM MRCET 19 Numeric Data types: The data stored in memory can be of many types. For example, a student roll number is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Int: Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. >>> print(24656354687654+2) 24656354687656 >>> print(20) 20 >>> print(0b10) 2 PYTHON PROGRAMMING III YEAR/I SEM MRCET 20 >>> print(0B10) 2 >>> print(0X20) 32 >>> 20 20 >>> 0b 2 a= print(a) 10 # To verify the type of any object in Python, use the type() function: type(10) <class 'int'> a= print(type(a)) <class 'int'> Float: Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Float can also be scientific numbers with an "e" to indicate the power of 10. y=2. y
y=2. print(type(y)) <class 'float'> type(.4) <class 'float'>