# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Python Collections
#
# * Lists
# * Tuples
# * Dictionaries
# * Sets

# ## Lists

list_data = [10,10.5,"python is cool",True,5==10]
print(list_data)

# ### Functions Of List

dir(list_data)

list1 = [1,2,3]
list2 = [10.5,"python is cool",True,False,5==5]
list3 = [4,5,6,7,8,9]

list1.append(50) # Add value 
list1

len(list1) # Length of list

list1.count(1) # Counting the value 

max(list1) # Will return maximam number

min(list1) # Will return minimam number

sum(list1) # Will return sum of numbers

list3 = list1+list2 # Concat Two List
list3

sum(list1), max(list1), min(list1)

list_sum = [] # Suming Two lists by for loop
if len(list1) == len(list3):
    for i in range(len(list1)):
        list_sum.append(list1[i]+list3[i])
list_sum

list(zip(list1,list3)) # zip function for concat two lists

list_sum = [a + b for a,b in zip(list1,list3)] 
'''
one line to concating two lists.
a + b | the operator and the two values in list after zip.
zip(list1,list3) | zip function for concat two lists.
'''
list_sum

list_sum.extend(list3[len(list_sum):]) # Add the rest of list to total list.
list_sum

# ## Tuple
# Tuple is immutuble list

tuple1 = (1,2,3,3,3,3)
tuple1

dir(tuple)

tuple1.count(3) # sum of indexes was repeated

tuple1.index(1) # index value 

# ### Comparison in tuples

(1,2) > (0,1) # will chexk first index only => return true

(1,2) > (2,3) # will chexk first index only => return false

(1,2) > (1,3) # will chexk second index when the first indexes is equals => return false

# ## Dictionaries

# dict = {k:v, k:v, ...}
dict1 = {"id":120123232311,"name":"ahmed","dept":"IT"}
dict1

dir(dict1)

print("id: "+str(dict1["id"])) # call by key
print("name: "+dict1["name"]) # call by key
print("department: "+dict1["dept"]) # call by key

dict1["age"] = 25 # will add a new key with value
dict1

dict1["age"] = 20 # will update the value
dict1

dict1.get("gpa") # return 'None'
if dict1.get("gpa"):
    print("yes")

dict1.get("gpa",85.2) # return the defult value and it is = 85.2 

dict1.items()  # will return the all items K & V

dict1.values()  # will return the values only

dict1

age = dict1.pop("age") # will delete age from dict and store it in variable
dict1

dict1.popitem() # will delete last item.

dict1

# ## Set

set1 = {1,2,3,3} # it will return the values without repetition
set1

1 in set1

for i in set1:
    print(i)

# ## Words counting in text 

text = '''
When executing one cell at a time, mind code dependencies.
For example, the second cell in the code fragment uses 
the variables defined in the first cell. So, if you modify 
the N value, the changes will be reflected in the scatter 
chart only after the first cell gets executed. 
You can use smart shortcuts to execute the 
current cell and select the below one: 
'''

len(text) # will return number of characters

len(text.split()) # will return number of words 'split()' is function

# to get number of repeatition words.
words = text.split() 
count_words = dict()
for word in words:
    if word in count_words:
        count_words[word] += 1
    else:
        count_words[word] = 1
print(count_words)

# to get number of repeatition words with less code.
words = text.split() 
count_words = dict()
for word in words:
    count_words[word] = count_words.get(word, 0) + 1
print(count_words)

# to get word is more repeatition.
more = 0
word = ""
for k,v in count_words.items():
    if v > more:
        more = v
        word = k
word +" : "+str(more)

# to get word is more repeatition with less code.
count_word = [(v, k) for k,v in count_words.items()]
sorted(count_word,reverse=True)

text.index("the")


# Function for getting all indecses of word 
def get_all_indeces(text,word):
    counter = []
    words = text.split()
    for i in range(len(words)):
        if words[i] == word:
            counter.append(i)
    return counter


get_all_indeces(text,"the")



StarcoderdataJupyterScriptsDedupFiltered