0% found this document useful (0 votes)
110 views7 pages

Python CCE - II by Atul Sadiwal?

Lists and tuples are both used to store collections of data in Python. The key differences are: - Lists are mutable while tuples are immutable - Lists use square brackets while tuples use parentheses - Lists are used when items need to be modified, tuples when items should not be modified

Uploaded by

Harsha Choudhary
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)
110 views7 pages

Python CCE - II by Atul Sadiwal?

Lists and tuples are both used to store collections of data in Python. The key differences are: - Lists are mutable while tuples are immutable - Lists use square brackets while tuples use parentheses - Lists are used when items need to be modified, tuples when items should not be modified

Uploaded by

Harsha Choudhary
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/ 7

CCE - II

B.C.A. IV Sem (Python)

Q.1 What is the difference between list and tuples in Python?


Ans :- Lists and tuples are both used to store collections of data in Python. However, there
are some key differences between them:
Mutability: Lists are mutable, which means you can change their content after they have
been created, while tuples are immutable, meaning that their content cannot be changed
after they have been created.
Syntax: Lists are created using square brackets [], while tuples are created using parentheses
().
Usage: Lists are typically used when you want to store a collection of items that can be
modified, while tuples are typically used when you want to store a collection of items that
should not be modified.
Performance: Lists are slightly slower than tuples when it comes to accessing items, but they
are faster when it comes to modifying them.
Here are some examples to illustrate the differences:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_list[0] = 4
print(my_list)
my_tuple[0] = 4
my_list.append(4)
print(my_list)
my_tuple.append(4)

Q.2 What are the key features of Python?


Ans :- Python is a high-level, interpreted programming language that is known for its
simplicity, readability, and versatility. Some of the key features of Python are:
Easy to learn: Python has a simple syntax and is easy to learn, even for beginners.
Interpreted language: Python code is executed line-by-line by an interpreter, which allows
for quick prototyping and testing.
Dynamically typed: Python is dynamically typed, meaning that variables don't have to be
declared before use, and their data type can change during runtime.
Object-oriented: Python supports object-oriented programming, which allows for code
reusability and encapsulation.

Cross-platform: Python code can run on various operating systems, including Windows,
macOS, and Linux.
Extensive libraries: Python has a vast collection of built-in libraries and modules for various
tasks, such as web development, data analysis, scientific computing, and more.
Third-party packages: Python has a huge ecosystem of third-party packages and modules
that can be easily installed using package managers like pip.
High-level data structures: Python has built-in high-level data structures like lists,
dictionaries, and tuples that make it easy to work with complex data.
Large community: Python has a large and active community of developers who contribute to
the language and create useful tools and libraries.
These features make Python a popular choice for a wide range of applications, from web
development and data science to machine learning and artificial intelligence.

Q.3 What are the common built-in data types in Python?


Ans :- Python has several built-in data types that are used to store different kinds of data.
Here are some of the most common ones:
Integers (int): Used to store whole numbers, both positive and negative.
Floating-point numbers (float): Used to store decimal numbers, both positive and negative.
Boolean (bool): Used to store either True or False.
Strings (str): Used to store a sequence of characters, such as text.
Lists: Used to store a collection of items that can be modified.
Tuples: Used to store a collection of items that cannot be modified.
Dictionaries: Used to store key-value pairs, where each key is associated with a value.
Sets: Used to store a collection of unique items.
None: Used to represent the absence of a value.
These data types can be combined and manipulated in various ways to perform complex
operations and solve problems in Python. Additionally, Python also allows for creating
custom data types using classes and objects.
Q.4 What is slicing in Python?
Ans :- Slicing is a way to extract a subset of elements from a sequence object like strings,
lists, and tuples in Python. It is performed by specifying the start and end indices of the
desired subset within the original sequence.
In Python, slicing is done using the colon (:) operator. The basic syntax for slicing is as
follows:
sequence[start:end:step]
start: The starting index of the subset (inclusive). If omitted, it defaults to 0.
end: The ending index of the subset (exclusive). If omitted, it defaults to the length of the
sequence.
step: The increment between the elements to include in the subset. If omitted, it defaults to
1.
Here are some examples of slicing in Python:
my_list = [1, 2, 3, 4, 5]
subset = my_list[1:4] # get elements from index 1 to 3 (excluded)
print(subset) # Output: [2, 3, 4]
my_string = "Hello, World!"
subset = my_string[7:12] # get characters from index 7 to 11 (excluded)
print(subset) # Output: World
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
subset = my_list[1:9:2] # get every other element from index 1 to 8 (excluded)
print(subset) # Output: [2, 4, 6, 8]
Slicing is a useful technique for extracting and manipulating subsets of data from sequences
in Python. It can be used in various ways to perform complex data operations and analysis.

Q.5 What is a lambda function?


Ans :- A lambda function in Python is a small anonymous function that can have any number
of arguments, but can only have one expression. It is also known as an inline function or a
lambda expression.
The basic syntax for creating a lambda function is as follows:
lambda arguments: expression
Here, arguments represent the input parameters of the function, and expression is a single
statement that the function will return as its output.
Lambda functions are often used as a shortcut for creating simple, one-line functions that
are only used once and don't require a formal function definition. They are commonly used
in functional programming, where functions are treated as first-class objects and can be
passed as arguments to other functions.
Here is an example of a lambda function that calculates the square of a number:
square = lambda x: x**2
result = square(4)
print(result) # Output: 16
In this example, we have defined a lambda function named square that takes one argument
x and returns the square of x. We have then called this function with an argument of 4,
which returns the value of 16.
Lambda functions can also be used in combination with other built-in functions like map(),
filter(), and reduce() to perform complex operations on sequences.

Q.6 What is a dictionary in Python?


Ans :- In Python, a dictionary is a built-in data type that represents a collection of key-value
pairs. It is an unordered, mutable collection that allows you to store and retrieve values
based on their keys.
Dictionaries are created using curly braces {} or the built-in dict() function. Each key in a
dictionary must be unique and associated with a corresponding value. The syntax for
creating a dictionary is as follows:
my_dict = {"key1": value1, "key2": value2, ...}
Here, key1, key2, etc. are the keys in the dictionary, and value1, value2, etc. are the
corresponding values.
You can also add, remove, or modify key-value pairs in a dictionary using the built-in
methods such as update(), pop(), popitem(), clear(), and others.
Here is an example of a simple dictionary:
my_dict = {"apple": 2, "banana": 4, "orange": 1}
print(my_dict["apple"]) # Output: 2
In this example, we have defined a dictionary my_dict with three key-value pairs. We have
then accessed the value associated with the key "apple" using square bracket notation and
printed it to the console.
Dictionaries are commonly used in Python to represent real-world entities such as people,
products, or events, where each key represents a unique identifier or attribute, and each
value represents a corresponding value or property.

Q.7 What are negative indexes and why are they used?
Ans :- In Python, negative indexes are used to access elements from the end of a sequence
(such as a string, list, or tuple), rather than from the beginning. Negative indexes start at -1
for the last element of the sequence, -2 for the second-to-last element, and so on, up to the
first element of the sequence at index -len(sequence).
The use of negative indexes can be helpful when you need to access elements in a sequence
from the end, or when you don't know the exact length of a sequence. It can also be used in
combination with slicing to extract a subset of elements from the end of a sequence.
Here is an example of using negative indexes to access elements in a string:
my_string = "Hello, world!"
print(my_string[-1]) # Output: !
print(my_string[-2]) # Output: d
print(my_string[-7:-1]) # Output: world
In this example, we have defined a string my_string and used negative indexes to access the
last and second-to-last characters of the string. We have also used negative indexes in
combination with slicing to extract the word "world" from the end of the string.
It is important to note that negative indexes can cause errors if you try to access an index
that is beyond the length of the sequence, such as my_string[-14] in the example above,
which would result in an IndexError.

Q.8 What advantages do NumPy arrays offer over (nested) Python lists?
Ans :- NumPy arrays offer several advantages over nested Python lists, including:
Efficiency: NumPy arrays are more efficient than nested Python lists because they are
implemented in C and can take advantage of hardware-level optimizations for better
performance. They also use less memory because they store data in contiguous blocks of
memory, unlike nested lists which store data in scattered locations.
Ease of use: NumPy arrays provide a simple and intuitive interface for working with large
arrays of data, including a wide range of mathematical functions and operations that are
optimized for speed.
Broadcasting: NumPy arrays support broadcasting, which allows for element-wise
operations between arrays of different shapes and sizes without the need for explicit
looping or reshaping.
Multi-dimensional arrays: NumPy arrays can have any number of dimensions, making them
well-suited for working with multi-dimensional data such as images, audio, and video.
Numerical precision: NumPy arrays offer a high level of numerical precision, with support
for floating-point and complex numbers, as well as various data types for integers, booleans,
and more.
Overall, NumPy arrays provide a powerful and efficient tool for working with large and
complex data sets, particularly in scientific computing and data analysis applications. They
offer a number of advantages over nested Python lists, including better performance, ease
of use, and support for advanced mathematical and numerical operations.

Q.9 Write a program to produce Fibonacci series in Python.


Ans :- Here's a simple program in Python to generate the Fibonacci series up to a given
number of terms:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
if n == 1:
print(a)
elif n == 2:
print(a, b)
else:
print(a, b, end=" ")
for i in range(n-2):
c=a+b
print(c, end=" ")
a, b = b, c
Q.10 What is string in python? How many types of functions are used to manipulate a
string. Explain any 10.
Ans :- In Python, a string is a sequence of characters enclosed in quotes (either single quotes
or double quotes). Strings are a fundamental data type in Python and are used to represent
text and other types of data that can be represented as a sequence of characters.
Python provides several built-in functions to manipulate strings. Here are ten common
functions used to manipulate strings:
len(): Returns the length of a string in characters.
str.upper(): Converts all the characters in a string to uppercase.
str.lower(): Converts all the characters in a string to lowercase.
str.strip(): Removes any leading or trailing whitespace characters from a string.
str.replace(old, new): Replaces all occurrences of a substring old with a new substring new.
str.split(sep): Splits a string into a list of substrings based on a specified separator sep.
str.join(iterable): Joins a list of strings or other iterable into a single string, with each
element separated by the original string
str.startswith(prefix): Returns True if a string starts with the specified prefix, otherwise
returns False.
str.endswith(suffix): Returns True if a string ends with the specified suffix, otherwise returns
False.
str.isnumeric(): Returns True if all the characters in a string are numeric, otherwise returns
False.
There are many more functions that can be used to manipulate strings in Python. These
functions can be combined and used in creative ways to achieve a wide range of text
processing and manipulation tasks.

for more info contact me on IG:- atul.xt

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy