DATA STRUCTURE ASSIGNMENT_01 (2)
DATA STRUCTURE ASSIGNMENT_01 (2)
Unit1:Introduction to Python
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.
Logical Operators:
Assignment Operators:
Identity Operators:
Membership Operators:
Bitwise Operators:
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:
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.
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.
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.
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)