0% found this document useful (0 votes)
2 views4 pages

Assignment

The document provides an overview of various Python concepts including string data types and operations, list slicing, dictionary manipulation, functions, and list manipulation. It includes definitions, examples, and common operations for each topic. The content is aimed at helping students understand fundamental Python programming techniques.

Uploaded by

Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Assignment

The document provides an overview of various Python concepts including string data types and operations, list slicing, dictionary manipulation, functions, and list manipulation. It includes definitions, examples, and common operations for each topic. The content is aimed at helping students understand fundamental Python programming techniques.

Uploaded by

Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment (3)

Name - Shishir Mishra

Branch (CSE)

SUBJECT - PYTHON

1. Define String Data Type and Its Operations


String is a sequence of characters enclosed in single (' ') or double (" ") quotes. Strings are
immutable, meaning they cannot be changed after creation.

Example:

my_string = "Hello, Python!"

Common String Operations:

text = "Python"

# Access characters
print(text[0]) # Output: P

# Slicing
print(text[0:3]) # Output: Pyt

# Length
print(len(text)) # Output: 6

# Concatenation
print(text + " Programming") # Output: Python Programming

# Repetition
print(text * 2) # Output: PythonPython

# Conversion
print(text.upper()) # Output: PYTHON
print(text.lower()) # Output: python
# Search
print("th" in text) # Output: True

2. Define List Slicing with Appropriate Example


List slicing is used to access a subset of elements from a list using a specific range.

Syntax: list[start:stop:step]

Example:

my_list = [10, 20, 30, 40, 50, 60]

print(my_list[1:4]) # Output: [20, 30, 40]


print(my_list[:3]) # Output: [10, 20, 30]
print(my_list[::2]) # Output: [10, 30, 50]
print(my_list[::-1]) # Output: [60, 50, 40, 30, 20, 10] (reversed list)

3. Explain Dictionary Manipulation


A dictionary in Python stores key-value pairs. It is mutable, unordered, and indexed by keys.

Basic Manipulations:

# Creating a dictionary
student = {'name': 'Alice', 'age': 21, 'grade': 'A'}

# Accessing values
print(student['name']) # Output: Alice

# Adding a new key-value pair


student['subject'] = 'Math'

# Modifying an existing value


student['grade'] = 'A+'

# Deleting a key
del student['age']

# Iterating through keys and values


for key, value in student.items():
print(key, ":", value)
4. What Do You Understand by Python Function?
A function in Python is a reusable block of code that performs a specific task. It helps in
modularity and code reusability.

Defining and Calling a Function:

def greet(name):
print("Hello,", name)

greet("John") # Output: Hello, John

Function with Return Value:

def add(a, b):


return a + b

result = add(5, 3)
print(result) # Output: 8

5. Explain List Manipulation with the Help of Example


List manipulation refers to modifying and working with lists using built-in functions and
methods.

Examples:

numbers = [1, 2, 3, 4, 5]

# Append element
numbers.append(6) # [1, 2, 3, 4, 5, 6]

# Insert at position
numbers.insert(2, 99) # [1, 2, 99, 3, 4, 5, 6]

# Remove element
numbers.remove(3) # Removes first occurrence of 3

# Pop element
numbers.pop() # Removes last item
# Sort the list
numbers.sort() # [1, 2, 4, 5, 6, 99]

# Reverse the list


numbers.reverse() # [99, 6, 5, 4, 2, 1]

You might also like