[go: up one dir, main page]

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

1.b.sequence

The document explains the four built-in sequence types in Python: strings, lists, tuples, and ranges. It details their characteristics, such as mutability, indexing, and common operations. Additionally, it provides practical examples for manipulating these sequence types.

Uploaded by

kalpanapriyam213
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)
6 views4 pages

1.b.sequence

The document explains the four built-in sequence types in Python: strings, lists, tuples, and ranges. It details their characteristics, such as mutability, indexing, and common operations. Additionally, it provides practical examples for manipulating these sequence types.

Uploaded by

kalpanapriyam213
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/ 4

Sequence Data Types in Python

A sequence in Python is an ordered collection of items that can be indexed and sliced. Python provides four
built-in sequence types:

1. String (str) – A sequence of characters


2. List (list) – A mutable (changeable) collection of items
3. Tuple (tuple) – An immutable (unchangeable) collection of items
4. Range (range) – A sequence of numbers

1. String (str)
A string is a collection of characters enclosed in single ('), double ("), or triple quotes (''' or """ """).

Creating Strings
str1 = 'Hello' # Single quotes
str2 = "Python" # Double quotes
str3 = '''Welcome''' # Triple quotes (also used for multi-line strings)

print(str1, str2, str3)

Output:

Hello Python Welcome

String Operations
text = "Python"

# Accessing characters
print(text[0]) # Output: P (first character)
print(text[-1]) # Output: n (last character)

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

# String Concatenation
print("Hello " + "World") # Output: Hello World

# String Repetition
print("Python! " * 3) # Output: Python! Python! Python!

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

Common String Methods


msg = "hello world"

print(msg.upper()) # HELLO WORLD


print(msg.lower()) # hello world
print(msg.title()) # Hello World
print(msg.replace("world", "Python")) # hello Python
print(msg.split()) # ['hello', 'world']

2. List (list)
A list is a collection of ordered, mutable (changeable) elements. It can store different data types.

Creating Lists
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
mixed = [1, "hello", 3.5, True]

print(fruits)
print(numbers)
print(mixed)

List Operations
# Accessing Elements
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry

# Slicing
print(fruits[1:3]) # Output: ['banana', 'cherry']

# Modifying a List
fruits[1] = "blueberry" # Changing an element
print(fruits) # Output: ['apple', 'blueberry', 'cherry']

# Adding Elements
fruits.append("orange") # Adds at the end
print(fruits) # ['apple', 'blueberry', 'cherry', 'orange']

fruits.insert(1, "grape") # Adds at index 1


print(fruits) # ['apple', 'grape', 'blueberry', 'cherry', 'orange']

# Removing Elements
fruits.remove("cherry") # Removes specific element
print(fruits)

fruits.pop() # Removes last element


print(fruits)

# Length of List
print(len(fruits)) # Output: 3

Iterating Over a List


for fruit in fruits:
print(fruit)

3. Tuple (tuple)
A tuple is similar to a list but immutable (cannot be changed).

Creating Tuples
numbers = (10, 20, 30, 40)
fruits = ("apple", "banana", "cherry")

print(numbers)
print(fruits)

Tuple Operations
# Accessing Elements
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry

# Slicing
print(fruits[1:3]) # Output: ('banana', 'cherry')

# Length of Tuple
print(len(fruits)) # Output: 3

Tuple Immutability
# This will cause an error because tuples cannot be changed
# fruits[1] = "blueberry"

Tuple Packing and Unpacking


person = ("John", 30, "Engineer") # Packing
name, age, job = person # Unpacking

print(name) # Output: John


print(age) # Output: 30
print(job) # Output: Engineer

4. Range (range)
A range represents a sequence of numbers.

Creating Ranges
r1 = range(5) # 0 to 4
r2 = range(2, 10) # 2 to 9
r3 = range(1, 10, 2) # 1 to 9 with step 2

print(list(r1)) # Output: [0, 1, 2, 3, 4]


print(list(r2)) # Output: [2, 3, 4, 5, 6, 7, 8, 9]
print(list(r3)) # Output: [1, 3, 5, 7, 9]

Using range() in Loops


for i in range(5):
print(i) # Output: 0 1 2 3 4
Practical Example: Working with Sequences
Finding the Longest Word in a List
words = ["apple", "banana", "strawberry", "mango"]

longest_word = max(words, key=len) # Finds the longest word


print("Longest word:", longest_word) # Output: strawberry

Reversing a String and List


text = "Python"
reversed_text = text[::-1]
print("Reversed String:", reversed_text) # Output: nohtyP

numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print("Reversed List:", numbers) # Output: [5, 4, 3, 2, 1]

Summary
Sequence Type Mutable? Indexed? Allows Duplicate Values?
String (str) No Yes Yes
List (list) Yes Yes Yes
Tuple (tuple) No Yes Yes
Range (range) No Yes No

✔ Strings are used for text manipulation.


✔ Lists are used for collections that need modification.
✔ Tuples are immutable collections (faster than lists).
✔ Range is used for generating number sequences.

You might also like