<jupyter_start><jupyter_text>This notebook will contain information about basic Python Data Structures and their uses. 1. Numbers2. Strings3. Lists4. Dictionaries5. Tuples6. Files7. Sets8. Booleans  1. Numbers:<jupyter_code>a = 10
print(a)<jupyter_output>10<jupyter_text>General Arithmetic Operations: Addition, Subtraction, Multiplication, Division, Remainder(Modulo), Exponent<jupyter_code>a + a
a - a
2 * a - a
a / 3
a % 3
a ** a
a
a ** 2<jupyter_output><empty_output><jupyter_text>2. Strings: Here, I am very performing basic String operations and using predefined string functions<jupyter_code>"Hello World"
"HI"
"<NAME>"<jupyter_output><empty_output><jupyter_text>As we can see above only "<NAME>" is printed instead of both the strings, reason Python is using interpreter which is working line by line<jupyter_code>print("Hi")
print("<NAME>")
print("The length of string s is\t")
print(len("<NAME>"))
myString = "Hi this is pilot in Python!"
for i in myString:
    print(i,end="") #end is used in python to print within same line, but we can append how we need to end the string
print()
for i in myString:
    print(i,end=" ")<jupyter_output>Hi this is pilot in Python
H i   t h i s   i s   p i l o t   i n   P y t h o n<jupyter_text>Slicing & Indexing<jupyter_code>print(myString[0]) #prints the 1st character of the input String
print(myString[-1]) #prints the last character of the input string
str="abcdefghijk"<jupyter_output><empty_output><jupyter_text>Slicing : [start:stop(upto but not include this index): step]<jupyter_code>str[:] # start - stop is not there, hence print the complete string
str[::2] #Skipping the next character from 0-index onwards
str[:-2:2]
str[:len(str):2]
str[0:len(str)-11] # here start_index & end_index is same
str[0:-1] #start_ndex=0 : end_index=-1, which means upto end of the string<jupyter_output><empty_output><jupyter_text>Reversing the String<jupyter_code>str[::-1]
#Working with steps of different lengths
#
#
print(str[::len(str)+1])
print(str[::len(str)])
print(str[::len(str)+10])
print(str[::len(str)+100])
print(str[::len(str)-1])
print("Working with -ve steps")
print(str[::-len(str)])
print(str[::len(str)-3])
print(str[::len(str)-100])<jupyter_output>a
a
a
a
ak
Working with -ve steps
k
ai
k<jupyter_text>String is immuatable in Python also, like Java. Hence everytime we work on a String new string will be created.<jupyter_code>a="<NAME>"
print(a.upper())
print(a.lower())
print(a.split()) #by default, delimiter is white space and return type is LIST.
print(a.split("a"))<jupyter_output><NAME>
aman rastogi
['Aman', 'Rastogi']
['Am', 'n R', 'stogi']<jupyter_text>String Formatting and Interpolation1. Using .format()2. f-string<jupyter_code>print("Alan {} - <NAME> {} -  Steve {} ".format("Turing","Wozniack","Neumann"))
#same string (a) has been interpolated.
print("Alan {a} - <NAME> {a} -  Steve {a} ".format(a="Turing",b="Wozniack",c="Neumann"))
print("Alan {a} - <NAME> {c} -  Steve {b} ".format(a="Turing",b="Wozniack",c="Neumann"))
print("Alan {1} - <NAME> {1} -  Steve {1} ".format("Turing","Wozniack","Neumann"))
print("Alan {0} - <NAME> {2} -  Steve {1} ".format("Turing","Wozniack","Neumann"))
res=100/3
print(res)
print("The value of a is {a:1.5f}".format(a=res))<jupyter_output>The value of a is 33.33333<jupyter_text>f-String<jupyter_code>a= 100
res=100/77
print(f"the result is {res}")
print(f"{a}/77 is {res}")
a="Aman"
b="Rastogi"
print(f"{a} {b}")<jupyter_output><NAME><jupyter_text>3. Lists<jupyter_code>a= ['1',aman,4.0]
a= ['1','aman',4.0,98]
print(a)
print(a.sort())
b=[12,11,10,9]
print(b)
print(b.sort())
print(b)
print(b.reverse())
print(b)
print(b.pop())
print(b)
set("aman")<jupyter_output><empty_output><jupyter_text>4. Dictionaries Dictionaries store value in unordered sequence.They store value as key-value pair.Preferred over lists when indexing is not mandatory or in the code flow index will not be used. Syntax: dict = {"Key1":"Val1","Key2":"Val2"}<jupyter_code>dict1 = {'apple':2.99,'oranges':3.5,'pineapple':4.50}
type(dict1)
#To Retrieve all keys
dict1.keys()
#To retrieve all values
dict1.values()
#To retrieve all key-value pairs
dict1.items()
dict1['apple']
dict1['figs']=4.5
dict1
#Remove give apple and its value. Returns apple value
dict1.pop('apple')
dict1
#Returns figs value but do not delete figs key
dict1.get('figs')
dict1
#Dictionary can contain other data structures also.
#Here dict 2 contains, None, List, dicitinary within dictionary
dict2 = {"a":None,"b":[1,2,3,5],"c":{'oranges': 3.5, 'pineapple': 4.5, 'figs': 4.5, 'str':['a','b','a','c','e']}}
type(dict2)
#Retrieving Lists from Dict2
print(dict2['b'])
print(dict2['c']['str'])
#Retrieving from dict using get() method
print(dict2.get('b'))
print(dict2.get('c'))
print(dict2.get('c').get('str')) 
print(dict2.get('c').get('str')[3].upper())<jupyter_output>[1, 2, 3, 5]
{'oranges': 3.5, 'pineapple': 4.5, 'figs': 4.5, 'str': ['a', 'b', 'a', 'c', 'e']}
['a', 'b', 'a', 'c', 'e']
C<jupyter_text>Also Explore ordereddict for ordered dictionary.  5. Tuples Tuples are similar to Lists, but tuples are immutable. Hence, state of tuple cannot be changed, once value is assigned to it. Slicing and Indexing works in Tuples.<jupyter_code>tup = (1,2,3)
type(tup)
print(tup)
tup[0]
print(tup.count(1))
print(tup.index(3))
#values cannot be modified in tuple
tup[1]=5
tup[3]=5
#Adding values in tuple
tup_list_convert = list(tup)
#print(type(tup_list_convert))
tup_list_convert.append(5)
tup_list_convert.append(50)
tup_list_convert.append(500)
tup = tuple(tup_list_convert)
print(tup)<jupyter_output>(1, 2, 3, 5, 50, 500, 5, 50, 500)<jupyter_text>6. Sets Unordered collection of Unique objeccts. Hence, only one object can be added in set.<jupyter_code>Set1 = set()
type(Set1)
print(Set1)
Set1.add(1)
Set1.add(2)
Set1.add(3)
print(Set1)
#Values will be overwritten,2 will be overwritten
Set1.add(2)
print(Set1)
list_used_for_set = [1,2,2,2,3,43,3]
print(list_used_for_set)
print(list_used_for_set)
s=set(list_used_for_set)
print("Set is :\t",s)<jupyter_output>Set is :	 {1, 2, 3, 43}
{'fraction_non_alphanumeric': 0.16994870097633627, 'fraction_numerical': 0.0347509515141486, 'mean_word_length': 6.3707317073170735, 'pattern_counts': {'":': 5, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '314128', 'license': 'MIT', 'n_tokens_mistral': 2460, 'n_tokens_neox': 2357, 'n_words': 636, 'path': 'Python Objects and Data Structures/Python Objects and Data Structures.ipynb', 'repo_name': '101414juit/Python-101', 'size': '6090'}