Strings
A string stores textual data. Defined using single or double quotes.
integer_var = 100
string_var = "sample text"
Output Formatting
employee = 'Bob'
job_title = 'Developer'
office = 'Innovation Hub, Floor 3'
print('-------------------')
print(f"Name: {employee}")
print(f"Position: {job_title}")
print(f"Location: {office}")
print('-------------------')
User Input Handling
Input values are stored as strings by default.
username = input('Enter login ID: ')
print(f"Login ID: {username}")
access_key = input('Enter security key: ')
print(f"Security key: {access_key}")
Indexing and Slicing
Acces characters via zero-based indices.
data_string = "algorithm"
print(data_string[0]) # 'a'
print(data_string[4]) # 'o'
# Substring extraction
print(data_string[0:4]) # 'algo'
print(data_string[3:]) # 'orithm'
print(data_string[::-1]) # 'mhtirogla'
Common String Methods
sample = "data processing example"
# Search operations
print(sample.find('proc')) # 5
print(sample.count('a')) # 3
# Transformation
print(sample.upper()) # 'DATA PROCESSING EXAMPLE'
print(sample.replace('a', '@')) # 'd@t@ process@ng ex@mple'
# Validation
print(sample.isalpha()) # False
print('123'.isdigit()) # True
Lists
Ordered mutable collections supporting mixed data types.
team_members = ['Anna', 'Ben', 'Clara']
mixed_data = [42, 'text', 3.14]
Iteration Tehcniques
For loop:
for member in team_members:
print(member)
While loop:
index = 0
while index < len(team_members):
print(team_members[index])
index += 1
List Modification
Append: Add single element
team_members.append('David')
Extend: Merge collections
additional = ['Eva', 'Frank']
team_members.extend(additional)
Insert: Position-specific addition
team_members.insert(1, 'Xander')
Modify: Update by index
team_members[2] = 'Carlos'
Remove:
del team_members[3] # By index
team_members.pop() # Last element
team_members.remove('Ben') # By value
Sorting:
numbers = [5, 2, 8, 1]
numbers.sort() # Ascending
numbers.sort(reverse=True) # Descending
numbers.reverse() # In-place reversal
Nested Lists
departments = [
['HR', 'Finance'],
['Engineering', 'QA'],
['Marketing', 'Sales']
]
Tuples
Immutable ordered sequences defined with parentheses.
coordinates = (40.7128, -74.0060)
mixed_tuple = ('A', 100, 2.5)
Access Patterns
print(coordinates[0]) # 40.7128
print(mixed_tuple[1:]) # (100, 2.5)
Built-in Methods
test_tuple = ('a', 'b', 'c', 'a')
print(test_tuple.count('a')) # 2
print(test_tuple.index('c')) # 2
Dictionaries
Key-value pair collections with unique keys.
employee_record = {
'id': 789,
'name': 'Sarah',
'department': 'Research'
}
Value Access
print(employee_record['name']) # 'Sarah'
print(employee_record.get('title', 'N/A')) # Default handling
Dictionary Operations
Update:
employee_record['id'] = 790
Add new entry:
employee_record['status'] = 'Active'
Delete:
del employee_record['department']
employee_record.clear() # Empty contents
Dictionary Analysis
print(len(employee_record)) # Key count
print(list(employee_record.keys())) # All keys
print(list(employee_record.values())) # All values
Common Operations
Operators
# Concatenation
print([1, 2] + [3, 4]) # [1, 2, 3, 4]
print('Hi' * 3) # 'HiHiHi'
# Membership test
print('a' in ('a', 'b')) # True
Built-in Functions
# Length calculation
print(len({'x': 1, 'y': 2})) # 2
# Extremes identification
print(max([5, 8, 2])) # 8
# Element deletion
temp_list = [10, 20, 30]
del temp_list[1]
Mutability Characteristics
Mutable types: Lists, dictionaries (modifiable in-place) Immutable types: Strings, numbers, tuples (create new instances when changed)