0% found this document useful (0 votes)
2 views

Python Revision Tour II Notes

The document provides a comprehensive overview of Python, covering key concepts such as strings, lists, tuples, and dictionaries, along with their properties and operations. It highlights the immutability of strings and tuples, the mutability of lists and dictionaries, and includes examples of various operations and functions. Additionally, it introduces basic sorting techniques like bubble sort and insertion sort.

Uploaded by

aman11211369y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Revision Tour II Notes

The document provides a comprehensive overview of Python, covering key concepts such as strings, lists, tuples, and dictionaries, along with their properties and operations. It highlights the immutability of strings and tuples, the mutability of lists and dictionaries, and includes examples of various operations and functions. Additionally, it introduces basic sorting techniques like bubble sort and insertion sort.

Uploaded by

aman11211369y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Revision Tour - II

2.1 Introduction
Python is a high-level, interpreted programming language that supports multiple paradigms like
OOPs and procedural programming. It is simple and readable.

Important:
- Interpreted
- High-level

2.2 Strings in Python

2.2.1 Item Assignment not Supported


Strings are immutable; you cannot change a character directly by assigning a new value.

Important:
- Strings are immutable.

s = "hello"
s[0] = "H" # Error: TypeError

2.2.2 Traversing a String


You can access each character using a loop.

Important:
- Use loops to access characters.

for ch in "hello":
print(ch)

2.2.3 String Operators


Operators like +, *, in work on strings.

Important:
Python Revision Tour - II

- + joins, * repeats.

print("Py" + "thon") # Python


print("a" * 3) # aaa

2.2.4 String Slices


Slicing extracts parts of a string using indexes.

Important:
- Syntax: string[start:end]

s = "Python"
print(s[0:3]) # Pyt

2.2.5 String Functions


Built-in functions like len(), lower(), upper() are used.

Important:
- String manipulation made easy.

s = "Python"
print(s.upper()) # PYTHON

2.3 Lists in Python

2.3.1 Creating Lists


Lists store multiple items in a single variable.

Important:
- Lists are mutable.

list1 = [1, 2, 3]

2.3.2 Lists vs. Strings


Python Revision Tour - II

Lists are mutable; strings are immutable. Both are sequences.

Important:
- Lists can be modified.

list1[0] = 5 # Works
# s[0] = "a" # Error for strings

2.3.3 List Operations


Operators like +, *, in, not in work.

Important:
- + merges, * repeats.

list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2) # [1, 2, 3, 4]

2.3.4 List Manipulation


Methods like append(), insert(), remove() modify lists.

Important:
- Lists are flexible.

list1 = [1, 2, 3]
list1.append(4)

2.3.5 Making True Copy of a List


Copy using list.copy() or slicing.

Important:
- Avoids reference issues.

list1 = [1, 2]
list2 = list1.copy()
Python Revision Tour - II

2.3.6 List Functions


Functions like len(), max(), min() are used with lists.

Important:
- Useful for analyzing list data.

list1 = [1, 2, 3]
print(len(list1)) # 3

2.4 Tuples in Python

2.4.1 Creating Tuples


Tuples are immutable sequences.

Important:
- Use parentheses ().

t = (1, 2, 3)

2.4.2 Tuples vs. Lists


Tuples are immutable, lists are mutable.

Important:
- Tuples are faster than lists.

t = (1, 2)
# t[0] = 5 # Error

2.4.3 Tuple Operations


Similar operators like in lists: +, *, in.

Important:
- Tuples support operations.
Python Revision Tour - II

t = (1, 2)
print(t + (3, 4)) # (1, 2, 3, 4)

2.4.4 Tuple Functions and Methods


Functions like len(), count(), index() are used.

Important:
- Methods help analyze tuples.

t = (1, 2, 1)
print(t.count(1)) # 2

2.5 Dictionaries in Python

2.5.1 Creating a Dictionary


Dictionaries store key-value pairs.

Important:
- Keys must be unique.

d = {"name": "John", "age": 25}

2.5.2 Accessing Elements of a Dictionary


Access elements by key.

Important:
- Use keys to access values.

d = {"name": "John"}
print(d["name"]) # John

2.5.3 Characteristics of a Dictionary


Unordered, mutable, keys must be unique and hashable.
Python Revision Tour - II

Important:
- No order guaranteed.

d = {"a": 1, "b": 2}

2.5.4 Dictionary Operations


Use in, del, updating values etc.

Important:
- Dictionaries are mutable.

d = {"a": 1}
d["a"] = 2

2.5.5 Dictionary Functions and Methods


Functions like keys(), values(), items() are useful.

Important:
- Helps retrieve information.

d = {"a": 1}
print(d.keys()) # dict_keys(['a'])

2.6 Sorting Techniques

2.6.1 Bubble Sort


Repeatedly swaps adjacent elements if they are in wrong order.

Important:
- Simple but slow for large lists.

arr = [5, 1, 4]
for i in range(len(arr)):
for j in range(len(arr) - i - 1):
Python Revision Tour - II

if arr[j] > arr[j+1]:


arr[j], arr[j+1] = arr[j+1], arr[j]
print(arr) # [1, 4, 5]

2.6.2 Insertion Sort


Builds sorted array by inserting elements one by one.

Important:
- Efficient for small lists.

arr = [5, 2, 4]
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
print(arr) # [2, 4, 5]

You might also like