programmingdoor.com

Python Arrays and Strings: A Complete Guide to mastery

Blogprogrammingpython

Basic data structures that are essential to computer science and programming are arrays and strings. We will explore the properties, functions, and real-world uses of arrays and strings in this blog article, all while utilizing the flexible Python programming language. Together, let’s set out to become experts on these basic data structures!Comprehending Python ArraysElements in an array are grouped together and uniquely recognized by a key or index. Since Python is a dynamically-typed language, lists are a strong and adaptable array-like structure. Let’s examine the fundamentals of Python arrays with some examples.

Creating Arrays (List) in python

# Creating a simple integer array
integer_array = [1, 2, 3, 4, 5]

# Creating a mixed-type array
mixed_array = [1, 'two', 3.0, [4, 5]]
  • accessing and modifying Array elements
# Accessing elements by index
print(integer_array[2])  # Output: 3

# Modifying elements
mixed_array[1] = 'three'
print(mixed_array)  # Output: [1, 'three', 3.0, [4, 5]]
  • array Slicing
# Slicing an array
subset = integer_array[1:4]
print(subset)  # Output: [2, 3, 4]
  • concatination
# Concatenating arrays
new_array = integer_array + [6, 7, 8]
print(new_array)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]
  • searching and sorting
# Searching for an element
index_of_three = integer_array.index(3)
print(index_of_three)  # Output: 2

# Sorting an array
sorted_array = sorted(integer_array)
print(sorted_array)  # Output: [1, 2, 3, 4, 5]

Python Strings: More Than Just Text

Strings in Python are character sequences surrounded by single or double quotes. They enable for a variety of operations, much as arrays. Let’s investigate Python’s string richness.

  • Creating and accessing Strings
# Creating strings
string1 = 'Hello'
string2 = "World"

# Accessing characters
print(string1[1])  # Output: 'e'
  • String concatenation and Repetation
# Concatenating strings
concatenated_string = string1 + ' ' + string2
print(concatenated_string)  # Output: 'Hello World'

# Repeating strings
repeated_string = string1 * 3
print(repeated_string)  # Output: 'HelloHelloHello'
  • String methods
# String methods
uppercase_string = string1.upper()
print(uppercase_string)  # Output: 'HELLO'

# Splitting a string
words = concatenated_string.split()
print(words)  # Output: ['Hello', 'World']

Real World Example

  • Finding duplicates in arrays
def find_duplicates(nums):
    seen = set()
    duplicates = []

    for num in nums:
        if num in seen:
            duplicates.append(num)
        else:
            seen.add(num)

    return duplicates

numbers = [1, 2, 3, 4, 2, 5, 6, 7, 8, 9, 1]
print(find_duplicates(numbers))  # Output: [2, 1]
  • Reversing words in arrays
def reverse_words(sentence):
    words = sentence.split()
    reversed_sentence = ''.join(reversed(words))
    return reversed_sentence

sentence = "Python is amazing"
print(reverse_words(sentence))  # Output: 'amazing is Python'