0% found this document useful (0 votes)
16 views9 pages

DATA STRUCTURE ASSIGNMENT_01 (2)

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)
16 views9 pages

DATA STRUCTURE ASSIGNMENT_01 (2)

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/ 9

DATA STRUCTURE ASSIGNMENT:01

Unit1:Introduction to Python

1) Explain Different Data Types supported by Python.


Ans) Numeric Types:
● int (Integer): Represents whole numbers. For example, x = 5.
● float (Floating-Point): Represents decimal or floating-point numbers. For
example, y = 3.14.
● complex (Complex Number): Represents complex numbers with a real and
imaginary part. For example, z = 2 + 3j.

Text Type:
● str (String): Represents a sequence of characters enclosed in single,
double, or triple quotes. For example, text = "Hello, World!".
● Sequence Types:
● list: An ordered collection of elements that can be of different data types.
For example, my_list = [1, 2, 'apple', 3.14].
● tuple: Similar to lists but immutable, meaning their elements cannot be
changed after creation. For example, my_tuple = (1, 2, 'banana').

Mapping Type:
● dict (Dictionary): A collection of key-value pairs, where keys are unique
and associated with values. For example, my_dict = {'name': 'Alice', 'age':
30}.

Set Types:
● set: An unordered collection of unique elements. For example, my_set =
{1, 2, 3}.
● frozenset: Similar to sets but immutable, like tuples. Once created, you
cannot change its elements.

Boolean Type:
● bool (Boolean): Represents either True or False. It is often used for logical
operations and conditional statements. For example, is_valid = True.

None Type:
● None: Represents the absence of a value or a null value. It is often used to
indicate that a variable has no assigned value. For example, x = None.

2) What is operator? Explain different types of operators in python.


Ans) Arithmetic Operators:

● Arithmetic operators perform basic mathematical operations on


numeric values.
● Common arithmetic operators include addition (+), subtraction (-),
multiplication (*), division (/), floor division (//), modulus (%), and
exponentiation (**).

Comparison Operators (Relational Operators):

● Comparison operators are used to compare two values or


expressions and return a Boolean result (True or False).
● Common comparison operators include equality (==), inequality (!=),
less than (<), greater than (>), less than or equal to (<=), and greater
than or equal to (>=).

Logical Operators:

● Logical operators are used to combine and manipulate Boolean


values (True or False).
● Common logical operators include logical AND (and), logical OR (or),
and logical NOT (not).

Assignment Operators:

● Assignment operators are used to assign values to variables.


● Common assignment operators include the simple assignment (=)
and compound assignment operators such as addition and
assignment (+=), subtraction and assignment (-=), and so on.

Identity Operators:

● Identity operators are used to compare the memory addresses of two


objects.
● Common identity operators include is (True if both objects refer to the
same memory location) and is not (True if both objects do not refer to
the same memory location).

Membership Operators:

● Membership operators are used to test if a value belongs to a


sequence, such as a list or a string.
● Common membership operators include in (True if the value is found
in the sequence) and not in (True if the value is not found in the
sequence).

Bitwise Operators:

● Bitwise operators are used to perform bit-level operations on integers.


● Common bitwise operators include bitwise AND (&), bitwise OR (|),
bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>).

3) Explain comments in python.


Ans) Comments in Python:

Comments in Python are textual annotations or explanations added within


the source code. They are not executed by the Python interpreter and
serve various purposes, including:
Documentation: Comments are used to describe the purpose and
functionality of the code. They help developers understand how the code
works and its intended use.

Clarity: Comments provide clarity on complex or non-obvious parts of the


code, making it easier for others to understand the logic.

Debugging: Comments can be used to temporarily disable lines of code


for debugging or testing purposes without actually removing the code.

Notes: Developers use comments to leave reminders, to-do lists, or notes


to themselves or other team members.

Version Control: Comments can document changes and updates to the


code when using version control systems.

Comments in Python can be single-line or multi-line:

Single-line comments start with the # character and continue until the end
of the line.

Multi-line comments are created using triple-quotes (''' or """). These are
often used for documenting functions, classes, or modules, and are known
as "docstrings." They serve both as comments and documentation that can
be accessed by tools like help().
4) Compare Expression and Statement.
5) What are the different variables in Python?
Ans) Local Variables:

● Local variables are declared and used within a specific function or


block of code.
● They are only accessible within the function or block where they are
defined.
● Local variables have a limited scope and lifetime, existing only within
the function's execution.

Global Variables:
● Global variables are defined at the top level of a module or script,
outside of any function.
● They can be accessed from anywhere within the module or script.
● Global variables persist throughout the entire program's execution.

6) List and explain 5 reserved words of Python.


Ans) if:
● if is a keyword used to start a conditional statement. It allows you to
execute a block of code if a specified condition is true.

else:
● else is used in conjunction with an if statement. It specifies an
alternative block of code to execute when the condition in the if
statement is false.
for:
● for is a keyword used to initiate a loop. It is commonly used to iterate
over sequences or collections, allowing you to perform actions on
each item in the sequence.

while:
● while is a keyword used to create a loop that continues executing a
block of code as long as a specified condition remains true. It is used
for repetitive tasks with a variable termination condition.

def:
● def is a keyword used to define user-defined functions in Python.
Functions are reusable blocks of code that can be called with specific
arguments to perform a particular task.

7) Explain Arithmetic operators in python.


Ans) Addition (+):
● The addition operator (+) is used to add two or more numeric values.
● It can also be used to concatenate strings.
● For example, 5 + 3 results in 8, and "Hello" + "World" combines the
two strings to create "HelloWorld".

Subtraction (-):
● The subtraction operator (-) is used to subtract one numeric value
from another.
● For example, 10 - 4 results in 6.
Multiplication (*):
● The multiplication operator (*) is used to multiply two or more numeric
values.
● For example, 6 * 7 results in 42.

Division (/):
● The division operator (/) is used to divide one numeric value by
another.
● It always returns a floating-point number, even when dividing two
integers.
● For example, 10 / 3 results in approximately 3.3333.

Floor Division (//):


● The floor division operator (//) is used to perform division and return
the integer part of the result, effectively rounding down to the nearest
whole number.
● For example, 10 // 3 results in 3 (integer part).

Modulus (%):
● The modulus operator (%) returns the remainder when one number is
divided by another.
● For example, 10 % 3 results in 1, as it's the remainder when 10 is
divided by 3.

Exponentiation ():**
● The exponentiation operator (**) is used to raise a number to a
power.
● For example, 2 ** 3 calculates 2 raised to the power of 3, resulting in
8.
● Arithmetic operators are essential for performing various
mathematical calculations and are used extensively in Python to
manipulate numeric values and perform basic arithmetic operations.
8) Write down a simple program of addition of 2 integers.
Ans) a=int(input(“enter first number”))
b=int(input(“enter second number”))
print(“Addition of the numbers is”,a+b)

You might also like