Python String Operators and List Functions Reference

Python String Operators and Operations

Discuss various string operators in Python:

1. Concatenation Operator

The + operator is used to concatenate two or more strings.

Python Code Example:

str1 = "Hello"
str2 = "World"
print(str1 + " " + str2)

Output: Hello World

2. Repetition Operator

The * operator is used to repeat a string a specified number of times.

Python Code Example:

str1 = "Hello"
print(str1 * 3)

Output: HelloHelloHello

3. Indexing Operation ([])

The [] operator is used to access a character at a specified index in a string.

Python Code Example:

str1 = "Hello"
print(str1[0])

Output: H

4. Slicing Operator ([start:stop:step])

The [] operator is used to extract a subset of characters from a string.

Python Code Examples:

str1 = "Hello World"
print(str1[0:5])  # Output: Hello
print(str1[6:])   # Output: World
print(str1[::2])  # Output: HloWrd

Commonly Used List Functions in Python

  • append(): Adds an element to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
  • extend(): Adds multiple elements to the end of the list.
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
  • insert(): Inserts an element at a specified position in the list.
my_list = [1, 2, 3]
my_list.insert(1, 4)
print(my_list) # Output: [1, 4, 2, 3]
  • remove(): Removes the first occurrence of a specified element from the list.
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list) # Output: [1, 3, 2]
  • count(): Returns the number of occurrences of a specified element in the list.
my_list = [1, 2, 3, 2]
count = my_list.count(2)
print(count) # Output: 2
  • sort(): Sorts the list in ascending order (in-place).
my_list = [3, 1, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3]

Lambda Functions in Python

A lambda function, also known as an anonymous function, is a small, one-line function that can be defined inline within a larger expression.

Syntax

lambda arguments: expression

Example

double = lambda x: x * 2
print(double(5))

Output: 10

How It Works

  1. The lambda keyword is used to define a lambda function.
  2. The arguments part specifies the input parameters of the function.
  3. The expression part specifies the code that will be executed when the function is called.

Key Characteristics

  • Anonymous: Lambda functions do not have a name.
  • Single Expression: Lambda functions can only contain a single expression.
  • Inline: Lambda functions are defined inline within a larger expression.

Use Cases

  1. Simple Transformations: list(map(lambda x: x**2, [1, 2, 3, 4, 5]))
  2. Event Handling: button_click_handler = lambda event: print("Button clicked!")
  3. Data Processing: sorted_data = sorted(data, key=lambda x: x['age'])

Benefits

  • Concise Code: Lambda functions are shorter than regular functions.
  • Readability: Can improve code readability by avoiding unnecessary function definitions.
  • Flexibility: Can be used as higher-order functions or as arguments to other functions.

File Types and Handling in Python

Python supports various file types, including:

  • Text Files: Files containing text data, such as .txt, .csv, .json, etc.
  • Binary Files: Files containing binary data, such as images, audio, video, etc.

Text Files Operations

  • Reading Text Files: Use the read() method to read the contents of a text file.
  • Writing Text Files: Use the write() method to write to a text file.

Binary Files Operations

  • Reading Binary Files: Use the read() method to read the contents of a binary file.
  • Writing Binary Files: Use the write() method to write to a binary file.

File Modes

  • r: Read mode
  • w: Write mode
  • a: Append mode
  • x: Exclusive creation mode
  • b: Binary mode
  • t: Text mode (default)

File Handling Example

Reading and writing a text file:

# Read a text file
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

# Write to a text file
with open('example.txt', 'w') as file:
    file.write('Hello, World!')