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

Python Lab 3

The document provides an overview of various data structures in Python, including lists, tuples, dictionaries, sets, arrays, stacks, queues, and string operations. Each data structure is described with its characteristics, syntax, and sample programs. Additionally, it covers string slicing and various string methods for text processing.

Uploaded by

bhelhostel3
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)
2 views10 pages

Python Lab 3

The document provides an overview of various data structures in Python, including lists, tuples, dictionaries, sets, arrays, stacks, queues, and string operations. Each data structure is described with its characteristics, syntax, and sample programs. Additionally, it covers string slicing and various string methods for text processing.

Uploaded by

bhelhostel3
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/ 10

Python Lab Task 3 – Data Structures

1. List

Theory

A list is an ordered, mutable collection in Python. It can store mixed data types and allows
dynamic sizing.

• Mutable (can be changed)


• Maintains insertion order
• Supports indexing, slicing, and iteration.

Syntax

Sample Programs
2. Tuple

Theory

Tuples are immutable, ordered sequences. Ideal for fixed collections like coordinates or months.
• Immutable (cannot be changed)

• Ordered and indexable

Syntax

Sample Programs

3. Dictionary

Theory

Dictionaries are key-value pairs with fast lookups. Use for structured data like student records.
• Key-based access
• Unordered but optimized for speed

• Mutable

Syntax

Sample Programs

4. Set

Theory

Sets store unique elements with no guaranteed order. Ideal for deduplication and mathematical
operations.

• No duplicates

• Unordered
• Efficient union/intersection
Syntax

Sample Programs

5. Array (array module)

Theory

array.array provides compact, typed arrays. Useful when memory and performance are critical.

• Homogeneous type (e.g. all integers)

• 🗜 More space-efficient than lists

Syntax

Sample Programs
6. Stack (LIFO)

Theory

A stack is a collection that uses Last-In-First-Out (LIFO). Used in undo systems, recursion, and
parsing.

Syntax (using list)

Sample Programs
7. Queue (FIFO)

Theory

A queue uses First-In-First-Out (FIFO). Useful in task scheduling, buffering, and simulations.

Syntax (using deque)

Sample Programs

8. String Operations

Theory

Strings are immutable sequences of characters. Key for text processing, searching, formatting.

Syntax
Sample Programs

Various operations (methods) on Strings. Use these for more understanding.

Method Description Example Output

upper() Converts all letters to uppercase "hello".upper() → "HELLO"

lower() Converts all letters to lowercase "Python".lower() → "python"

strip() Removes leading/trailing whitespace " hello ".strip() → "hello"

title() Capitalizes first letter of each word "python lab".title() → "Python Lab"

count(x) Returns number of times x appears "banana".count("a") → 3

replace(a,b) Replaces a with b "abc".replace("a","x") → "xbc"

[::-1] Reverses the entire string (slicing) "madam"[::-1] → "madam"


What Is Slicing?

Slicing lets you extract a substring from a given string using this syntax:

start – index to begin (inclusive)

• stop – index to end (exclusive)

• step – gap between characters


If omitted:

• start defaults to 0
• stop goes to end of string

• step defaults to 1

Examples

Extract a Substring

From index 0 to 5 (6 is excluded)

Skip Characters with Step


Takes every second character

Reverse the String

[::-1] means: start at end, go backward one step at a time

Slice from the Middle

Picks letters between index 4 and 9.

Negative Indices

-3 starts counting from the end.

Slicing never throws an error if the indices go out of bounds—it just returns what it can.
That makes it safe for experimentation

You might also like