[TY BSC CS] PYTHON MOST IMPORTANT QUESTIONS FOR SEM 5 IN 2023 | PYTHON IMP QUESTIONS FOR COMPUTER SCIENCE SEMESTER FIVE
1) What is a file? How to read and write to a file?
A file is a information or data which permanently stored in computer storage devices that
can access any time.
In python, there are some inbuilt functions exist, to read and write to a file.
write(): method is used to write/ insert data into the file.
Open(‘filename’, ‘r’) : method is used to open file in read format.
2) Explain the frozen set with an example.
- i) The frozen sets are the immutable form of the normal sets, i.e., the items of the frozen
- set cannot be changed and therefore it can be used as a key in the dictionary.
- ii) The elements of the frozen set cannot be changed after the creation.
- iii) We cannot change or append the content of the frozen sets by using the methods like add() or remove().
- iv) The frozenset() method is used to create the frozenset object.
- v) The iterable sequence is passed into this method which is converted into the frozen set
- as a return type of the method.
Ex:
Frozenset = frozenset([1,2,3,4,5])
print(type(Frozenset))
print("\nprinting the content of frozen set...")
for i in Frozenset:
print(i);
Frozenset.add(6)
[TY BSC CS] PYTHON MOST IMPORTANT QUESTIONS FOR SEM 5 IN 2023 | PYTHON IMP QUESTIONS FOR COMPUTER SCIENCE SEMESTER FIVE
3) Define text and binary files with an example.
- i) A text file consists of human readable characters, which can be opened by any text editor.
- ii)Binary files are made up of non-human readable characters and symbols, which require specific programs to access its contents.
4) Give the purpose of the match function.
Python's re.match() function finds and delivers the very first appearance of a regular
expression pattern.
5) Explain RegEx.
A regular expression is a set of characters with highly specialized syntax that we can use
to find or match other characters or groups of characters.
In short, regular expressions, or Regex, are widely used in the UNIX world.
The re-module in Python gives full support for regular expressions of Pearl style.
The re module raises the re.error exception whenever an error occurs while implementing
or using a regular expression.
6) Write the use of tell() and seek() functions.
It returns the current position of the file pointer within the file.
Python provides us the seek() method which enables us to modify the file pointer
position externally.
7) Explain Call by value and call by reference in python.
Call by value:
- i) In this particular parameter passing method, the values of the actual parameters copy into the function’s formal parameters. It stores both types of parameters in different memory locations. Thus, if one makes any changes inside the function- it does not show on the caller’s actual parameters.
- ii)This method passes a copy of an actual argument to the formal argument of any called function.
Call by Reference
- iii) In this case, both the formal and actual parameters refer to a similar location. It means that if one makes any changes inside the function, it gets reflected in the caller’s actual parameters.
- iv) This method passes the address or location of the actual arguments to the formal arguments of any called function. It means that by accessing the actual argument’s addresses.
READ ALSO: Most Usefull Computer Keyboard shortcuts for you
1k views in just 10 min on YouTube 2023
8) Explain string methods with examples.
Method Description
- capitalize(): Converts the first character to upper case
- casefold(): Converts string into lower case
- center(): Returns a centered string
- count(): Returns the number of times a specified value occurs in a string
- encode(): Returns an encoded version of the string
- endswith(): Returns true if the string ends with the specified value.
9) Explain Anonymous function with an example.
- i) Python Lambda Functions are anonymous function means that the function is without a name.
- ii)As we already know that the def keyword is used to define a normal function in Python.
- iii) Also, the lambda keyword is used to define an anonymous function in Python.
Ex.,
str1 = 'GeeksforGeeks'
# lambda returns a function object
rev_upper = lambda string: string.upper()[::-1]
print(rev_upper(str1))
10) Define List and Explain list methods with an example.
- i) A list in Python is used to store the sequence of various types of data.
- ii)The list is a sequence data type which is used to store the collection of data.
- iii) Python lists are mutable types; it means we can modify its element after it is created.
Method Description
- append(): Used for appending and adding elements to the end of the List.
- copy(): It returns a shallow copy of a list
- clear(): This method is used for removing all items from the list.
- count() :This methods count the elements
- extend(): Adds each element of the iterable to the end of the List
- index() : Returns the lowest index where the element appears.
- insert(): Inserts a given element at a given index in a list.
- pop(): Removes and returns the last value from the List or the given index value.
11) What is a dictionary? Explain all the dictionary methods with an example?
- i) Python Dictionary is used to store the data in a key-value pair format.
- ii) The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key.
- iii) It is a mutable data-structure.
- iv) The dictionary is defined into element Keys and values.
- v) Keys must be a single element
Method Description
- clear(): Removes all the elements from the dictionary
- copy(): Returns a copy of the dictionary
- fromkeys(): Returns a dictionary with the specified keys and value
- get(): Returns the value of the specified key
- items(): Returns a list containing a tuple for each key value pair
- keys() :Returns a list containing the dictionary's keys
- pop(): Removes the element with the specified key
- popitem() : Removes the last inserted key-value pair
- setdefault(): Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
- update(): Updates the dictionary with the specified key-value pairs
- values(): Returns a list of all the values in the dictionary
12) What is the local and global scope of variables in python? Explain the different
scenarios with an example snippet.
13) Explain tuples with an example.
- i) Tuple is a collection of Python objects much like a list.
- ii) The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
- iii) Values of a tuple are syntactically separated by ‘commas’.
Ex.,
Tuple1 = ('Geeks', 'For', ‘Geeks’)
print(Tuple1)
14) Show how try…except blocks is used for exception handling in Python with
examples.
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
15) Explain set operations in detail with an example.
set1 = {1, 2, 3, 4}
set2 = {2, 5, 4, 6}
set1.add(4)
set2.clear()
x = set1.copy(set2)
y = set1.difference(set2)
a = set1.union(set2)
z = set1.intersection(set2)
set1.pop()
set1.remove()
16) Design a python program which will throw an exception if the value entered by the
user is less than zero.
17) Define:
i. Data type
Data type is a type of data stored into the variable. Python is a dynamically typed
language; hence we do not need to define the type of the variable while declaring it.
ii. Function
A function is a block of code which only runs when it is called. You can pass data,
known as parameters, into a function. A function can return data as a result.
18) Explain the need for Unicodes.
Unicode provides a unique way to define every character in every spoken language
of the world by assigning it a unique number.
The Unicode standard is maintained by the Unicode Consortium and defines more
than 1,40,000 characters from more than 150 modern and historic scripts along with
emoji.
Unicode can be defined with different character encoding like UTF-8, UTF-16, UTF-
32, etc.
Among these UTF-8 is the most popular as it is used in over 90% of websites on the
World Wide Web as well as on most modern Operating systems like Windows.
ALSO READ :
Simple 30 Computer science project topic ideas for begging students.
19) Explain the match function in detail
Python's re.match() function finds and delivers the very first appearance of a regular
expression pattern.
In Python, the RegEx Match function solely searches for a matching string at the
beginning of the provided text to be searched.
The matching object is produced if one match is found in the first line.
If a match is found in a subsequent line, the Python RegEx Match function gives
output as null.
Examine the implementation for the re.match() method in Python.
The expressions ".w*" and ".w*?" will match words that have the letter "w," and
anything that does not have the letter "w" will be ignored.
The for loop is used in this Python re.match() illustration to inspect for matches for
every element in the list of words.
0 Comments