Algorithm A set of rules or steps used to solve a problem
Data Structure - A particular way of organizing data in a computer
primitive data type: int, float, str بيانات أولية وليست تجميعية ، فقط استطيع تخزين قيمة واحدة في المتغير
x = 10
print(x)
x = 20
print(x)
List is a collect. We can store many values in one variable
x = [10, 20]
print(x)
print(type(x))
# differet types in one collection
x = [10, 'Ahmed']
print(x)
# different types: str, int, float
print(['red', 24, 98.6])
# list constant
print(3)
print([1, 24, 76])
x = [1, [5, 6], 7]
print(x)
print(x[0])
print(x[1])
print(x[1][0])
# empty lists
x = []
y = list()
print('x = ', x)
print('y = ', y)
# Empty String
empty_str = ''
print(empty_str)
# iteration variable
for num in [5, 4, 3, 2, 1] :
print(num)
print('Blastoff!')
# iteration variable
friends = ['Joseph', 'Glenn', 'Sally']
for name in friends:
print('Happy New Year:', name)
print('Done!')
# loop using index
friends = ['Joseph', 'Glenn', 'Sally']
for i in range(len(friends)):
print('Happy New Year:', friends[i])
print('Done!')
# access elements by index
print(friends[1])
# mutable list
friends[1] = 'Ahmed'
print(friends)
# immutable list
fruit = 'banana'
print(fruit)
print(fruit[1])
fruit[1] = 'e' # error
# convert to mutable
f = list(fruit)
print(f) # mutable
f[1] = 'e'
print(f)
fruit = ''.join(f)
print(fruit)
# join list
l1 = [1, 3, 10, 19]
print(l1)
l2 = [str(n) for n in l1]
print('\t'.join(l2))
# loop on list
friends = ['Joseph', 'Glenn', 'Sally']
# way 1: iteration variable
for friend in friends :
print('Happy New Year:', friend)
# loop on list
friends = ['Joseph', 'Glenn', 'Sally']
# way 2: index
for i in range(len(friends)) :
print('Happy New Year:', friends[i])
a = [1, 2, 3]
b = [4, 5, 6]
# concat
c = a + b
print(c)
d = a - b # error
# sum 2 lists
a = [1, 2, 3]
b = [4, 5, 6]
c = [a[i] + b[i] for i in range(len(a))] # using index
print(c)
# sum 2 lists
a = [1, 2, 3]
b = [4, 5, 6]
c = [m + n for m, n in zip(a, b)] # using iteration variables
print(c)
a = [1, 2, 3]
b = [4, 5, 6]
print(list(zip(a, b)))
# slicing a list
t = [9, 41, 12, 3, 74, 15]
print(t)
print(t[2:4]) # index 2, 3
print(t[2:]) # index 2 to the end
print(t[:4]) # index 0 to 3
# list mehtods
t = [9, 41, 12, 3, 74, 15]
print(dir(t))
print(t)
t.append(10) # add 3 to the end of the list
print(t)
# sum 2 lists using empty list with append
a = [1, 2, 3]
b = [4, 5, 6]
c = []
print('before sum: c = ', c)
list_size = len(a)
for i in range(list_size):
r = a[i] + b[i]
c.append(r)
print(i, c)
# in one line
# c = [a[i] + b[i] for i in range(len(a))]
# c = [m + n for m, n in zip(a, b)]
# count
t = [9, 41, 12, 3, 74, 15]
print(t)
print(t.count(3))
print(t.count(13))
print(t.count(74))
t.append(3)
print(t)
print(t.count(3))
t = [9, 41, 12, 3, 74, 15]
print(t)
t.extend([1, 2, 15]) # similar to t + [1, 2, 5] # like concat
print(t)
# extend vs append
t = [9, 41, 12, 3, 74, 15]
t.append([1, 2, 15])
print(t)
t = [9, 41, 12, 3, 74, 15]
print(t)
t.index(9)
t.index(5) # does not exist --> value error
t = [9, 41, 12, 3, 74, 15]
print(t)
t.insert(3, 5) # insert 5 at index 3
print(t)
return value and remove from the end of list
t = [9, 41, 12, 3, 74, 15]
print('before', t)
print('call pop', t.pop())
print('after', t)
t = [9, 41, 12, 3, 74, 15, 3]
print(t)
t.remove(12)
print(t)
t = [9, 41, 12, 3, 74, 15, 3]
print(t)
t.remove(3) # remove the first match
print(t)
t.remove(70) # error
t = [9, 41, 12, 3, 74, 15]
print(t)
t.reverse()
print(t)
t = [9, 41, 12, 3, 74, 15]
print(t)
t.sort()
print(t)
t = [9, 41, 12, 3, 74, 15]
print(t)
t.sort(reverse=True)
print(t)
t = [9, 41, 12, 3, 74, 15]
print(t)
12 in t
5 in t
nums = [3, 41, 12, 9, 74, 15]
print(len(nums))
print(max(nums))
print(min(nums))
print(sum(nums))
print(sum(nums)/len(nums))
line = 'A lot of spaces' # white space
line.split()
line = 'first;second;third'
line.split(';')
line = '10\t13\t25'
print(line)
numbers = line.split('\t')
print(numbers)
int_numbers = [int(n) for n in numbers]
print(int_numbers)
print(sum(int_numbers))
line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
words = line.split()
print(words)
email = words[1]
print(email)
pieces = email.split('@')
print(pieces)
year = words[-1]
print(year)