0% found this document useful (0 votes)
66 views

Pds Imp

python with data science important questions with solution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
66 views

Pds Imp

python with data science important questions with solution
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 60
Python for Data Science ( 3150713) Question Bank for Reference [2023-24] Module 1: (Weightage = 20%) Chapter ; Overview of Python and Data Structures 1. Explain different features of Python language. Python is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming. Features in Python There are many features in Python, some of which are discussed below as follows: 1. Free and Open Source Python language is freely available at the official website and you can download it from the given download link below click on the Download Python keyword. Download Python Since it is open-source, this means that source code is also available to the public. So you can download it, use itas well as share it, 2. Easy to code Python is a high-level proaramming language. Python is very easy to learn the language as compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in the Python language and anybody can learn Python basics in a few hours or days. Itis also a developer-friendly language. 3. Easy to Read As you will see, learning Python is quite simple. As was already established, Python's syntax is really straightforward. The code block is defined by the indentations rather than by semicolons or brackets. 4, Object-Oriented Language One of the key features of Python is Object-Oriented programming. Python supports object-oriented language and concepts of classes, object encapsulation, ete. 5. GUI Programming Support Graphical User interfaces can be made using a module such as PyOt5, PyQt4, wxPython, or Tk in Python. PyQtS is the mast popular option for creating graphical apps with Python. 6. High-Level Language Python is a high-level language. When we write programs in Python, we do not need to remember the system architecture, nor do we need to manage the memory. 7. Large Community Support Python has gained popularity over the years. Our questions are constantly answered by the enormous StackOverflow community. These websites have already provided answers to many questions about Python, so Python users can consult them as needed. 8. Easy to Debug Excellent information for mistake tracing, You will be able to quickly identify and correct the majority of your program's issues once you understand how to interpret Python's error traces. Simply by glancing at the code, you can determine what it is designed to perform. 9. Python is a Portable language Python language is also a portable language. For example, if we have Python code for Windows and if we want to run this code on other platforms such as Linux, Unix, and Mac then we do not need to change it, we can run this code on any platform. 10. Python is an Integrated language Python is also an Integrated language because we can easily integrate Python with other languages like C, Co, ete. 11. Interpreted Language: Python is an Interpreted Language because Python code is executed line by Line at a time. like other languages C, C++, Java, etc. there is no need to compile Python code this makes it easier to debug our code. The source code of Pythan is converted into an immediate form called bytecode, O seconds of 15 secondsVolume 0% 12. Large Standard Library Python has a large standard library that provides a rich set of modules and functions so you do not have to write your own code for every single thing. There are many libraries present in Python such as reqular expressions, unit-testina, web browsers, etc. 13. Dynamically Typed Language Python is a dynamically-typed language. That means the type (for example- int, double, long, etc) for a variable is decided at run time not in advance because of this feature we don't need to specify the type of variable, 14, Frontend and backend development With a new project py script, you can run and write Python codes in HTML with the help of some simple tags , , ete. This will help you do frontend development work in Python like javascript. Backend is the strong forte of Python it’s extensively used for this work cause of its frameworks like Diango and Flask. 15. Allocating Memory Dynamically In Python, the variable data type does not need to be specified. The memory is automatically allocated to a variable at runtime when itis given a value. Developers do not need to write int y = 18 if the integer value 15 is set to y. You may just type y=18. 2. Why is Python called a dynamic typed language? When we declare a variable in C or alike languages, this sets aside an area of memory for holding values allowed by the data type of the variable. The memory allocated will be interpreted as the data type suggests. If it's an integer variable the memory allocated will be read as an integer and so on, When we assign or initialize it with some value, that value will get stored at that memory location. At compile time, initial value or assigned value will be checked. So we cannot mix types. Example: initializing a string value to an int variable is not allowed and the program will not compile. But Python is a dynamically typed language. It doesn't know about the type of the variable until the code is run, So declaration is of no use. What it does is, It stores that value at some memory location and then binds that variable name to that memory container. And makes the contents of the container accessible through that variable name. So the data type does not matter, As it will get to know the type of the value at run-time. 4 This will store 6 in the memory and binds the #name x toit. After it runs, type of x will abe int. x print(type(x)) 4# This will store ‘hello’ at some location int 4 the memory and binds name xtoit. After it suns type of x will be str. x= ‘hello’ print(type(x)) Output: 3. Explain different data types available in python. Ansin ppt 4, Explain List and it’s any six operations. (with syntax) List in Python is similar to arrays in C/Java * Array is a fixed size storage used to store elements of same data type « List can store differenttypes of elements List can grow dynamically in memory © They are represented in [ ] wherein each elements are separated by comma (,).. 1 append() —_—_ Used for appending and adding elements to the end of the List. 2 copy()It returns a shallow copy of a list 3 clear() This method is used for removing all items from the list. 4 count() These methods count the elements 5 extend() Adds each element of the iterable to the end of the List 6 index() Returns the lowest index where the element appears. 7 insert() Inserts a given element at a given index in a list. 8 pop() Removes and returns the last value from the List or the given index value. 9 remove() _ Removes a given object from the List. 10 reverse() —_Reverses objects of the List in place. 11 sort() Sorta List in ascending, descending, or user-defined order 12 min() Calculates the minimum of all the elements of the List 13 max() Calculates the maximum of all the elements of the List $. Explain tuple and its operations. (with syntax) Similar to list, © Can group elements of different data types, enclosed in ( ) and separated by commas (,) © Unlike List, tuples are not modifiable A tuple is a collection of objects that are ordered and immutable. In Python, tuples are sequences, just like lists. The main difference between tuples and lists is that lists are mutable, meaning they can be changed after they are created, while tuples are immutable, meaning they cannot be changed after they are created. Here is an example of creating a tuple: my_tuple = (1, 2, 3) You can access elements of a tuple by their index, just like you can with a list: print(my_tuple{0}) # Output: 4 Tuples also support operations like concatenation, repetition, and slicing: # Concatenation t= (1,2, 3) 4, 5, 6) 11+ (2 # Output: (1, 2, 3, 4, 5, 6) # Repetition 3’) 4 * 3 # Output: ('a''b’, ‘a’, ''b’) # Slicing In addition to these operations, tuples also support the following methods: - ‘count(value)’: Returns the number of times a specified value appears in the tuple. - ‘index(value)’: Returns the index of the first occurrence of a specified value in the tuple. Here is an example of using the ‘count’ method: my_tuple = (1, 2, 3, 2, 3) print(my_tuple.count(2)) # Output: 2 And here is an example of using the ‘index’ method: my_tuple = (1, 2, 3, 2, 3) print(my_tuple.index(3)) # Output: 2 In summary, tuples are a collection of ordered, immutable objects in Python. They support various operations and methods 6. Explain the set and its six operations. (with syntax) Set is an unordered collection of elements much like a set in Maths © Elements may not appear in the same order as they are entered into the set © Does not accept duplicate elements © There are 2 subtypes: 1. Set Datatype 2. Frozen Set Datatype The most common way of creating a set in Python is by using the built-in set() function. You can also create sets using the curly brace {} syntax: 1. Set: A setis a collection of unique elements. Each element in a set is unique and cannot be duplicated. 2. Set Operations: a. Union: The union of two sets is a set that contains all the elements from both sets. sett = (1,2, 3} set2 = {3, 4, 5} union_set = sett union(set2) print(union_set) # Output: {1, 2, 3, 4, 5} b. Intersection: The intersection of two sets is a set that contains only the elements that are common to both sets. sett = (1, 2, 3} set2 = {3, 4, 5} intersection_set = sett .intersection(set2) print(intersection_set) # Output: {3} ¢. Difference: The difference of two sets is a set that contains only the elements that are in the first set but not in the second set. sett = (1,2, 3} set2 = (3, 4, 5} difference_set = set! difference(set2) print(difference_set) # Output: (1, 2} d. Symmetric Difference: The symmetric difference of two sets is a set that contains only the elements that are in exactly one of the two sets. sett = {1, 2, 3} set2 = (3, 4, 5} symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # Output: {1, 2, 4, 5) e. Subset: A set is a subset of another set if all its elements are in the other set. sett = (1,2, 3} set2 = (1, 2,3, 4, 5) print(set .issubset(set2)) # Output: True . Superset: A set is a superset of another set if all the elements of the other set are in the set. sett = {1, 2, 3, 4, 5} set2 = {1, 2, 3} print(sett issuperset(set2)) # Output: True 7. Explain the dictionary and its six operations. (with syntax) A dictionary is a built-in data type in Python that stores data in key-value pairs. The keys are unique and the values can be of any type. Dictionaries are mutable, meaning that they can be modified after they are created The following are the six operations that can be performed on a dictionary: 1. **Creation:** A dictionary can be created using the curly braces ‘{}° syntax. The keys and values are separated by a colon *:", and each key-value pair is separated by acomma *,”, For example, the following code creates a dictionary with three key-value pairs: my_dict = (name': ‘John Doe’, ‘age’: 30, ‘city’: ‘New York’) 2. **Access:** The value of a specific key can be accessed using the square bracket “IT syntax. For example, the following code prints the value of the “name” key: print(my_dict['name']) # Output: John Doe 3. *Modification:** The value of a specific key can be modified using the square bracket ‘[}' syntax. For example, the following code changes the value of the ‘age” key to 34 my_dictf'ag 4. “Deletion:** A key-value pair can be deleted from a dictionary using the “del” keyword. For example, the following code deletes the ‘city’ key-value pair from the dictionary: del my_dictf'city'] 5. **Iteration:** The keys, values, or key-value pairs of a dictionary can be iterated over using the *keys()’, ‘values()’, and ‘items()’ methods, respectively. For example, the following code prints the keys of the dictionary: for key in my_dict.keys() print(key) # Output: name, age 6. **Membership:** The ‘in’ operator can be used to check if a key exists in a dictionary. For example, the following code checks if the ‘name’ key exists in the dictionary: if ‘name’ in my_dict: print(‘The name key exists in the dictionary.’) # Output: The name key exists in the dictionary. 8. Differentiate List and tuple (with syntax) LIST TUPLE 1 Lists are mutable 2 The implication of iterations is Time-consuming The list is better for performing 3 operations, such as insertion and deletion. 4 Lists consume more memory 5 Lists have several built-in methods 6 Unexpected changes and errors are more likely to occur Python List vs Python Tuple Test whether tuples are immutable and lists are mutable Tuples are immutable The implication of iterations is comparatively Faster A Tuple data type is appropriate for accessing the elements Tuple consumes less memory as compared to the list Tuple does not have many built-in methods. Ina tuple, it is hard to take place. Here we are going to compare the list and tuple mutability tests, Python3 # Creating a List with # the use of Numbers # code to test that tuples are mutable List = (1, 2,4, 4, 3, 3, 3, 6, 5] print(*Original list ", List) List[3] = 77 print("Example to show mutability *, List) Output Original list [1, 2, 4, 4, 3, 3, 3, 6, 5] Example to show mutability [1, 2, 4, 77, 3, 3, 3, 6, 5] ‘We can see here tuple can not be modified. Python3 # code to test that tuples are immutable tuplet = (0, 1, 2, 3) tuple1[0] = 4 print(tuple1) Output: Traceback (most recent call last): File "eQeaddff843a8695575daec345061126.py’, line 3, in tuplet[o]=4 TypeError: ‘tuple’ object does not support item assignment 9. Differentiate mutable and immutable data types in Python (with syntax) MutabletmmutableDefinitionData type whose values can be changed after creation, Data types whose values can't be changed or altered. Memory LocationRetains the same memory location even after the content is modified Any modification results in a new object and new memory location ExampleList, Dictionaries, SetStrings, Types, Integer Performancelt is memory-efficient, as no new objects are created for frequent changes.It might be faster in some scenarios as there's no need to track changes. Thread-SafetyNot inherently thread-safe. Concurrent modification can lead to unpredictable results.They are inherently thread-safe due to their unchangeable nature Use-casesWhen you need to modify, add, or remove existing data frequentlyWhen you want to ensure data remains consistent and unaltered. 10, Explain looping structures in Python (with syntax) In Python, looping structures are used to repeatedly execute a block of code. There are three types of loops in Python: ‘for’, ‘while’, and ‘nested loops’. 1. ‘for’ loop This loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. The syntax for a ‘for’ loop is as follows: for variable in sequence: # code to be executed For example, the following code prints the numbers from 0 to 4: for iin range(5): print(i) 2. ‘while’ loop: This loop is used to repeatedly execute a block of code as long as a given condition is true. The syntax for a ‘while’ loop is as follows: while condition: # code to be executed For example, the following code prints the numbers from 0 to 4: i=0 while i < 5: print(i) i+s4 3. Nested loops: Nested loops are loops inside other loops. They are used to iterate over multiple sequences or to perform operations that require multiple iterations. The syntax for a nested loop is as follows: for variable1 in sequence1 for variable2 in sequence2: # code to be executed For example, the following code prints a multiplication table for numbers from 1 to 5: for iin range(1, 6) for j in range(1, 6): print(i* j, end="\t) print() In this example, the outer ‘for’ loop iterates over the range of numbers from 1 to 5, and for each number, the inner ‘for’ loop iterates over the same range of numbers. The ‘print’ statement inside the inner loop prints the product of the current numbers from the outer and inner loops. These are the basic looping structures in Python. They allow you to control the flow of your program and perform operations that require repetition.', and more based on your class's requirements. 21, Explain polymorphism and inheritance. Polymorphism and inheritance are two fundamental concepts in object-oriented programming (OOP) that help you create more flexible and maintainable code by promoting code reuse and abstraction. Let's explain both concepts: 1. Inheritance: Inheritance is a key feature of object-oriented programming that allows you to create a new class (subclass or derived class) based on an existing class (superclass or base class). The derived class inherits the properties (attributes) and behaviors (methods) of the base class, enabling you to reuse and extend the functionality of the base class. Key points about inheritance: - The base class defines a common set of attributes and methods that can be shared by multiple derived classes. - A derived class can add its own attributes and methods, as well as override or extend the behavior of the base class methods. - Inheritance promotes code reusability, as you can create new classes that build upon existing classes, reducing duplication of code. Example of inheritance in Python “python class Animal def _ init__(self, name) seif.name = name def speak(selt): pass class Dog(Animal) def speak(selt): return "Woof!" class Cat(Animal): def speak(selt): return "Meow!" # Create instances dog = Dog("Buddy") cat = Cat("Whiskers") print(f'{dog.name} says {dog.speak()}") # Output: "Buddy says Woof!" print(P{cat.name} says {cat.speak()}") # Output: "Whiskers says Meow!" In this example, ‘Dog’ and "Cat’ are subclasses of the ‘Animal’ superclass, inheriting the ‘name’ attribute and overriding the “speak’ method to provide their own implementations. 2. Polymorphism: Polymorphism is the ability of different objects to respond to the same method or function call in a way that is specific to their individual types. It allows you to work with objects of different classes in a unified way, simplifying code and making it more flexible Key points about polymorphism: - Polymorphism is achieved through method overriding, where different classes provide their own implementations of the same method. - It allows you to write code that can work with a range of objects without needing to know their specific types. - Polymorphism promotes code flexibility and extensibility, as you can add new classes that conform to existing interfaces without modifying existing code. Example of polymorphism in Python: “python def animal_sound(animal): return animal.speak() # Create instances dog = Dog("Buddy") cat = Cat("Whiskers") # Use the same function with different objects print(f'{dog.name} says {animal_sound(dog)}") # Output: "Buddy says Woof!" print(f*{cat.name} says {animal_sound(cat)}") # Output: "Whiskers says Meow!" In this example, the ‘animal_sound’ function can accept different objects of the *Animal’ class hierarchy (polymorphism) and call the ‘speak’ method on each of them, producing the appropriate sound based on the specific object's type. Polymorphism and inheritance are powerful OOP concepts that help you create modular, extensible, and maintainable code by allowing you to build on existing code and work with objects in a more abstract and flexible manner. 22, Implement a Python program to display factorial of a given number n. # Python 3 program to find # factorial of given number def factorial(n): # single line to find factorial return 1 if (n==1 or n==0) else n * factorial(n - 1) # Driver Code num = 5 print("Factorial of",num,"is” factorial(num)) Here's the step-by-step explanation of the program: The function "factorial" is defined. It takes one argument, "n", which is the number for which we want to find the factorial. Inside the function, we have a recursive call to the “factorial” function itself. This is the core of the algorithm. The base case of the recursion is when "n" equals 1 or 0, in which case the function retums 1. Otherwise, the function returns the product of "n" and the factorial of "n-1" In the driver code, we define a variable "num" and assign it the value 5. Then, we call the "factorial" function with "num" as the argument. The factorial of 5 is calculated using the recursive calls. When the calculation reaches the base case, the recursion unwinds, and the final result is returned and printed. 23, Implement a python program to create a list of n numbers given by the user. Print only even indexed numbers. def create_list(n): # Creating a list of n numbers given by the user num_list = {] for i in range(n): num = int(input("Enter number * + str(i) +": ")) num_list.append(num) return num_list def print_even_indexed_numbers(num_list) # Print only even indexed numbers print("Even indexed numbers: ", end="") for iin range(0, len(num_list), 2) print(num_list{i], end="") print() # Driver code n= int(input("Enter the number of elements: ")) num_list = create_list(n) print_even_indexed_numbers(num_list) We define a function called create_list(n) that takes an integer n as an argument. This function creates a list of n numbers by iterating over the range from 0 to n-1. For each iteration, it prompts the user to enter a number, which is then appended to the num_list. We define another function called print_even_indexed_numbers(num_list). This function takes a list num_list as an argument. It iterates over the num_list with a step of 2, which means it will only visit even indexed numbers in the list. For each visited number, it is printed out In the driver code, we first prompt the user to enter the number of elements they want to create in the list. This value is then passed to the create_list(n) function to create the list After the list is created, it is passed to the print_even_indexed_numbers(num_list) function to print out only the even indexed numbers in the list. 24, Implement a Python program to display the first n fibonacci series. Follow the below steps to Implement the idea: © Build a recursive function that takes integer N as a parameter. © If N=0 fibonacci number will be 0. @ Else if n = 1 Fibonacci number will be 1 © Else return value for func(n-1) + func(n-2) # python code to print first n fibonacci numbers def fibonacci_numbers(num): if num return 0 elif num return 4 else: # printing fibonacci numbers return fibonacci_numbers(num-2)+fibonacci_numbers(num-1) n=7 for iin range(0, n): print(fibonacci_numbers(i), end= Output 0112358 Time Complexity: O(n*2n) Auxiliary Space: O(n), For recursion call stack. 25, Demonstrate positive and negative indexing operations on arrays. Python provides the ability to access array elements using both positive and negative indexing. Here is an example demonstrating both: python Download Copy code 1# Creating an array 2arr =[1, 2, 3, 4, 5] 3 4# Accessing elements using positive indexing Sprint(arr[0}) # Output: 1 6print(arr{2}) # Output: 3 7print(arr{4]) # Output: 5 8 9# Accessing elements using negative indexing 10print(arr[-1}) # Output: 5 11print(arr{-3}) # Output: 3 42print(arr[-5]) # Output: 4 In this example, the positive index values are used to access the elements from the beginning of the array. The negative index values are used to access the elements from the end of the array. The indexing operation starts from 0, so arr{0] represents the first element of the array, and arr[-1] represents the last element of the array. 26. Demonstrate slicing operation on string. Slicing in Python is a feature that allows us to retrieve specific parts of an object like strings, lists, and tuples. We can define a sequence of items to be sliced, starting from a specific index, and ending at a specified index. Here is an example demonstrating slicing operation on a string: python Download Copy code 1# Original string 2original_string = “Hello, World!" 3 4# Slicing operation 5sliced_string = original_string[0:5] 6 7# Printing the result 8print("The sliced string is:", sliced_string) In this example, the original string is "Hello, World!". The slicing operation [0:5] extracts the substring from index 0 (inclusive) to index 5 (exclusive). Therefore, the sliced string is "Hello". 27. Explain membership operators with examples Membership operators in Python are used to check whether a specific element is present within a sequence (like a string, list, or tuple). There are two membership operators: ‘in’ and ‘not in’. These operators return a Boolean value (“True’ or *False’) based on whether the specified element is found in the sequence. Let's explore both membership operators with examples: Operator: The ‘in’ operator checks if a specific element is present in a sequence and returns “True” if the element exists in the sequence; otherwise, it returns ‘False’. Example using ‘in’ operator: python fruits = ['apple", "banana’, "cherry", "date"] # Check if "banana" is in the list of fruits if "banana" in fruits: print("Yes, ‘banana’ is in the list.") else: print("No, ‘banana’ is not in the list.") Output Yes, ‘banana’ is in the list. In this example, the ‘if "banana’ in fruits’ condition checks if the string "banana’ is present in the list ‘fruits’, and it evaluates to “True” 2. ‘not in’ Operator: The ‘not in’ operator checks if a specific element is not present in a sequence and returns ‘True’ if the element does not exist in the sequence; otherwise, it returns “False” Example using ‘not in’ operator: python colors = blue", "yellow"] "red", “green” # Check if "purple” is not in the list of colors if "purple" not in colors: print("Yes, ‘purple’ is not in the list.") else: print("No, ‘purple’ is in the list.”) Output Yes, ‘purple’ is not in the list. In this example, the “if "purple" not in colors’ condition checks if the string "purple" is not present in the list ‘colors’, and it evaluates to “True”. Membership operators are useful for conditional statements, loop control, and searching within sequences to determine the presence or absence of specific elements 28. Explain identity operators with examples. In Python, identity operators are used to compare the memory addresses (identities) of two objects. There are two identity operators: ‘is’ and ‘is not’. These operators return “True” if the objects on both sides of the operator refer to the same object in memory and ‘False’ otherwise. Identity operators are often used to check if two variables reference the same object. Let's explore both identity operators with examples: 1. ‘is’ Operator: The ‘is’ operator checks if two variables or objects reference the same memory location. If they do, it returns ‘True’; otherwise, it retums "False. Example using the ‘is’ operator: “python x=[1,2,3] yax # Check if x and y refer to the same object ifxisy: print("x and y refer to the same object.") else: print("x and y do not refer to the same object.") Output x and y refer to the same object. In this example, the *is* operator is used to check if x’ and °y’ refer to the same list object in memory. Since "y’ was assigned the same list as x’, they do refer to the same object. 2. “is not’ Operator: The “is not’ operator checks if two variables or objects do not reference the same memory location. If they do not, it returns ‘True’; otherwise, it retums ‘False. Example using the ‘is not’ operator: “python a=[1,2,3] b=[1,2,3] # Check if a and b do not refer to the same object if ais not b: print("a and b do not refer to the same object.") else: print("a and b refer to the same object.") Output: a and b do not refer to the same object. In this example, the ‘is not’ operator checks if ‘a’ and “b* do not refer to the same list object in memory. Even though the lists have the same values, they are distinct objects in memory. Identity operators are primarily used to compare objects’ identities, which can be helpful in cases where you want to ensure that two variables reference the same object, especially when dealing with mutable objects like lists, dictionaries, or custom objects. 29, Explain input and print function. The ‘input()’ and ‘print()’ functions are two fundamental builtin functions in Python that are commonly used for interacting with users and displaying output. Let's explain each of them: input()’ Function: The ‘input()’ function is used to receive input from the user through the keyboard, It allows you to prompt the user for information, which is typically entered as text. The function takes an optional argument, which is the prompt or message displayed to the user before they provide input. The ‘input()’ function returns the user's input as a string. Here's an example of how to use the ‘input()’ function: ~ python user_name = input("Please enter your name: ") print("Hello, " + user_name +"! Welcome to Python. In this example, the “input()° function prompts the user to enter their name, and the entered name is stored in the ‘user_name’ variable. The program then greets the user using the entered name. 2. ‘print()’ Function: The ‘print()’ function is used to display output to the console or terminal. It can take one or more arguments, which can be variables, values, or expressions, The function converts these arguments into strings and then displays them in the console. By default, ‘print()’ separates the output with spaces and adds a newline character at the end. Here's an example of how to use the ‘print()’ function: age = 30 print("Name:", name, "Age:", age) In this example, the ‘print()’ function is used to display the name and age variables along with labels. You can format the output using different methods, such as f strings, the *strformat()’ method, or by specifying the ‘sep’ and ‘end’ parameters in the “print()° function to control the separator between values and the ending character (e.g., newline or space) Here's an example using f-strings: “python name = "Bob" age = 25 print(f'Name: {name}, Age: {age}") Both the ‘input()° and ‘print()’ functions are essential for building interactive Python programs, taking user input, and displaying results to the user or in the console, making them fundamental for many applications and scripts. 30. Explain type casting in python. Type casting, also known as type conversion, is a way to convert an object of one data type to another. In Python, type casting can be done using built-in functions such as int(), float(), str(), etc. For example, if you have a string that represents a number, you can convert it to an integer using the int() function python Download Copy code 4num_str = "42" 2num_int = int(num_str) 3print(num_int) # Output: 42 Here, the int() function takes a string as an argument and converts it to an integer. It's important to note that type casting is not the same as type coercion, which Python performs automatically in some cases. Type casting involves explicitly converting a value from one data type to another, while type coercion is performed by Python when it can infer that it's the right thing to do. Here's an example to demonstrate the difference between type casting and type coercion python Download Copy code 4num_str = "42" 2num_int = int(num_str) # Type casting: explicitly converting string to integer 3print(num_int) # Output: 42 4 Snum_str = "42" 6num_int integer for multiplication 7print(num_int) # Output: 84 \um_str * 2 # Type coercion: Python automatically converts string to In the second example, when we multiply the string "42" by 2, Python automatically converts the string to an integer and performs the multiplication. This is an example of type coercion, where Python performs the conversion implicitly without requiring explicit instructions from the programmer 31, Differentiate C and Python. An Imperative programming model is basically followed by C. Variables are declared in C. C doesn't have native OOP. Pointers are available in C language Cis a compiled language. There is a limited number of built-in functions available in C. Implementation of data structures requires its functions to be explicitly implemented. Cis compiled directly to machine code which is executed directly by the CPU Declaring of variable type in C is a necessary condition Python An object-oriented programming model is basically followed by Python. Python has no declaration Python has OOP which is a part of the language. No pointers functionality is available in Python. Python is an interpreted language. There is a large library of builtin functions in Python. Itis easy to implement data structures in Python with built-in insert, append functions. Python is firstly compiled to a byte-code and then it is interpreted by a large C program. There is no need to declare a type of variable in Python, C does not have complex data structures. Cis statically typed. Syntax of C is harder than python because of which programmers prefer to use python instead of C C programs are saved with .c extension An assignment is allowed in a line. In C language testing and debugging is harder. Cis complex than Python. The basic if statement in cis represented as: if) The basic if-else statement in C is represented as: if OL } else { } C language is fast. C.uses {} to identify a separate block of code Python has some complex data structures. Python is dynamically typed. Itis easy to learn, write and read Python programs than C. Python programs are saved by .py extension, The assignment gives an error in line. For example, a=5 gives an error in python. In Python, testing and debugging are directly not harder than in C. Python is much easier than C. The basic if statement in Python is represented as: ift The basic if-else statement is represented as: if: else: Python programming language is slow Python uses indentation to identify separate blocks of code. Itis not mandatory to mark the end of every statement with a semicolon in Python. Itis mandatory to mark the end of every statement with a semicolon in C. 32. Explain range() with suitable examples. The ‘range()’ function in Python is used to generate a sequence of numbers. It's often used in ‘for’ loops to iterate over a range of numbers. The ‘range()° function takes up to three arguments: ‘start’, ‘stop’, and ‘step’. It returns an iterable sequence of numbers that starts at ‘start’, increments by ‘step’ (default is 1), and stops just before reaching the ‘stop’ value. Here's the basic syntax of the ‘range()" function: ~python range({start], stop, [step]) Now, let's explore some examples of using the “range()' function: 1. Creating a simple range: You can create a range from a starting value to a stopping value. By default, the ‘start’ value is O and the ‘step’ is 1 “python for jin range(5): print() Output PRONTO 2. Specifying a start and stop value: You can specify the ‘start’ and ‘stop" values to define a custom range. yython for iin range(2, 8): print(i) Output pNOTARON 3. Adding a step value: You can also specify a “step” value to determine the increment between numbers in the range yython foriin range(1, 10, 2) print(i) Output: PON aw 4. Creating a list from a range: You can convert a “range’ object into a list using ‘list()". This is useful if you want to store the range in a list for later use. “python my_range = range(3, 10) my list = list(my_range) print(my_list) Output 13,4, 5,6, 7, 8, 9] 5. Using ‘range()' in a ‘for’ loop: “range()’ is often used in ‘for’ loops to iterate over a range of values. “python for num in range(1, 6) print(*Square of {num} is {num ** 2}") Output Square of 1 is 4 Square of 2 is 4 Square of 3 is 9 Square of 4 is 16 Square of 5 is 25 The ‘range()’ function is a versatile tool for creating sequences of numbers for various purposes, such as looping, generating indices, or creating lists of values within a specified range. Module 2: (Weightage = 15%) 33. Define Data Science Data science is an interdisciplinary field that © Uses scientific methods, processes, algorithms and systems © To extract knowledge and insights from noisy, structured and unstructured data, and © Apply knowledge from data across a broad range of application domains. Data science is related to data mining, machine learning and big data. “Data Science is the domain of study that deals with vast volumes of data using modern tools and techniques to find unseen patterns, derive meaningful information, and make business decisions.” 34, Why Data Science is considered as amazing field of study? now, data science is one of those occupations in which “the more you leam, the more you want to learn’ © Answering one question often spawns more questions that are more interesting than the one just answered * Reasons why data science is an amazing field of study: o Considering the emergence of data science © Outlining the core competencies of a data scientist © Linking data science, big data and Al © Understanding the role of programming Considering the Emergence of Data Science: Data Science has emerged as an amazing field of study due to the rapid growth of digital data in recent years. With the advent of the internet, social media, loT devices, and more, massive amounts of data are generated daily. This proliferation of data has created a need for experts who can harness this information and derive insights from it. Data science serves as the bridge between raw data and actionable knowledge, making it an essential discipline in the information age Outlining the Core Competencies of a Data Scientist: Data scientists possess a unique combination of skills that include statistical analysis, data manipulation, data visualization, domain expertise, and problem-solving abilities. They excel in understanding complex datasets, formulating hypotheses, and using statistical methods to extract meaningful patterns and trends. Their ability to communicate these insights effectively to non-technical stakeholders is a crucial competency. This multifaceted skill set makes data science an exciting field for those who want to develop a broad range of capabilities. Linking Data Science, Big Data, and Al: Data Science is closely linked to Big Data and Artificial Intelligence (Al). Big Data technologies enable the storage and processing of vast datasets, while Al techniques, particularly machine learning, allow data scientists to build predictive models and automate decision-making processes. Data science integrates these components to unlock the full potential of data. This connection to emerging technologies like Aland Big Data positions data science as a field at the forefront of innovation. Understanding the Role of Programming: Programming is a fundamental aspect of data science. Data scientists use programming languages like Python, R, and SQL to manipulate data, create algorithms, and develop machine learning models. The ability to code not only enables data scientists to work with data efficiently but also allows them to customize and implement various data analysis techniques. Programming skills are essential for data scientists to handle the technical aspects of their work, making it a field that appeals to those who enjoy coding and logical problem-solving 35, Outline the core competencies of data scientist. Outlining the core competencies of a data scientist © Data scientists requires knowledge of a broad range of skills to perform the required tasks. © Following are the areas in which a data scientist could excel 1. Data capture 2. Analysis 3. Presentation * Outlining the core competencies of a data scientist: (...) © Following are the areas in which a data scientist could excel: (...) 1. Data capture: * Skills for obtaining proper data for analysis : The act of capturing data begins by managing a data source using database management skills * Skills for understanding data domain : Understanding raw data and its domain, and begin formulation of questions to be answered « Skills for data modelling: Understand how the data is connected and whether the data is structured 2. Analysis: * Once the data is obtained and complexities of the data is understood, apply statistics, specialized math tricks, algorithms to find patterns in the data that can help in drawing conclusions 3. Presentation: *# Provide graphical presentation of identified patterns to help others visualize what the numbers mean and how to apply them in a meaningful way . i.e. impact of data should not be lost 36. Explain data science pipeline. The Data Science pipeline refers to the process and tools used in preparation, analysis and presentation of the data © Steps are: © Preparing the data Performing exploratory analysis © Learning from data © Visualizing © Obtaining insights and data products Step 1: Preparing the data © Data comes from different sources i.e. not ready for analysis ‘© Raw data need to be transformed to make it cohesive and amenable for analysis, © Transformation may require: m= Changing data types = Changing order in which data appears = Creation of data entries based on the information provided by existing entries Creating the Data Science Pipeline: (...) ‘© Step1:Preparing the data (..) #Preparing the data import pandas as pd import numpy as np df = pd.DataFrame({"Name” : ["Nitya","Suraj", "Shreya", "Shourya"], "age" : [34,np.nan,36,35]}) #Filling missing enteries df["Age"] = df["Age"].fillna(df["Age"].mean()) print (d#) ane 2° Nitya 1 Suraj 3. shourya Creating the Data Science Pipeline: ( ‘* Step1:Preparing the data (..) #Changing data type if = df.astype({"Age" : int}) print(df) sma) Fes #Sorting data @ Nitya 34 df. sort_values(by=["Age"]) 1 Suraj 35 2 3 Shreya 36 vane age Shourya 35 o Nive 34 41 Suraj 35 3. Shouya 38 %6 2 sheya Step 2: Performing exploratory data analysis © Data science provides a wealth of statistical methods and algorithms to find patterns © Asingle approach might not give desired output, iteratively, multiple algorithms need to be applied to get best result © This use of trial and error is data science art © Step 3 : Learning from data © Iterative approach of applying statistical methods and algorithms to find pattern in data, helps us to learn more about the data © Discovery is a part of being data scientist © Data might reveal the story that is different from what we have predicted in initial stage Step 4 : Visualizing © Visualization means seeing the pattems in the data and then being able to react to those pattems It also means being able to see when data is not part of the pattern # Step 5 : Obtaining insights and data products © Insights that we obtain from manipulating and analyzing the data help to perform real-world tasks e.g. result of analysis can be used to make business decisions 37, What Python is considered as best for data science needs? Reasons for considering Python as best for data science needs are: a, Considering the shifting profile of data scientists = Earlier view : Data Scientist, an unapproachable nerd who performs miracles on data using math skills = Current view: Data Scientist is a A new developer m= From a business perspective : The necessity of fusing data science and application development is obvious * Business must perform various sorts of analysis on the huge databases it has. collected - to make sense of the information and use it to predict future b. Working with multipurpose, simple and efficient language = Third party libraries - ready to use code and all types of application development support Rich ecosystem of data science libraries Open source and active community support Easy to leam and readable syntax Versatile for various programming tasks ‘Seamless integration with other languages Cross-platform compatibility Powerful data visualization libraries Leading machine learning frameworks Specialized data science environments like Jupyter Notebook 38, Explain different coding styles supported by python with examples. m= Python supports four programming / coding styles: 1. Functional © Every statement is treated as a Mathematical eqn and mutable data can be avoided. © Most of the programmers prefer this type of coding for recursion and lambda calculus. © Merit : works well for parallel processing, as there is no state to consider. © Preferred by : Academics and Data scientists. © listi = [1,2,3,4,5] newList = list(map(lambda x: x * 2 , listi)) print(newList) [2, 4, 6, 8, 10] 2. Imperative: © Performs computations as a direct change to program state © This style is especially useful when manipulating data structures and produces elegant, but simple, code. © list1 = [1,2,3,4,5] list1[4] = 50 print(list1) [1, 2, 3, 4, 52] 3. Procedural: © Independent repeated units are written as functions and called when needed © This coding style favors iteration, sequencing, selection and modularization, © def product(1): prod = 1 for i inl: prod = prod * i return(prod) la = [1,2,3] 12 = [9,152] print (product(11),product(12)) D 6e 4, Object oriented © Relies on data fields that are treated as objects and manipulated only through prescribed methods. © Python doesn't fully support this coding style as it does not support data hiding © However, this is useful to build complex applications because it supports encapsulation and polymorphism © It also favors code reuse 4. Object oriented: (...) © class ClassName: def whichClass(self): print("This is Python for Data Science Class print("Welcome....") myClass = ClassName() myClass.whichClass() This is Python for Data Science Class! Welcome. 39, What the need of indentation in Python. Python indentation refers to adding white space before a statement to a particular block of code. In another word, all the statements with the same space to the right, belong to the same code block. Statement {Code block 1 begins if condition: |Code block 1 continues if condition: How the interpreter Code block 2 begins Statement, —““*"*_,| Code block 3 begins else: Code block 2 continues Statement Code block 3 continu: ‘Statement [Code block 1 continues 0G Example of Python Indentation © Statement (line 1), if condition (line 2), and statement (last line) belongs to the same block which means that after statement 1, if condition will be executed, and suppose the if condition becomes False then the Python will jump to the last statement for execution. © The nested if-else belongs to block 2 which means that if nested if becomes False, then Python will execute the statements inside the else condition © Statements inside nested if-else belong to block 3 and only one statement will be executed depending on the if-else condition Python indentation is a way of telling a Python interpreter that the group of statements belongs to a particular block of code. A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose 40. Explain rapid prototyping and experimentation in Data Science. Rapid prototyping and experimentation are essential components of the Data Science workflow, allowing data scientists to develop and refine data-driven solutions efficiently. Let's, break down each concept and the phases involved: *"1, Rapid Prototyping:** Rapid prototyping in Data Science involves creating preliminary versions of data analysis or machine learning models to quickly test hypotheses and gain insights. It typically involves the following steps: - "*Designing a Prototype": You start by defining your problem, selecting the relevant data, and designing a basic structure or code for your data analysis or model. This initial design doesn't need to be detailed but should serve as a starting point. - "Using Libraries*: In Python, prototyping is often faster due to the availability of numerous data science libraries like NumPy, Pandas, Scikit-Learn, and TensorFlow. These libraries provide pre-built functions and tools that can significantly speed up the development process. - "Iterative Development™: Prototyping is usually an iterative process. Data scientists create a basic version of their solution, experiment with it, and then refine and expand upon it as they gain insights and identify areas for improvement. 2. Experimentation:"* Experimentation is the process of systematically trying out different algorithms, approaches, and parameters to find the best solution for a particular data science problem. This phase involves: - "Building a Data Pipeline”: To work with large datasets, you need to set up a data pipeline. This involves acquiring and preprocessing data, which may include steps like data cleaning, transformation, and feature engineering. A well-designed data pipeline is crucial for efficient experimentation - "Data Shaping"*: Once you have your data pipeline in place, you need to shape your data for modeling. This could involve selecting relevant features, normalizing data, and splitting the data into training and testing sets, -**Analyzing Data**: In the experimentation phase, you apply various machine learning algorithms, models, and techniques to the data. You assess model performance, make adjustments to hyperparameters, and evaluate different algorithms to find the best-performing one - “Presenting Results": After experimenting with different approaches, data scientists need to present their findings. This often involves communicating insights, visualizing data, and explaining which algorithm or approach was chosen and why. “*Phases in Prototyping and Experimentation:** The entire process involves the following phases: 1. "Problem Definition**: Clearly define the problem you want to solve and set objectives. 2. “Data Pipeline**: Create a pipeline to access, clean, and preprocess data efficiently. 3. “Data Shaping"*: Prepare the data for modeling. 4, “Rapid Prototyping**: Develop initial code or solutions to test your ideas. 5. "*Experimentation"*: Test various algorithms, models, and approaches to find the best solution 6. **Analysis and Evaluation**: Assess the performance of different approaches and choose the most suitable one. 7. *Presentation**: Communicate your results and insights in a meaningful way, taking into account the context and the needs of stakeholders. This iterative process of prototyping and experimentation allows data scientists to iteratively refine their solutions, find the best-performing models, and deliver valuable insights and results in the field of data science. 41, Explain the factors that affect the execution speed of data science applications. Following factors controls the execution speed of data science application: 1. Dataset size’ = Application type (business based decisions) determines the size of dataset in part, but dataset size also relies on the size of source data, = Underestimating dataset size for critical applications will be deadly 2. Loading technique: m= Loading technique depends on storage places of data = Access is faster if loaded into memory else appropriate techniques need to be analysed that help in execution 3. Coding style: Select best coding style to make fast data science application 4, Machine capability: Should meet up requirements of memory, disk and processor 5. Analysis algorithm: Determines kind of result obtained and controls execution speed 42. Explain Python ecosystem for Data Science The Python ecosystem for data science is a vast and growing collection of tools, libraries, and frameworks that are used to analyze and interpret data. It is built on top of the core Python programming language, which is a general-purpose language that is known for its simplicity and readability The Python ecosystem for data science includes a number of key libraries that are used for data manipulation, analysis, and visualization. These libraries include: NumPy: A library for working with numerical data arrays. Pandas: A library for working with tabular data. Matplotlib: A library for creating 2D and 3D visualizations. ‘Seaborn: A library for creating statistical graphics. Scikit-Learn: A library for machine leaming. These libraries are used by data scientists to solve a wide variety of problems, including} © Exploratory data analysis: This involves finding patterns and trends in data. © Data cleaning and preparation: This involves transforming data into a format that can be used for analysis. Model building: This involves creating statistical models that can be used to predict future outcomes. Model evaluation: This involves assessing the accuracy of statistical models. Model deployment: This involves making statistical models available to users. The Python ecosystem for data science is a powerful and versatile tool that can be used to solve a wide variety of problems. It is a valuable resource for data scientists and other professionals who work with data, Module 3 : (Weightage = 30%) Chapter : Understandina the tools 43, Explain any five magic functions in the Jupyter notebook. In Jupyter Notebook, "magic functions” are special commands that start with one or two percentage signs ("%" or °%%") and provide various built-in functionalities to enhance your interactive coding and data analysis experience. Here are five commonly used magic functions: 4% run - This magic function allows you to run Python script files (,py) as if they were programs. ~ Syntax: “%run script.py”

You might also like