Assignment
Assignment
Branch (CSE)
SUBJECT - PYTHON
Example:
text = "Python"
# Access characters
print(text[0]) # Output: P
# Slicing
print(text[0:3]) # Output: Pyt
# Length
print(len(text)) # Output: 6
# Concatenation
print(text + " Programming") # Output: Python Programming
# Repetition
print(text * 2) # Output: PythonPython
# Conversion
print(text.upper()) # Output: PYTHON
print(text.lower()) # Output: python
# Search
print("th" in text) # Output: True
Syntax: list[start:stop:step]
Example:
Basic Manipulations:
# Creating a dictionary
student = {'name': 'Alice', 'age': 21, 'grade': 'A'}
# Accessing values
print(student['name']) # Output: Alice
# Deleting a key
del student['age']
def greet(name):
print("Hello,", name)
result = add(5, 3)
print(result) # Output: 8
Examples:
numbers = [1, 2, 3, 4, 5]
# Append element
numbers.append(6) # [1, 2, 3, 4, 5, 6]
# Insert at position
numbers.insert(2, 99) # [1, 2, 99, 3, 4, 5, 6]
# Remove element
numbers.remove(3) # Removes first occurrence of 3
# Pop element
numbers.pop() # Removes last item
# Sort the list
numbers.sort() # [1, 2, 4, 5, 6, 99]