1. Python is well-suited for AI projects due to its large library ecosystem, open source nature, and ability to handle complex applications. Popular AI frameworks like TensorFlow leverage Python.
2. Lists and tuples are mutable and immutable Python data types respectively. Lists use brackets while tuples use parentheses. Lists allow adding/removing elements while tuples do not.
3. Python supports various operations to access, modify, and delete elements from lists, including indexing, slicing, looping, del, remove(), pop(), append(), and extend().
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
159 views
Chapter 7 - Basics of Python in AI
1. Python is well-suited for AI projects due to its large library ecosystem, open source nature, and ability to handle complex applications. Popular AI frameworks like TensorFlow leverage Python.
2. Lists and tuples are mutable and immutable Python data types respectively. Lists use brackets while tuples use parentheses. Lists allow adding/removing elements while tuples do not.
3. Python supports various operations to access, modify, and delete elements from lists, including indexing, slicing, looping, del, remove(), pop(), append(), and extend().
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Chapter 7: Basics of Python for Use in AI Projects 1.
Python is one of the best choices for web
development for the large developer community for the following reasons: (a) Python has a vast library ecosystem that makes it easy for developers to build complex web applications quickly. Some popular web development frameworks like Django, Flask and Pyramid are built on top of Python, making it a powerful tool for web development. (b) Python is open-source, which means it is free to use and distribute, making it an attractive option for start-ups and small businesses. (c) Python is highly scalable, meaning it can handle large and complex web applications with ease. Many large- scale websites and web applications, such as Instagram and YouTube, have been built using Python. 2. The different set operations are: (a) Union: It is performed using | operator. The union of a and b is a set of all the elements from both the sets. For example, a = {0,1,2,3} b = {2,3,4,5} The union of a and b is {0,1,2,3,4,5} Teacher’s Manual AI–IX (Subject-specific Skills) 17 (b) Intersection: It is performed by & operator. The intersection of a and b is a set of elements that are common in both sets. For example, a = {0,1,2,3} b = {2,3,4,5} The intersection of a and b is {2,3} (c) Difference: It is performed using–operator. The difference of a and b (a – b) is a set of elements that are only in a but not in b. Similarly, b – a is a set of elements in b but not in a. For example, a = {0,1,2,3,4} b = {3,4,5,6,7} The difference of a and b is {0,1,2} (d) Symmetric difference: It is performed using ^ operator. The symmetric difference of a and b is a set of elements in both a and b except those that are common in both. For example, a = {1,2,3,4,5} b = {4,5,6,7,8} The symmetric difference of a and b is {6,7,8,1,2,3} 3. Python supports two types of data: Immutable and Mutable. Immutable data type: Their values cannot be changed. For example, Integer, Float, Complex, String, Tuple. (a) Numeric: It stores numeric values and supports three data types. (i) Integer: It can hold all the integer values, i.e., positive, negative and whole numbers. For example, 10, –200, 600000, etc. (ii) Float: It holds real numbers, for example, 3.666, 23456.67788, etc. (iii) Complex: These are the form of x + yj, where x and y are floats and j represents imaginary number which is the square roots of –1. For example, 19+6j. (b) String: It is a sequence of character, number or special characters enclosed in single or double quotes. For example, a = "Welcome to Python 3.9.2" (c) Tuple: Tuple is a sequence of elements of different data types enclosed in parentheses (). For example, Number = (1,2,3,4,5) Mutable data type: Their values can be changed. For example, List, Dictionary, sets. (d) List: List is a sequence of elements of different data types enclosed in square brackets. For example, List = [1,2,'a','Delhi',4.6] (e) Dictionary: It contains key-value pair where key is separated by colon ‘:’ and enclosed within curly brackets { }. The keys in dictionary are immutable and values mutable. For example, Dict = {1:'one',2:'Two',3:'Three',4:'Four'} 18 Teacher’s Manual AI–IX (Subject-specific Skills) 4. Mathematical or Arithmetic operators in Python are used to perform mathematical operations on values or variables. The mathematical operators in Python are: Operators Description Example(x=10;y=3) Addition (+) Used to add two values or variables. print(x+y) Output:13 Subtraction (–) Used to subtract one value or variable from another. print(x-y) Output:7 Multiplication (*) Used to multiply two values or variables. print(x*y) Output:30 Division (/) Used to divide one value or variable by another. print(x/y) Output:3.33 Floor Division (//) Used to divide one value or variable by another, but the result is rounded down to the nearest whole number. print(x//y) Output:3 Modulo (%) Used to find the remainder of a division operation. print(x%y) Output:1 Exponentiation (**) Used to raise a value or variable to a power. print(x**y) Output:1000 5. Differences between tuples and lists are: (a) Tuples are immutable, meaning once a tuple is created, it cannot be changed. Lists, on the other hand, are mutable, meaning you can add, remove or change elements in a list. (b) Tuples are defined using parentheses () while lists are defined using square brackets []. (c) Tuples are immutable and faster than lists when it comes to iteration and accessing elements. Example: # creating a tuple my_tuple=(1, 2, 3) # creating a list my_list=[4, 5, 6] 6. While loop is called entry-controlled loop as it first checks the condition and, if true, control will move inside the loop and execute the statements until the condition becomes false. This loop is used when we are not sure of the number of times the part of code needs to be executed. For example, count=0 while(count<=10): print(count) count=count+1 It will display numbers from 0 to 10. 7. Dictionary: A dictionary is a built-in data type that is used to store and retrieve key-value pairs. A dictionary is defined using curly braces {}. It contains key-value pair where key is separated by its value using colon ‘:’ and each key-value pair is separated by a comma. The keys are unique and immutable, while the values can be of any data type, such as strings, integers, lists or even other dictionaries. For example, Dict={1:'one', 2:'Two', 3:'Three', 4:'Four'} Teacher’s Manual AI–IX (Subject- specific Skills) 19 The different operations of dictionary are: (a) Accessing value from dictionary Dict = {1:'One', 2:'Two', 3:'Three'} print (Dict[2]) Output: 'Two'. (b) Changing values in the dictionary Dict = {'RollNo':1, 'Name':'Parul', 'Age':14, 'Class':'IX'} Dict['Age']=15 print(Dict) Output: {'RollNo':1, 'Name':'Parul', 'Age':15, 'Class':'IX'} 8. Python is the most preferred programming language used for AI for many reasons, such as: (a) Less code: AI projects involve lots of algorithms and Python supports pre-defined libraries to code these algorithms. (b) Pre-built libraries: Python has pre-built libraries to implement various Machine Learning and Deep Learning algorithms. For example, NumPy, Pandas, Keras, Tensorflow, Pytorch, etc. (c) Ease of learning: Python uses a very simple syntax that can be used to implement building a Machine Learning model. (d) Platform-Independent: Python can run on multiple platforms including Windows, macOS, Linux, etc., that helps transfer code from one platform to another. (e) Massive community support: Python has a huge community of users which is always helpful when encountering syntax problems and coding errors. 9. The main reasons for the rise in demand for AI are: (a) More computing power: AI projects require a lot of computing power in building AI models that involve heavy algorithms, data and use of complex neural networks. With the invention of GPUs, high-level computations and implementation of complex algorithms have become possible. (b) Data generation: Enormous amount of data has been generated over the years that needs to be interpreted, analyzed and processed by using AI tools. (c) More effective algorithms: AI adapts progressive learning algorithms to let the data do the programming and finds structure and regularities in data so that the algorithm acquires a skill. (d) Increased Investments: Since tech giants such as Google, Amazon, Netflix and Facebook have started investing in AI, it is gaining popularity for AI-based systems. 10. Ways to access elements from the list: In Python, there are various ways to access elements from a list: (a) Indexing: We can access individual elements of a list using their index, which starts at 0 for the first element. my_list=[10,20,30,40,50] print(my_list[0]) [10] (b) Slicing: You can access a subset of elements from a list using slicing. Slicing allows you to specify a range of indices to include in the subset. my_list=[10,20,30,40,50] print(my_list[0:3]) [10, 20, 30] 20 Teacher’s Manual AI–IX (Subject-specific Skills) (c) Looping: You can iterate over all the elements of a list using a for loop. my_list=[10,20,30,40,50] for item in my_list: print(item) 10 20 30 40 50 11. Different methods to delete elements in a list: (a) Using the del keyword: We can use the del keyword followed by the index of the element we want to delete and we can also delete the whole list by specifying the list name. my_list=[10,20,30,40,50] del my_list[2] #will delete the third element from the list my_list. print(my_list) [10,20,40,50] my_list=[10,20,30,40,50] my_list[2:5]=[] It will remove the elements from index 2 to index 4 (inclusive) from the list my_list. print(my_list) [10,20] my_list=[10,20,30,40,50] del(my_list) print(my_list) NameError: name 'my_list' is not defined (b) Using the remove() method: We can use the remove() method to delete the first occurrence of a specific value in the list. my_list.remove(30) will remove the first occurrence of the value 30 from the list my_list. my_list=[10,20,30,40,50] my_list.remove(30) print(my_list) [10,20,40,50] (c) Using the pop() method: We can use the pop() method to remove an element from the list by its index and also retrieve its value. If no index is provided, it removes and returns the last item from the list. my_list.pop(2) will remove the third element from the list my_list and return its value. my_list=[10,20,30,40,50] my_list.pop(2) 30 Teacher’s Manual AI–IX (Subject-specific Skills) 21 12. In Python, both the append() and extend() methods are used to add elements to a list but they have some differences in their functionality. append() method adds a single element to the end of a list. fruits=['apple', 'banana', 'orange'] fruits.append('kiwi') print(fruits) ['apple', 'banana', 'orange', 'kiwi'] extend() method is used to add multiple elements to the end of a list. fruits = ['apple', 'banana', 'orange'] fruits.extend(['kiwi', 'mango']) print(fruits) ['apple', 'banana', 'orange', 'kiwi', 'mango'