1.What is Python and why is it important?
Ans: Python is a high-level, interpreted, and general-purpose programming language. It is important because it is easy to learn, has a large community and many libraries, and is used in various fields such as data science, web development, and machine learning.2.What is the difference between a tuple and a list in Python?
Ans: A tuple is an immutable and ordered collection of elements, while a list is a mutable and ordered collection of elements. Tuples are defined using parentheses, while lists are defined using square brackets.
10.What is the difference between list and tuple in Python?
14.How do you sort a list in Python?
Python code:
3.What are the benefits of using Python?
Ans: Easy to learn and read, has a large community, a vast collection of libraries, supports multiple programming paradigms, used in a variety of fields such as web development, data science, and machine learning.
4.What is PEP 8 and why is it important?
Ans: PEP 8 is the Python style guide that provides guidelines for writing readable and maintainable code. It is important because it ensures that all code written in Python follows a consistent style, making it easier to read and maintain.
5.How do you define a function in Python?
Ans: A function in Python is defined using the "def" keyword followed by the function name and the function's inputs within parentheses. The code within the function is indented and ends with a return statement if a value is to be returned.
6.What is a decorator in Python?
Ans: A decorator in Python is a special type of function that can modify or extend the behavior of another function. Decorators are defined using the "@" symbol and are applied to functions using the "@" symbol followed by the decorator name.
7.What is the difference between deep and shallow copying in Python?
Ans: Shallow copying in Python creates a new object that points to the same objects as the original object. Deep copying creates a new object with a new memory location and a new set of objects, independent of the original object.
8.What is a module in Python?
Ans: A module in Python is a collection of functions and variables that can be imported into another Python script. Modules can be created using a .py file or by writing code in the built-in Python module named "module".
9.What is Python used for?
Ans: Python is a general-purpose programming language that can be used for a wide range of tasks, including web development, data analysis, machine learning, and scientific computing.
Ans: Lists are mutable (can be changed), while tuples are immutable (cannot be changed). Lists use square brackets ([]), while tuples use parentheses (()).
11.What is a dictionary in Python?
Ans: A dictionary in Python is an unordered collection of key-value pairs, where each key maps to a specific value. Dictionaries use curly braces ({}).
12.How do you check if a variable is an instance of a specific type in Python?
Ans: You can use the is instance() function to check if a variable is an instance of a specific type in Python. For example: is instance(var, int).
13.How do you convert a string to a number in Python?
Ans: To convert a string to a number in Python, you can use the int() or float() functions, depending on whether the string represents an integer or a floating-point number. For example: int("123") or float("3.14").
14.How do you sort a list in Python?
Ans: You can use the sorted() function to sort a list in Python. For example: sorted([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]). You can also use the sort() method on a list object to sort the list in-place.
15.How do you reverse a string in Python?
Ans: Using string slicing:
def reverse_string(s):
return s[::-1]
This function takes a string s as input and returns a new string that is the reverse of the input string. The slice s[::-1] takes all characters of the string s, starting from the end and going to the start, with a step of -1.
return s[::-1]
This function takes a string s as input and returns a new string that is the reverse of the input string. The slice s[::-1] takes all characters of the string s, starting from the end and going to the start, with a step of -1.
16. How would you check if a number is a palindrome in Python?
Ans:
def is_palindrome(num):
return str(num) == str(num)[::-1]
This function takes a number num as input and returns True if it's a palindrome, and False otherwise. The function converts the number to a string, and then checks if the string is equal to its reverse by slicing.
return str(num) == str(num)[::-1]
This function takes a number num as input and returns True if it's a palindrome, and False otherwise. The function converts the number to a string, and then checks if the string is equal to its reverse by slicing.
17. How would you remove duplicates from a list in Python?
Ans:
python Code:
def remove_duplicates(1st):
return list(set(1st))
This function takes a list 1st as input and returns a new list with duplicates removed. The function converts the input list to a set, which does not allow duplicates, and then back to a list.
return list(set(1st))
This function takes a list 1st as input and returns a new list with duplicates removed. The function converts the input list to a set, which does not allow duplicates, and then back to a list.
No comments:
Post a Comment