Page 1 :
ADVANCED PYTHON
Page 2 :
Class – IX & X Artificial Intelligence, Chapter Notes, , 5. Type conda update --all –yes command. It will update Anaconda, on your computer to the latest version., Start Using Jupyter Notebook, 1. Type jupyter notebook command to open Jupyter notebook., 2. Jupyter Notebook opens in your default browser. The Notebook, Dashboard will be visible, which will show a list of notebook, files, and subdirectories where the notebook server had started. Mostly it is, your home directory., , Topic : Advanced Python, Introduction to Jupyter Notebook :, The Jupyter Notebook is an open-source web application. It enables, us to create and share documents with live codes, equations,, visualization etc., This platform is also used for data cleaning. It is also used for, transformation, statistical modelling, data visualisation, Machine, learning, and a lot more. It supports more than 40 different, programming languages, and Python is one of them. Python must be, installed on your system before you install the Jupyter Notebook., , Creating a New Notebook, 1. On the Dashboard, Click on the ‘New’ button at the top rightcorner., 2. From the drop-down list. Click on Python 3., 3. This will open a new notebook., 4. Another tab in your browser will open., 5. If you click on the ‘Untitled’, which is at the top of the screen,, you can change the name of your notebook., 6. It will saved as a file name along with extension ‘ipynb’., , Before Installation:, • You can try features of Jupyter Notebook before installing on, your PC., • For this, visit https://jupyter.org in your browser and choose ‘Try, Jupyter with Python’ option., • This will open a homepage of https://mybinder.org, INSTALLATION, 1. Using Anaconda Installer:, Anaconda’s latest Python 3 version can be downloaded from:, https://www.anaconda.com/products/individual, 2. During Installation of Anaconda, Select the default options for, easy, setup., 3. After Downloading, Install Anaconda, Open “Anaconda Prompt”, by finding it in the windows (Start) Menu., 4. Type python –version command to verify that Anaconda has been, installed on your PC., , Working with the Notebook, 1. The Notebook includes cells where you write your python program., Once a new notebook is created, the first cell is empty and already, available for you to write your code., 2. By default, the cells are of type Code and you can start typing in, Python code directly., 3. To execute the lines of code in a cell, Press Sift + Enter keys or, Simply click on Run button., , •, , More About Jupyter Notebook:, • Jupyter Notebook is a powerful tool for creating and developing, interactive data science projects., Jupyter Notebook is used extensively in Python for scientific, computing and data analysis along with Python libraries like NumPy,, pandas and matplotlib.
Page 3 :
Introduction to Virtual Environment:, A Virtual environment is a tool that helps to keep the, dependencies required by different projects separate by creating, isolated Python Virtual Environments for them. This is one of the, most important tools that the Python developers use., 1. To get started, Let’s make a new directory called, python_venv and enter it., Mkdir python_venv, cd python_venv, 2. Enter the following command to create a virtual environment., python3 –m venv My_venv01, 3. The command executes the module venv and passes the name, of the environment, which is to be created., 4. To activate the virtual environment and start working in the, environment, enter the following command:, My_venv01\Scripts\activate.bat, 5. By doing so, the name of the virtual environment should be, visible in parathenses at the beginning of the command, prompt., , Introduction to Python:, , Guido van Rossum developed Python. It is a very simple, programming language and quite easy to learn., , Myth v/s Reality:, Myth, Python was named after the, ferocious snake ‘Python’., , Reality, Did you ever watch a TV, show named Monty, Python’s Flying circus?, Python was named after, this show., , Features of Python:, 1. Free of Cost., 2. Open source Programming language, , 3. Available on almost Devices and OS (Windows, MacOS,, Linux, Unix etc.), 4. Extensive standard library, 5. Interpreted language, 6. Exception handling, 7. Automated Memory management., Applications of Python:, 1. Web development, : Server-side code, backend, programming, logic, mapping, URLs etc, 2. Machine learning, : Product recommendation, Face, recognition, voice recognition etc., 3. Data Analysis:, 4. Game development, 5. Scripting, , Python Basics:, Python code can be written in text editors as well as Integrated, Developments (IDEs)., The Print() Function:, Python has inbuilt function print() that is used for displaying, content on the screen., The syntax for the print( ) is:, , Note : Double-quotes are used as per the syntax for displaying simple, messages., Python Statement & Comments:, Statements: Any instruction that a Python interpreter can execute are, called statements., Ex. var = 24 is an assignment statement.
Page 4 :
Comments: Comments are used to explain codes. It makes the codes, more readable and enables an on-looker to have an idea about the, program and functionality of particular lines of code., Python ignores the comments mentioned in a program., , Multi-line comments: For writing, multi-line comments, add ‘#’, before every line or include multiple lines within triple quotes (‘‘‘ ’’’)., , Keywords & Identifiers:, Keyword: A python keyword is a reserved word, which cannot be used, as the name of a variable, class, function, etc. These keywords have a, specific purpose in Python and should be used only in the way they, are intended., Here is a list of Python keywords:, , Variables:Variables can be thought as containers for storing data values., In Python, the variables are created when the values are assigned to them., , Note : For displaying the values of the variables do not use double, quotes. Doing so will print the variable name., , Variables in Python can change their data type simply by assigning the, variable within the data of a different data type. This will overwrite the, data already assigned to that variable., , Assigning Values to Multiple Variables: In Python we can assign, values to multiple variables in one line., Also we can assign some values to the variables using a single line., , Identifiers: Python identifiers are words that we use to specify or, declare variables. Any word that we use as a variable name is called, an identifier., The identifiers are case sensitive.
Page 5 :
Rules for Variable Names in Python:, 1. The name of the variable must start with a letter or an underscore., Correct Variables:B, _A, Name, NAME, _Name, etc., Incorrect Variables: 47X, (Name, #Name#, etc., 2. The variable name can only have alphanumerical characters and, underscores., Correct Variables:Age, a_g_e, a23_24, BB21, B_21_C, etc., Incorrect Variables: AG&*, re$, te@, etc., 3. The variable names are case sensitive., Age, AGE , aGE, age, AgE, etc. are different variables., Data Types: The three basic data type are:, , , , , int – Integer type:, 15, -5, 12, 3531 etc., float – Float (Decimal) type: 5.0, 2.3333334, -2.5, 69e7, 34E11 etc., str – String type: “Hello”, “23”, , Note: We cannot conduct mathematical operations on the str data type., , [PART -2], , Python Operators:, Operators are specific symbols in Python that are responsible for, arithmetic and logical computation., The value that the operator operates on is called the operands., a) Arithmetic Operators:, To used mathematical computation, Operators, , Name, , Example, , +, , Addition, , x+y, , -, , Subtraction, , x–y, , *, , Multiplication, , x*y, , /, , Division, , x/y, , //, , Floor division, , x // y, , %, , Modulus, , x%y, , **, , Exponentiation, , x ** y, , Commnet, Will return the sum of the, values, Will return the difference, of the values, Will return the product of, the values, Will return the quotient of, the division, Will return the integral, quotient of the division, Will return the remainder, of the division, Will return the exponential, of the value., , b) Conditional Operators:, To compare values. It returns either, True or False according to the condition.
Page 6 :
In this Python automatically converts one data type to another data type, without any user involvement., , c) Logical Operators: To used for logical computation., , 2. Explicit Type Conversion:, , In this, the users themselves convert the data type of an object to, another data type, which they require. Predefined functions, like, int(), float(), str(), etc. are used to perform explicit type conversion., , d) Assignment Operators: To assign values to variables., , Type Conversion:, The process of converting the value of one data type (integer,, string, float, etc.) to another data type is called type conversion., It has two types:, 1. Implicit Type Conversion, 2. Explicit Type Conversion, 1. Implicit Type Conversion:, , Statement and Expressions:, Statements are snippets of code that the interpreter can execute, directly., Expressions are a combination of variables, value, functions,, and/or operators that have to be evaluated by the interpreter, before the execution., , The input() Function:, The input() is an inbuilt function used for getting the user’s input.
Page 7 :
, , , , The ‘+’ symbol can also be used for combining two strings or, adding two numbers., , , , Python does not allow the use of ‘+’ character for combining a, string and number., , The interface provides space for the user to type in their input, as, shown., , Using input() Function to Enter numbers:, Note: The default data type for the input is the string data type., Here * cannot find product for given input as x is an string data, type., , , , , , , To tackle this problem and get the desired result, the input(), function can be manipulated as given below:, , , , For sorting out this problem, we have to change the type of, variable x temporarily for displaying it., It can be done by using the str() function., , More on print(): We can use the print() function for combining, text, and variables. For doing this, we use ‘+’ operator., , Python Data Structures:, , [PART -3]
Page 8 :
Python provides some powerful features of inbuilt data structures or, collections. The four main types of collections offered by Python are:, Lists: Ordered and changeable collection that allows duplicate, members, Tuple: Ordered and unchangeable collection that allows, duplicate members, Set: Unordered and unindexed collection that does not allow, duplicate members, Dictionary: Unordered, changeable and indexed collection, that does not allow duplicate members, List:, S.No., a), b), c), , d), e), f), , g), , Tasks, Creating Lists, Printing Lists, Accessing an, individual, items on the, lists, , Positive, Index, , print(List1[0]), print(List1[1]), , Negative print(List1[-1]), print(List1[-2]), Index, , Slicing a list, Changing the value of, Items in a list, Finding out the number, of Items in a List, Adding and, Deleting, Items from, a list, , Syntax/Command, List1 = [1, 2, 3], print(List1), , Addition, , Deletion, , print(List1[2:4]), or, print(List1[-2:-4]), , List1 = [1, 2, 3], List1[2]=4, print(len(List01)), List01.append(5), or, List01.insert(0,5), List01.remove(2), or, List01.pop(3), or, del List01[2]
Page 9 :
, , Tuples:, S.No., a), b), c), , d), e), f), , Tasks, Creating Tuples, Printing Tuples, Accessing an, individual, items on the, Tuples, , Positive, Index, , Syntax/Command, tuple1 = (1, 2, 3), print(tuple1), print(tuple1[0]), print(tuple1[1]), , Negative print(tuple1[-1]), print(tuple1[-2]), Index, , Slicing a Tuples, Changing the value of, Items in a Tuples, Deletion of Tuples, , print(tuple1[2:4]), or, print(tuple1[-2:-4]), , Tuples cannot be, modified., del tuple1
Page 10 :
, , Control Structures:, , Sets:, S.No., a), b), , , , Tasks, Creating Sets, Printing Sets, , Syntax/Command, set1 = {1, 2, 3}, print(set1), , , , , , Flow control enables us to define a flow in executing our code., Any Computer program can be written using the basic control, structures., A control structure is a block of code that analyses variables and, parameters to decide on a direction to continue the program., , Dictionaries:, S.No., a), b), , Tasks, Creating, Dictionaries, Printing, Dictionaries, , Syntax/Command, Dictionary_name={key01:value01,, key02:value02, key03:value03}, , print(Dictionary_name), , Conditional Statements:, 1. if Statement: It is executed only if the corresponding condition, evaluates to true. The piece of code inside this statement is skipped if the, condition evaluates to false., 2. else Statement: The condition in the if statement is first checked. If, it is true then if-block will executed. If the condition is false, the control, moves to the else statement., It is executed only if all the preceding control statements evaluate to false., Syntax, if, else, , Condition, True, False, False, , Action, Executed, Skipped, Moves to Next, condition
Page 11 :
3. if, elif and else Statement: A series of condition can be, defined by using, if for first condition, elif for rest condition, , , 1. for Loop: for loops are implemented to iterate over a collection of, items, such as lists or dictionaries, and run a block of code with each, item from the collection., range() is a very useful function that returns a series of, numbers in iterable form. Thus it can be used in for loops., , else for last condition., Syntax, if, elif, else, , Sequence, First Condition, Rest Conditions, Last Condition, , 3. while Loop: A while loop is executed until the loop condition, becomes false., , 4. Loops with else Clause: for and while loops have the option of, being clubbed with an else clause., The else clause is executed only after a for loop or while loop, terminates., , Loops: Loops allow us to repeat certain parts of the code multiple, times until the given condition is satisfied, without having to rewrite, that piece of code.
Page 12 :
5. Jump Statements: break and continue in Loops:, When a certain condition is met, and you want to come out of, the loop, you can use the break statement., , A continue statement is used to skip to the next iteration of, the loop, bypassing the rest of the current block. The loop does not, break and it still continues., , 6. Pass in Loop: The pass statement is used when there is no need, for any action to be taken or code to be executed but a statement, is still required to make the code syntactically correct. Hence, it is, used as a place holder., , Python Libraries and Packages:, Python packages are a set of Python modules, while Python libraries are, a collection of Python functions intended to perform specific tasks., There are more than 137,000 Python libraries and 198,826, packages available to simplify the developer’s regular programming, experience., NUMPY: It offers valuable support for various dimensional array, objects and matrices., Installing numpy:, To install this package,, 1. First open Anaconda Navigator and activate your working, environment., 2. Then type conda install numpy, 3. This will ask you, if you wish to proceed with the installation., 4. Type Y to proceed. The package will be installed in your selected, environment., In the Jupyter notebook, any package can be imported by writing, any of the following commands:, (Import the whole package), import numpy, (Import the whole package in the concerned file), import numpy as np, (Imports only the array functionality), from numpy import array, (Imports only the array functionality in the concerned file), from numpy import array as arr, OPENCV: Open Source Computer Vision is used for image processing., MATPLOTLIB: Matplotlib is used for making 2D graphs and plots. It, is also used to manipulate different characteristics of figures also., NLTK: Natural language Toolkit is used for natural language processing.
Page 13 :
PANDAS: It is a Python software package and widely used for Data, Science., PYGAME: It is a set of Python functions and classes; specifically, developed for developing video games.