Similar to Strings, Python also supports below said (built-in) data structures:
Lists are similar to arrays and can be iterated via index values, starting from 0.
Its elements are enclosed within sqaure brackets, separated by commas and can be of any data-type.
These elements can be removed, added or changed through a number of functions.
li = [1, 1.5, "simran", True, print(), [1,2], ("a","b")]
for i in li:
print(i, "\t",type(i))
print(li[0:-2:2]) #slicing in Lists : start, stop, jump
li.append("oops!") #adding new element at the end
print(li)
li.pop() #removing last element
print(li)
li[4]="i replaced print" #update element at an index
print(li)
new_li = [int(x) for x in input().split()] #comprehension : to input list elements as integers
for i in new_li:
print(i, "\t",type(i))
A tuple is an immutable list, implying its elements can be iterated but not modified.
Unlike lists, tuples are declared within round brackets.
But like lists, it can store heterogeneous elements.
tu = (1,1.5,"simran",False,len("hey there"),[1,2],("a","b"))
for i in tu:
print(i,"\t",type(i))
It marks the key-value pairs.
Individual elements can be accessed via keys but not through index values.
Thus the values can be modified by specifying the key.
A dictionary is declared within curly brackets with a colon between the key & its associated value. And each key-value pair is separated via commas. Again, the values can be heterogeneous but its keys can not.
d = {
"name" : "simran",
"age" : 22,
"marks" : {
"eng" : 95,
"maths" : 81,
},
"some list" : [1,2,3]
}
print(d.keys())
print(d.values())
print(d.items())
d['name']="simran singh" #assigning new value to a key
print(d)
d['marks']['maths']=91 #altering the value of a nested dict
print(d)
A set stores only the unique elements and the elements can not be accessed by specifying the index values.
The elements are sorted and can be added or removed. And can be of any data-type.
It is usually used for union or intersection problems.
It can not store set/list/tuple as elements like in case of lists or tuples.
s = {1,2.5,"simran",print(),3,4,5}
print(s)
s.pop() #removes a RANDOM element
print(s)
s.pop()
print(s)
s.pop()
print(s)
s1 = {1,2,3,4,5}
s2 = {2,3,4}
print(s1.union(s2))
print(s1.intersection(s2))
print(s2.issubset(s1))
print(s1.issuperset(s2))
#to calculate character frequency in a given string
my_string = "hey there, wassup?"
freq = dict()
#creating a dictionary to keep count of each character in string
for char in my_string:
if char in freq:
freq[char] = freq[char] + 1
#we can modify the values by specifying the key --> dict[key] = new value
else:
freq[char] = 1
print(freq)
#finding pairs of elements in a given list that add up to 10
some_list = [1,8,9,3,7,1]
target = 10
visited= set()
#to keep check of elements visited already
result = list()
#the list that would return combo pairs
for first_num in some_list:
required_num = 10 - first_num
if required_num in visited :
result.append([required_num, first_num])
#append takes single argument, so pushing each pair as a list
visited.add(first_num)
print(result)
# we can typecast a dictionary into a list but not otherwise
list_new = list({
"name" : "simran",
"age" : 22
})
print(list_new)
# to typecast into a dictionary, we need to zip the individual lists
car = ["swift","polo","innova"]
color = ["silver","blue","white"]
price = [5,7,12]
a_zipped_list = list(zip(car,color,price))
print(a_zipped_list)
# for dictionaries we need to pass only 2 parameters in the zip function i.e keys and the values
vehicle = dict(zip(car,color))
print(vehicle)
With an objective to reduce repetition, function is piece of code that can be called (utilised) at multiple points within in the same program.
Syntax for creating user defined functions:
def my_func():
pass
Just like in any other programming language, we may pass a variety of parameters but the general sequence is -
Default parameters must be declared after args.
def my_func(var_a, *args ,var_b = "100"):
pass
Parameters vs Arguments - While defining a function we declare parameters and on calling the function we pass arguments.
def my_func(var_a,var_b,var_c):
print(var_a,var_b,var_c,sep="\n")
my_func("I am A", var_c = "I am C", var_b="I am B")
def my_func2(var_d,*args):
print(var_d,)
print(*args)
my_func2(10,20,30.6,"hello")
def last_func(a,b,c,*args,d="I am D"):
print(a,b,c,*args,d,sep = "\n")
last_func("I am A","I am B","I am C","I am *args1","I am *args2",d="I am new D")
Similar to functions, classes are fundamentals of Object Oriented Programming that promote code re-usability.
A class may be defined as a collection of similar objects, say a Car class, encompasess objects like Swift, Polo and Innova. And provides a general template for its objects, for ex - every object posseses certain number of attributes like seats, wheels etc.
class Car():
pass
These objects within a class, hold some state and behaviour. For ex - a car's color and age can be mapped to its state and the behvaiour may include functions like start engine or turn on the AC.
swift=Car()
class Human():
population=0
def __init__(self, name, money=0):
self.name=name
self.money=money
Human.population+=1
def party(self):
if(self.money>300):
self.money-=300
else:
print("broke!")
def borrow(self,friend,amount):
if(friend.money>amount):
friend.money-=amount
self.money+=amount
def __confidential(self): #encapsultion
print("This is confidential.")
def super_power(self):
self.__confidential()
@classmethod #singleton
def thanos(cls):
Human.population//=2
simran=Human("Simran Singh",200)
someone=Human("Mr. Someone",1000)
simran.party()
simran.borrow(someone,650)
simran.party()
simran.money
someone.party()
someone.money
simran.super_power()
p1 = Human("Person 1")
p2 = Human("Person 2")
p3 = Human("Person 3")
p4 = Human("Person 4")
Human.population
Human.thanos()
Human.population
# inheritance
class Child(Human):
# overriding
def party(self):
print("You are too young to party")
kid = Child("Mr. Kid")
kid.party()
Human.population
Before performing read/write operations on a file, we use open() function to return a file object wherein we pass two arguments, the filename and the mode.
The modes available are:
If the mode isn't specified, Python accepts it in the read mode by default.
In certain cases, we need to explicilty declare the type along with the mode : say wt for write-text and wb for write-binary.
By default the type is set to text but in scenarios like pictures or similar files, we need to specify the binary part.
r+ allows both read and write operations but r+ can be used only for existing files.
w+ apart from writing on to the file, allows to create a file if it is not present already.
While write overwrites anything stored within the file, append allows to add content at the end of the file.
Post write/append operation we need to call flush() to push away all the data in the stream on to the file. Also, when call we close() it automatically flushes the stream-data.
f = file.open("file_name.txt","w")
f.write("This will be written to file")
f.close()
A simpler syntax, which automatically closes the file is :
with open("file_name", "mode") as variable:
variable.function()
with open('sample.txt','w+') as f:
f.write("Hey there!")
with open('sample.txt') as f:
print(f.read())
with open('sample.txt',"a") as f:
f.write("\nWassup")
with open('sample.txt') as f:
print(f.read())