Python Programming Quiz Questions
Python Programming Quiz Questions
4. Tuple :
Tuple is a collection of elements and it is similar to the
List. But the items of the tuple are separated by comma
and the elements are enclosed in ( ) parenthesis.
Tuples are immutable or read-only. That means we can
2. String : not modify the size of tuple or we cannot change the
String is a collection of characters. value of items of the tuple.
In Python, we can use single quote, double quote or Here are examples of tuples -
triple quote to define a string.
We can use two operators along with the string one is +
and another is *.
The + operator is used to concatenate the two strings.
While * operator is used as a repetition operation.
Following execution illustrates it -
5. Dictionary
Dictionary is a collection of elements in the form of
key:value pair.
The elements of dictionary are present in the curly
brackets.
For example -
3. List :
It is similar to array in C or C++ but it can
simultaneously hold different types of data in list.
It is basically an ordered sequence of some data written
using square brackets ([]) and commas (,)
For example -
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 6
OR
[Link] and explain the arithmetic operators in Python
with examples.
[Link] to Chapter 1 of Venus Publication Question
Bank.
[Link]`if-else` and `if-elif-else` statements with
suitable examples.
[Link] to Chapter 2 of Venus Publication Question
Bank.
OR
[Link] for loop with syntax and example.
[Link] for loop
The for loop is another popular way of using iteration. [Link] are tuples? Explain with an example.
The syntax of for loop is Ans.A tuple is an ordered collection of elements in Python.
Syntax Unlike lists, tuples are immutable, meaning their
for variable in sequene: elements cannot be changed, added, or removed after
Body of for loop creation. Tuples are often used when you want to ensure
Example the integrity of the data and when performance is
for val in numbers: important since they are generally faster than lists for
val = val + 1 fixed collections of items.
The variable takes the value of the item inside the Characteristics of Tuples:
sequence on each iteration. (i) Ordered: Tuples maintain the order of elements.
Loop continues until we reach the item in the sequence. (ii) Immutable: Once created, you cannot change, add, or
The body of for loop is separated from the rest of the remove items.
code using indentation. (iii)Can contain different data types: A tuple can hold
The flowchart is as shown below– items of different data types like integers, strings, or
other objects.
(iv) Can be nested: Tuples can contain other tuples or data
structures.
Syntax:A tuple is created by placing a comma-separated
sequence of elements within parentheses `()`.
```python
Example of a tuple
my_tuple = (1, "apple", 3.14)
```
Accessing Elements:
```python
print(my_tuple[0]) # Output: 1
print(my_tuple[1]) # Output: "apple"
```
Example:
[Link] a Python program to check whether given num-
```python
ber is even or odd.
Defining a tuple
[Link] (“Enter value of n”)
person = ("Alice", 25, "Engineer")
n = int (input())
Accessing tuple elements
if n%2 = = 0:
name = person[0] # "Alice"
print (“Even Number”)
age = person[1] # 25
in n%2 = 1:
profession = person[2] # "Engineer"
print (“Odd Number”)
Tuples can also be used for packing and unpacking:
name, age, profession = person
print(name) # "Alice"
print(age) # 25
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Venus Python Programming 7
print(profession) # "Engineer" contains two modules: `[Link]` and
``` `[Link]`. You can import them like this:
In this example, `person` is a tuple containing three ```python
elements. We can access the elements using indexing # [Link]
or unpack the tuple into individual variables. from my_package import module1
OR from my_package.module2 import some_function
[Link] indexing and slicing in strings with ```
examples. Key Differences:
[Link] to Chapter 3 of Venus Publication Question (i) Module: A single file containing Python code (`.py`
Bank. file).
(ii) Package: A directory containing multiple Python
[Link] a module and a package in Python? modules and a special `__init__.py` file to make it a
Ans. Module:A module is a single file that contains package.
Python code. It can include functions, classes, Packages help you organize large codebases, while
variables, and runnable code. You can import and modules help organize smaller pieces of related code.
use the functions or classes defined in a module in OR
another Python script. A module is simply a Python [Link] will you define and call a function in python?
file with the `.py` extension. Give illustrative example.
Example of a module:Suppose you have a file [Link] to Chapter 4 of Venus Publication Question
`math_operations.py` with the following code: Bank.
```python
# math_operations.py [Link] is inheritance? List and explain the types of
def add(a, b): inheritance supported in Python.
return a + b [Link] to Chapter 5 of Venus Publication Question
def subtract(a, b): Bank.
return a - b OR
``` [Link] is polymorphism in OOP? How is it achieved
This file is a module named `math_operations`. To in Python?
use this module in another script, you would import [Link] in Object-Oriented Programming
it: (OOP) refers to the ability of different objects to
```python respond to the same method or message in a way
# [Link] that is appropriate to their specific types. In other
import math_operations words, polymorphism allows a single interface to
result = math_operations.add(3, 4) represent different underlying forms (data types or
print(result) objects).
``` There are two types of polymorphism in OOP:
Package:A package is a collection of Python modules (i) Compile-time polymorphism (Method Overloading)
grouped together in a directory. It is essentially a (ii) Runtime polymorphism(Method Overriding)
directory containing multiple module files and a special Python primarily supports runtime polymorphism
file named `__init__.py` (which can be empty) that tells because Python does not support method
Python this directory should be treated as a package. overloading in the traditional sense (i.e., multiple
Packages allow you to organize your modules into methods with the same name but different
subdirectories, making your code easier to manage, signatures).
especially for larger projects. (i) Method Overriding (Runtime Polymorphism)
Example of a package structure: In Python, subclasses can override methods of a parent
``` class. When a subclass redefines a method, the
my_package/ subclass's version of the method is called instead of the
__init__.py parent class's version.
[Link] The method in the subclass can have the same name,
[Link] but with different functionality, allowing for dynamic
``` dispatch.
In this example, `my_package` is a package that
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 8
Example:
```python
class Animal: For Doubts Discussion
def sound(self): Join : “[Link]
print("Some animal sound")
class Dog(Animal):
def sound(self): For 100% success go through Venus Question
print("Bark") Bank for 1st to 6th sem (Concept) ......(Avail-
class Cat(Animal): able at your nearest book shop)
def sound(self):
print("Meow")
# Polymorphism in action
animals = [Dog(), Cat()]
for animal in animals:
[Link]() # Dog barks, Cat meows
```
Output:
```
Bark
Meow
```
In this example, despite calling the same method
`sound()`, the actual method executed depends on the
object's class (either `Dog` or `Cat`), demonstrating
runtime polymorphism.
(ii) Duck Typing(Implicit Polymorphism):Python also
uses duck typing, where the type or class of an object
is less important than the methods or behaviors it
implements. If an object can perform the expected
behavior (e.g., has a `sound()` method), it is treated as For 100% success go through Venus Question
the required type, even if it doesn't explicitly inherit Bank for 1st to 6th sem (Concept) ......(Avail-
from a particular class. able at your nearest book shop)
Example:
```python
class Bird:
def sound(self):
print("Tweet")
class Human:
def sound(self):
print("Speak")
def make_sound(entity):
[Link]() # Duck typing: Any object with a sound()
method can be used
make_sound(Dog()) # Bark
make_sound(Cat()) # Meow
make_sound(Bird()) # Tweet
make_sound(Human()) # Speak
```
Here, any object that has the `sound()` method can be
passed to the `make_sound()` function, demonstrating
polymorphism through duck typing.
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)