Resource_20250121141100_unit-5_python-notes
Resource_20250121141100_unit-5_python-notes
2. What are comments in python? List down the various types of comments.
Ans A comment is text that doesn't affect the outcome of a code, it is just a piece of text to let
someone know what you have done in a program or what is being done in a block of code.
In Python, we use the hash (#) symbol to start writing a comment.
Single-Line Comments in Python starts with the hash symbol (#) with no whitespaces and lasts
till the end of the line.
Multi-Line Comments in Python starts with the triple quotes (“““) and ends with triple
quotes(”””)
Identifiers are user defined names for different parts of the program like variables,
objects, classes, functions, etc. Identifiers are not reserved. They can have letters, digits
and underscore. They must begin with either a letter or underscore. For example, _chk,
chess, trail, etc.
6. What are operators ? What is their function? Give examples of some unary and binary
operators.
Ans. Operators are tokens that trigger some computation/action when applied to
variables and other objects in an expression.
Unary plus (+), Unary minus (-), Bitwise complement (~), Logical negation (not) are a few
examples of unary operators.
Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division
(/).
Ans . Data types are used to identify the type of data a memory location can hold and the
associated operations of handling it. The data that we deal with in our programs can be of
many types like character, integer, real number, string, boolean, etc. Hence programming
languages including Python provide ways and facilities to handle all these different types
of data through data types.
The data types define the capabilities to handle a specific type of data such as memory
space it allocates to hold a certain type of data and the range of values supported for a
given data type, etc.
8. What is the difference between a mutable data type and an immutable data type?
Ans. Mutable data types are those that can be modified after their creation. It is suitable for
collections of items where frequent updates are needed Elements can be added, removed,
or changed. Examples: List, Dictionary, Set.
Immutable data types data types are those that cannot be modified after their creation.
Elements cannot be changed once set. Any operation that appears to modify an immutable
object will create a new object. Examples: Numeric (int, float), String, Tuple.
Ans. Python lists are containers that can store an ordered list of values of same or
different data types together in a single variable.
The fact that elements of a list need not be homogeneous makes them highly
adaptable and powerful data structure in Python.
Lists provide fast access to its elements using index numbers.
Python lists are mutable which makes them memory efficient.
They serve as the basic building blocks for programs that process large amounts of
data
10. What is the difference between appending a list and extending a list?