<jupyter_start><jupyter_text>Python Documentation* Main Python Site: www.python.org* Python 3 Documentation: https://docs.python.org/3/* Tutorial - takes about 3-4 hours to complete.  Contains many examples of the language and its usage: https://docs.python.org/3/tutorial/index.html * Standard Library: https://docs.python.org/3/library/index.html* Glossary: https://docs.python.org/3/glossary.html* Problems - w3resource.com/python-exercises/  Sequence Types List DefinitionA list is a sequence of values where the values can be of the same or different types.  A list is defined by writing a series of comma-separated values enclosed by brackets. Nomenclature    l = [1, 2, 3, 4]  List of four integers    l = ['a', 'b', 'c', 'd']  list of four characters    l = []  empty list    l = list()  empty list Operations* append(x)* clear()* copy()* extend(t)* insert(x)* pop([i])* remove(x)* reverse() Tuple DefinitionTuples are immutable sequences, typically used to store collections of heterogeneous data.  A tuple is defined by writing a series of comma-separated values encluded by parenthesis.  **Note: As an immutable type, tuple is hashable and therefore can be used as a key in a dictionary and stored in a set.** Nomenclature    t = (1, 2, 3)  Tuple of three integers    t = (1,)  Tuple of single integer    t = ('a', 'b', 'c')  Tuple of three characters    t = 'this', 'that', 'these', 'those'  Tuple of four strings    t = tuple()  Empty tuple    t = ()  Empty tuple     OperationsSee Common Sequence Operations Range DefinitionThe range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops. Nomenclature Operations Set DefinitionA set object is a mutable unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. Nomenclature    s = set([1, 2, 3, 4, 5])  Set of five integers from list    s = set(('a', 'b', 'c', 'd'))  Set of characters from tuple    s = set('asdf')  Set of four characters from string    s = set()  Empty set Operations* isdisjoint* issubset or set <= other* set < other* issuperset or set >= other* set > other* union or set | other | ...* intersection or set & other & ...* difference or set - other  - ...* symmetric_difference or set ^ other* copy* update or set |= other* intersection_update or set &= other* difference_update or set -= other* symmetric_difference_update or set ^= other* add(x)* remove(x)* discard(x)* pop()* clear() Bytes DefinitionsBytes objects are immutable sequences of single bytes. Nomenclature    b = b'12345656'  Bytes object of length 8    b = bytes(10)  Zero-filled bytes object of length 10    b = bytes(range(10))  Bytes object of length 10 filled with values 0 through 9 Operations* hex() - Converts ASCII to hex values* fromhex(x) - Converts hex string to ASCII bytes**Note: Contains many ASCII-based (string-type) operations.  Care must be taken when using these operations with non-ASCII data. ByteArrayImmutable counterpart to Bytes     Common Sequence Operations        * x in s* x not in s* s + t (concatenate)* s * n or n * s (repeat)* s[i]* s[start:stop]* s[start:stop:step]* len(s)* min(s)* max(s)* s.index(x)* s.count(x)     Exercises List Tuple Set Bytes/ByteArray Mapping Dictionary Keys Values Items ExerciseRock, Paper, Scissors Files Functions* Problem    * Count the number of words in a file     Now that we have learned some basics of programming, input, processing and output, its time to learn new ways to represent the information that is stored within a program.  The mechanisms we use to store information are called data structures.  Python has several useful builtin data structures and also allows a programmer to create her own. **List****Note: While a list can hold values of different types, typically, a list contains values of a single type.**<jupyter_code>[1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 'e']
[1, 'a', 2, 'b', 3, 'c']<jupyter_output><empty_output><jupyter_text>Python also provides a **list** function that will create a list from an iterable.  An iterable is defined as:    Iterable: An object capable of returning its members one at a time.    Iteration is a fundamental property used by many data structures throughout Python.<jupyter_code>list('abcde')
list('12345')
list(range(10))<jupyter_output><empty_output><jupyter_text>**List Operations**appendindexsliceinsertpop* Append   l = []   l.append('a')   l.append('b')   l.append('c')   l.append('d')   * Index    l = [1, 2, 3, 4, 5]    l.index[3]    l.index[0]    l.index[-1]    * Slice    l = [1, 2, 3, 4, 5]    l[0:5]    l[:]    l[3:5]    l[-1:-3]    * Push    l = []    l.push('a')    l.push('b')    l.push('c')    * Pop    l = [1, 2, 3, 4, 5]    l.pop(0) **List Exercises** 1. Write a Python program to sum all the items in a list.<jupyter_code>l = [1, 2, 3, 4, 5]
sum(l)<jupyter_output><empty_output><jupyter_text>2. Write a Python program to multiplies all the items in a list.<jupyter_code>def mul(iterable):
     x = 1
     for i in iterable:
         x*=i
     return x

l = [1, 2, 3, 4, 5]

mul(l)<jupyter_output><empty_output><jupyter_text>3. Write a Python program to get the largest number from a list.<jupyter_code>l = [1, 2, 3, 4, 5]
max(l)<jupyter_output><empty_output><jupyter_text>4. Write a Python program to get the smallest number from a list.<jupyter_code>l = [1, 2, 3, 4, 5]
min(l)<jupyter_output><empty_output><jupyter_text>5. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.<jupyter_code>s = ['this', 'that', 'these', 'those', 'it', 'what', 'wish']
        
for i in s:
    if len(i) >= 2 and i[0] == i[-1]:
        print(i)<jupyter_output>that<jupyter_text>6. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.<jupyter_code>l = [(1, 2), (3, 2), (2, 3), (5, 3), (7,2)]
sorted(l, key=lambda i: i[1], reverse=True)<jupyter_output><empty_output><jupyter_text>7. Write a Python program to remove duplicates from a list.<jupyter_code>l = [1, 2, 3, 4, 1, 5, 6, 5, 7, 6]
list(set(l))<jupyter_output><empty_output><jupyter_text>8. Write a Python program to check a list is empty or not.<jupyter_code>def is_empty(l):
    return l is None

l = [1, 2, 3, 4, 5]
print(is_empty(l))

l = None
print(is_empty(l))<jupyter_output>False
True<jupyter_text>9. Write a Python program to clone or copy a list.<jupyter_code>l = [1, 2, 3, 4, 5]
print(id(l))

m = l.copy()
print(id(m))<jupyter_output>140156395305608
140156395271560<jupyter_text>10. Write a Python program to find the list of words that are longer than n from a given list of words.<jupyter_code>l = ['this', 'that', 'these', 'those', 'which', 'what', 'who', 'where']
n = 4
m = [i for i in l if len(i) > n]
print(m)<jupyter_output>['these', 'those', 'which', 'where']<jupyter_text>11. Write a Python function that takes two lists and returns True if they have at least one common member.<jupyter_code>l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
m = [9, 5, 3, 1, 3, 8]

True if True in [
    i == j 
    for i in m 
    for j in l] else False<jupyter_output><empty_output><jupyter_text>12. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.<jupyter_code>l = [1, 3, 2, 6, 5, 7, 8, 9, 4]

r = [0, 4, 5]

m = [v for i,v in enumerate(l) if i not in r]

print(m)<jupyter_output>[3, 2, 6, 8, 9, 4]<jupyter_text>13. Write a Python program to generate a 3*4*6 3D array whose each element is *.<jupyter_code>def make_3d_array(i, j, k):
    return [[[ '*' for _i in range(i)] for _j in range(j)] for _k in range(k)]

make_3d_array(3, 4, 6)<jupyter_output><empty_output><jupyter_text>14. Write a Python program to print the numbers of a specified list after removing even numbers from it.<jupyter_code>l = [1, 4, 3, 2, 5, 7 ,4, 5, 8]
[i for i in l if i % 2 == 0]<jupyter_output><empty_output><jupyter_text>15. Write a Python program to shuffle and print a specified list.<jupyter_code>import random

l = [1, 2, 3, 4, 5, 6]
random.shuffle(l)
print(l)<jupyter_output>[5, 1, 2, 4, 6, 3]<jupyter_text>16. Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).<jupyter_code>import random
import math

l = [random.randint(0, 1000) for _ in range(100)]

m = [i for i in l if 1 < math.sqrt(i) < 30]
print(m[:5] + m[-5:])<jupyter_output>[633, 560, 750, 272, 407, 253, 19, 845, 762, 341]<jupyter_text>17. Write a Python program to generate and print a list except for the first 5 elements, where the values are square of numbers between 1 and 30 (both included).<jupyter_code>import random

l = [random.randint(0, 1000) for _ in range(100)]

m = [i for i in l if 1 < i < 30*30]
print(m[5:])<jupyter_output>[6, 652, 384, 697, 313, 330, 345, 612, 78, 468, 180, 768, 866, 591, 451, 804, 201, 379, 197, 253, 86, 621, 772, 215, 759, 534, 814, 159, 379, 568, 174, 164, 846, 143, 214, 38, 242, 883, 242, 685, 831, 800, 232, 644, 93, 210, 192, 670, 520, 379, 469, 698, 246, 504, 159, 272, 311, 610, 557, 818, 88, 92, 59, 212, 636, 832, 771, 342, 359, 459, 351, 566, 562, 119, 867, 377, 356, 594, 177, 160, 510, 604, 405]<jupyter_text>18. Write a Python program to generate all permutations of a list in Python.<jupyter_code>import random
import itertools

n = 3
l = [random.randint(0,n) for i in range(n)]

[i for i in itertools.permutations(l)]<jupyter_output><empty_output><jupyter_text>19. Write a Python program to get the difference between the two lists.<jupyter_code>l = [1, 2, 3]
m = [5, 8, 3, 2, 1]

print(list(set(m)-set(l)))<jupyter_output>[8, 5]<jupyter_text>20. Write a Python program access the index of a list.<jupyter_code>l = [1, 2, 3, 4, 5]

for k, v in enumerate(l):
    print(k, v)<jupyter_output>0 1
1 2
2 3
3 4
4 5<jupyter_text>21. Write a Python program to convert a list of characters into a string.<jupyter_code>l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

''.join(l)<jupyter_output><empty_output><jupyter_text>22. Write a Python program to find the index of an item in a specified list.<jupyter_code>l = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

print(l.index('a'))
print(l.index('e'))<jupyter_output>0
4<jupyter_text>23. Write a Python program to flatten a shallow list.<jupyter_code>import itertools

l = [[2,4,3],[1,5,6], [9], [7,9,0]]
m = list(itertools.chain(*l))
print(m)<jupyter_output>[2, 4, 3, 1, 5, 6, 9, 7, 9, 0]<jupyter_text>24. Write a Python program to append a list to the second list.<jupyter_code>l = [1, 2, 3, 4, 5]
m = ['a', 'b', 'c', 'd']

print(m + l)
m.extend(l)
print(m)<jupyter_output>['a', 'b', 'c', 'd', 1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5]<jupyter_text>25. Write a Python program to select an item randomly from a list.<jupyter_code>import random

l = [1, 2, 3, 4, 5, 6]

l[random.randint(0, len(l))]<jupyter_output><empty_output><jupyter_text>26. Write a python program to check whether two lists are circularly identical.<jupyter_code>list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list3 = [1, 10, 10, 0, 0]

def rotate(l, n=1):
    t = l.pop(0)
    l.append(t)

def linearly_circular(l1, l2):    
    for i in range(len(l1)):    
        rotate(l1)
        if l1 == l2:
            return True
    return False


linearly_circular(list2, list3)
list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list3 = [1, 10, 10, 0, 0]

def linearly_circular(l1, l2):
    return ''.join(map(str, l1)) in ''.join(map(str, l2*2))

linearly_circular(list1, list2)<jupyter_output><empty_output><jupyter_text>27. Write a Python program to find the second smallest number in a list.<jupyter_code>import random

l = [random.randint(0, 100) for _ in range(100)]

l.sort()
print(l[1])<jupyter_output>2<jupyter_text>28. Write a Python program to find the second largest number in a list.<jupyter_code>import random

l = [random.randint(0, 100) for _ in range(100)]

l.sort()
print(l[-2])<jupyter_output>96<jupyter_text>29. Write a Python program to get unique values from a list.<jupyter_code>import random

l = [random.randint(0, 100) for _ in range(100)]

print(set(l))<jupyter_output>{0, 2, 3, 5, 6, 7, 8, 9, 11, 12, 14, 16, 20, 21, 22, 24, 25, 26, 28, 29, 30, 31, 32, 34, 37, 38, 39, 41, 42, 43, 44, 46, 47, 48, 54, 55, 56, 57, 58, 60, 62, 64, 65, 66, 67, 68, 69, 72, 75, 76, 79, 80, 81, 85, 87, 88, 89, 90, 91, 94, 97, 99}<jupyter_text>30. Write a Python program to get the frequency of the elements in a list.<jupyter_code>import random
import collections

l = [random.randint(0, 100) for _ in range(100)]

collections.Counter(l)<jupyter_output><empty_output><jupyter_text>31. Write a Python program to count the number of elements in a list within a specified range.<jupyter_code>import random
import collections

l = [random.randint(0, 100) for _ in range(100)]

def count_elements_in_range(l, min, max):
    return collections.Counter([i for i in l if min < i < max])

count_elements_in_range(l, 40, 60)<jupyter_output><empty_output><jupyter_text>32. Write a Python program to check whether a list contains a sublist.<jupyter_code>l = [1, 2, 3, 4, 5]
s = [1, 2, 3]

print(set(s).issubset(set(l)) is not None)

print(set(s) <= set(l))<jupyter_output>True
True
{'fraction_non_alphanumeric': 0.1349212275069066, 'fraction_numerical': 0.06734861494810722, 'mean_word_length': 4.329884600079586, 'pattern_counts': {'":': 0, '<': 129, '<?xml version=': 0, '>': 124, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '34', 'dir': 'jupyter-structured-clean-dedup', 'id': '61829', 'license': 'MIT', 'n_tokens_mistral': 5575, 'n_tokens_neox': 4776, 'n_words': 1836, 'path': 'Beginning Python - Sequences, Mapping, Files, and Functions.ipynb', 'repo_name': 'swrpug/python-course', 'size': '13393'}