Python Assignment
Python Assignment
STRING :
A string is a sequence of characters, typically used to represent text. In most
programming languages, including Python, C, Java, and many others, a string is
a data type that allows you to store and manipulate a series of characters,
such as letters, numbers, symbols, and whitespace.
You can create a string by enclosing characters in either single quotes (') or
double quotes (").
For example:
string1 = "This is a string."
string2 = 'This is also a string.'
# Example:
text = "Hello, World!"
length = custom_len(text)
print("Custom Length:", length) # Output: 13
# Example:
text = "Hello, World!"
lowercase_text = custom_lower(text)
print("Custom Lower:", lowercase_text) # Output: "hello, world!"
# Example:
text = " Hello, World! "
stripped_text = custom_strip(text)
print("Custom Strip:", stripped_text) # Output: "Hello, World!"
IDENTIFY COMMON SYNTACTICAL ERRORS
WHEN WORKING WITH ARRAYS AND STRINGS
When working with arrays and strings in programming, there are several
common syntactical errors that developers often make. These errors can lead to
runtime issues, logical bugs, and unexpected behavior. Here are some of the
most common syntactical errors to watch out for when working with arrays
and string
1 : Off-by-One Errors:
These occur when you access an array or string element with an incorrect
index.
str1 = "Hello"
str2 = "World"
if str1 != str2:
# Incorrect string comparison (use ==)
9 : Case Sensitivity:
Some programming languages are case-sensitive, meaning that "myString"
and "mystring" are considered different variables or identifiers. Ensure
consistent casing.
• Error: Not initializing arrays and strings properly before using them,
leading to unpredictable behavior.
Example:
char myString[10]; // Uninitialized string
EXPERIMENT NO 8
ARRAYS:
Arrays are fundamental data structures in programming that allow you to
store and manipulate collections of data. Here are some common use cases
for arrays, along with examples of how to solve them using arrays in
various programming languages.
1: Storing a List of Items:
Use an array to store and manage a list of items.
Example:
const numbers = [1, 2, 3, 4, 5];
3: Modifying Elements:
Change the value of an element at a specific index.
Example:
const colors = ['red', 'green', 'blue'];
colors[0] = 'pink';
console.log(colors); // Output: ['pink', 'green', 'blue']
4: Adding Elements:
Add elements to the end of an array using ‘push’.
Example:
const animals = ['cat', 'dog'];
animals.push('elephant');
console.log(animals); // Output: ['cat', 'dog', 'elephant']
5: Removing Elements:
Remove elements from the end of an array using ‘pop’.
Example:
const fruits = ['apple', 'banana', 'cherry'];
fruits.pop();
console.log(fruits); // Output: ['apple', 'banana']
8: Filtering Elements:
Create a new array by filtering elements that meet a certain condition.
Example:
const numbers = [10, 20, 30, 40, 50];
const filteredNumbers = numbers.filter(num => num > 25);
console.log(filteredNumbers); // Output: [30, 40, 50]
9: Sorting an Array:
Sort the elements in an array in ascending order.
Example:
const fruits = ['banana', 'cherry', 'apple'];
fruits.sort();
console.log(fruits); // Output: ['apple', 'banana', 'cherry']