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

OneMarksPython

Vgg

Uploaded by

Swapnil Sawai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OneMarksPython

Vgg

Uploaded by

Swapnil Sawai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1. ONE MARKS QUESTIONS.

1.How are comments written in Python?


● Comments in Python can be written using the # symbol. Any text
following # on that line will be ignored by the interpreter.

For multi-line comments, triple quotes (''' or """) can be used

2.Give the use of index() in string.


● The index() method returns the lowest index of the substring if found in
the string. If the substring is not found, it raises a ValueError.

3.Explain the range() function and its parameters.


● The range() function generates a sequence of numbers. It can take one,
two, or three parameters:
○ range(stop): Generates numbers from 0 to stop - 1.
○ range(start, stop): Generates numbers from start to stop - 1.
○ range(start, stop, step): Generates numbers from start to stop - 1,

incrementing by step.

4.How to create a void function in Python?


○ A void function is a function that does not return any value. It is
defined using the def keyword followed by the function name. You
can use the return statement without a value or omit it entirely
def print_message():
print("This is a void function")

5.What is the difference between pop() and del in list?


● pop(index) removes and returns the element at the specified index. If no
index is specified, it removes and returns the last element.
● del is used to delete an element at a specified index but does not return
it.

6.Compare list and tuple (any two points).


● Mutability: Lists are mutable (can be changed), while tuples are
immutable (cannot be changed).
● Syntax: Lists are defined using square brackets [], whereas tuples are
defined using parentheses ().
7.What do the following functions return: clock() and gmtime()?
● clock(): Returns the current processor time as a float, indicating how
much time the program has been running.
● gmtime(): Returns the current time in UTC as a struct_time object.

8.List the syntax for handling exceptions.


● The syntax for handling exceptions in Python involves the use of try,
except, else, and finally.
try:
# Code that may raise an exception
except SomeException:
# Code to handle the exception
else:
# Code to execute if no exception occurs
finally:
# Code that will always execute

9.List any two functions in the math module.


● math.sqrt(x): Returns the square root of x.
● math.factorial(x): Returns the factorial of x.

10.Explain the function enumerate().


● The enumerate() function adds a counter to an iterable and returns it as
an enumerate object, which can be converted into a list of tuples.

11. What are the advantages of Python?


● Easy to Learn and Use
● Extensive Libraries and Frameworks
● Cross-Platform Compatibility
● Strong Community Support
● Versatility

12.List out main differences between lists and tuples.


Mutability: Lists are mutable (can be changed), while tuples are
immutable (cannot be changed).
Syntax: Lists use square brackets [], whereas tuples use parentheses ().
Performance: Tuples are generally faster than lists due to their
immutability.
13.Python is a scripting language. Comment.
● Yes, Python is often considered a scripting language because it is
interpreted, allowing developers to write scripts that automate tasks. It
can be used for simple command-line scripts or complex applications.

. Demonstrate set with an example.


A set is an unordered collection of unique elements

my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
my_set.add(6) # Adding an element
print(my_set) # Output: {1, 2, 3, 4, 5, 6}

15.What is a dictionary? Give an example.


A dictionary is a collection of key-value pairs, where each key is unique.
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict['name']) # Output: Alice

16. What is RegEx? Give an example


Regular Expressions (RegEx) are sequences of characters that form search
patterns, used for string searching and manipulation.
import re
pattern = r'\d+' # Matches one or more digits
result = re.findall(pattern, 'There are 2 cats and 3 dogs.')
print(result) # Output: ['2', '3']

17.What is a user-defined module? Give an example.


● A user-defined module is a Python file (with a .py extension) that
contains functions, classes, or variables that can be imported and used in
other Python files.

# my_module.py
def greet(name):
return f"Hello, {name}!"

# In another file
from my_module import greet
print(greet('Alice')) # Output: Hello, Alice!

18.Python is a case-sensitive language. Comment.


● Yes, Python is case-sensitive, meaning that variables, function names,
and class names with different cases are considered different identifiers.

19.What is dry run in Python?


● A dry run is the manual process of stepping through the code to
understand its logic without executing it on a computer. This helps in
identifying logical errors and understanding flow.

20.What is a lambda function? Give an example.


A lambda function is an anonymous function defined using the lambda keyword. It
can take any number of arguments but can only have one expression.
add = lambda x, y: x + y
print(add(3, 5)) # Output: 8

21.What is indentation?
● Indentation in Python refers to the use of spaces or tabs to define the
structure and flow of the code. It indicates a block of code belonging to a
specific control structure (like loops, functions, classes). Proper
indentation is crucial as it determines the grouping of statements.

22.Write code to print the elements of the list lst = [10, 20, 30, 40, 50].

lst = [10, 20, 30, 40, 50]


for element in lst:
print(element)

. What is a slice operator?


● The slice operator (:) is used to retrieve a subset of elements from a
sequence (like lists, tuples, or strings). It allows you to specify a start
index, an end index, and an optional step.

23.What is a variable-length argument?


● Variable-length arguments allow a function to accept an arbitrary number
of arguments. In Python, this can be done using *args (for positional
arguments) and **kwargs (for keyword arguments).
24.How to add multiple elements at the end of the list?
● You can use the extend() method or the += operator to add multiple
elements to the end of a list.

25.Explain the remove() method.


● The remove() method is used to remove the first occurrence of a
specified value from a list. If the value is not found, it raises a ValueError.

26.Write a lambda function to add 10 to a given integer.


add_ten = lambda x: x + 10
print(add_ten(5)) # Output: 15

27. How to raise an exception with arguments?


● You can raise an exception with a custom message using the raise
keyword followed by the exception type and the message
raise ValueError("This is a custom error message.")

28.List the methods of the re package.


● Common methods in the re package include:
○ re.match(): Determines if the regular expression matches at the

beginning of the string.


○ re.search(): Searches the string for a match anywhere.
○ re.findall(): Returns all non-overlapping matches of the pattern in the

string.
○ re.sub(): Replaces occurrences of the pattern with a specified string

29.What is wb mode in file?


● The wb mode opens a file for writing in binary format. If the file does not
exist, it creates a new file. If the file exists, it truncates the file to zero
length.

You might also like