Hints AI Session 2.ipynb - Colab
Hints AI Session 2.ipynb - Colab
Modification Methods
index(x, start, end) – Returns the index of the first occurrence of an element.
count(x) – Returns the number of times an element appears in the list.
sort(reverse=False, key=None) – Sorts the list in ascending order (or descending if reverse=True).
reverse() – Reverses the order of elements in the list. Copying & Other Operations
copy() – Returns a shallow copy of the list.
len(list) – Returns the number of elements in the list.
max(list) – Returns the largest element.
min(list) – Returns the smallest element.
sum(list) – Returns the sum of elements (if they are numeric).
sorted(list, reverse=False, key=None) – Returns a new sorted list without modifying the original.
any(iterable) – Returns True if at least one element is truthy.
all(iterable) – Returns True if all elements are truthy.
x = [55, 22, 33, 77, 26]
x.sort() # By default it will sort ascending order.
print(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-5ea86c8edc8d> in <cell line: 0>()
1 x = [55, 22, 33, 77, "W"]
----> 2 x.sort() # By default it will sort ascending order.
None
print(x)
x.sort()
print(x)
x = "welcome"
print(x.replace("e", "a"))
walcoma
print(x)
welcome
77
print(x.append(68))
None
print(sorted(x))
77
print(min(x))
55
print(sum(x))
198
print(sorted(x, reverse=True))
x = [5, 6, 7, 8, 9]
y = x
y[3] = 66
print(x)
print(y)
[5, 6, 7, 66, 9]
[5, 6, 7, 66, 9]
x = [5, 6, 7, 8, 9]
y = x[:]
y[3] = 66
print(x)
print(y)
[5, 6, 7, 8, 9]
[5, 6, 7, 66, 9]
[5, 6, 7, 8, 9]
[5, 6, 7, 66, 9]
Creates a new object but does not recursively copy nested objects.
If the list contains mutable objects (e.g., other lists or dictionaries), they are referenced rather than copied.
Changes to nested objects in the copied list affect the original list.
# Deep Copy
import copy
x = [5, 6, 7, 8, 9, [5, 6, 7]]
y = copy.deepcopy(x)
x[5][2] = 77
y[3] = 66
y[5][1] = 22
print(x)
print(y)
x = [5, 6, 7]
x.reverse()
print(x)
[7, 6, 5]
Get the second largest number in list: [5, 77, 66, 20, 35] in one line.
x = [5, 77, 66, 20, 35] # [5, 20,, 35,, 66, 77]
x.sort()
x[-2]
66
sorted(x)[-2]
66
x = [77, -5, 7]
all(x) # And
# any value not 0 this evaluates to True.
x = [] # This evaluates to False.
True
x = [0, 0, 0]
any(x) # Or
False
Lis Comprehension
keyboard_arrow_down Tuples
# Tuples ==> Colletion of elements that can be with diffferent datatypes.
# Tuples are Immutable ==> Can't be Modified.
# Tuples are defined using ()
x = (5, 6, 7, 8, 9, True, "Welcome")
print(x[0])
print(x[-1])
Welcome
print(x[1:5])
(6, 7, 8, 9)
x[::-1]
('Welcome', True, 9, 8, 7, 6, 5)
x.index(True)
x[0] = 55
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-61-4d0c2147cdf2> in <cell line: 0>()
----> 1 x[0] = 55
# Use Casting
x = (5, 6, 7, 8, 9, True, "Welcome", "Welcome")
x = list(x)
x[0] = 55
x = tuple(y)
print(x)
x = (4,) # Tuples with only one value, the value must be followed by a comma.
print(type(x))
<class 'tuple'>
Assigns multiple values to multiple variables in a single line. x is assigned 5, and y is assigned "welcome".
# Multiple Assignment
x, y = 5, "welcome"
(x, y, z) = (5, 6, 7)
x, y, z = (5, 6, "hello")
(x, y, z) = 5, 6, "hello"
input()
string name;
cin>>name;
print(name)
Mostafa
x = input("Enter 5 values ").split() # "55 66 welcome 77 hello".split() ==> [55, 66, welcome, 77, hello]
Enter 5 values 55 66 welcome 77 hello
print(x)
Welcome AI_Hello_
Welcome_Hello
5
Okay
x = 55
if x%2 != 0:
print("odd")
print("Hello")
print("Okay")
odd
Hello
Okay
== – Equal to
!= – Not equal to
> – Greater than
< – Less than
>= – Greater than or equal to
<= – Less than or equal to
Excellent
x = 55
# or
if x == 55 or x > 100:
print("Hello")
Hello
For Loops
# Loops ==> Iterates over something.
x = [11, 12, 3, 4, 5]
for i in x: # In each iteration i will carry a value in x.
print(i)
11
12
3
4
5
[0, 1, 2, 3, 4]
list(range(7))
[0, 1, 2, 3, 4, 5, 6]
list(range(2, 7, 2))
[2, 4, 6]
0
1
2
3
4
x = [11, 12, 3, 4, 5]
len(x) # This will return the number of elements found in x.
5
length_of_list = len(x) # 5
range_of_list = range(length_of_list) # [0, 1, 2, 3, 4]
for i in range_of_list:
print(x[i])
11
12
3
4
5
for i in range(len(x)):
print(x[i])