You are on page 1of 1
Jai Chaudhry 2K18/SE/@69 ML-LAB-C2-G2 Experiment -1 Exploring and demonstrating Python. Basics of Python In [4] In [5]: In [6]: In [7] In [8] an [13]: In [11]: In [12]: an [15]: In [29] In [37] in [49] In [44] In [2] In [18]: In [48] In [54]: an [57]: In [58] out [52]: In [59]: out[59]: In [61] In [71]: In [74] In [75] print("Hello World”) = 10 10.5 hello_python™ 25 +39 print(a) print(b) print(c) print (d) Hello World 10 10.5 hello_python (5439) print(type(a)) print(type(b)) print(type(c)) print(type(d)) # Working with Input name = input("enter your” print("Hello”,end=" ') print (name) Enter yourloker Hello Joker # Typecasting a = float(input("Enter a number")) print(a) print(a*a,end=" print("The square of the nunber is =" + str(a*a)) Enter a number23.23 23.23 539.632990000001The square of the number is =539.6329000000001 # multiple variable assignment a,b,c = 10,20,30.24 # multi Line string mystring = """This is a miltiline string **" print (mystring) This is a multiline string # If ElseIf Statement weather= ool" if weath sainy": print("Don't go outside”) print("rt is raining heavily") elif weather=="Coal" print("Lets play cricket") else: print("Lets go for shopping”) while ic=n: print("Step %d"%i) itis. print("Loop Finished") step step step step step step step step step 9 step 19 Loop Finished # Function : def helloFact(): print("Hello Factorial") helloFact() def factorial(n): ans = 1 for i in range(1,n+1): ans *= i return ans print (factorial(s)) Hello Factorial 120 Data Structures + Tuples + Dictionaries + Sets # TUPLES t = (1,2,3,"Hello") print(t) # tuples are immutable # t[0] = 5 # ERROR print(t[o]) print() # convert a tuple into a List 1 = list(t) print(1) le] = 5 print(1) print() # New Tuple 2 = tuple(1) print(t2) print() aconcatentation print(t2 + t) print() #Repetion t2 = t283 print(t2) print() # Inbuilt Function t = (1,44,3,2) print (min(t)) # delete del t (1, 2) 3, "Hello") [1, 2, 3, ‘Hello'] (5) 2, 3 ‘Hello'} (5, 2, 3, "Hello') (5) 2) 3, ‘Hello’, 1, 2, 3, ‘Hello") (5) 2, 3, ‘Hello’, 5, 2, 3, ‘Hello’, 5, 2, 3, ‘Hello’) 4# Dictionaries d = {"Mango":100, “apple print(d) #100 Up print (d{” lango"],end="\n\n") d[ "Guava" print(d) d[ "Grape" print (d{” 10,20] rape" ],end="\n\n") d["Pineapple"] = {"small":90,"Large" 150} print(d["Pineapple"]["smal1"]) © PRINT KEYS print (d.keys() ,end="\n\n" 4 PRINT VALUES print(d.values(),end="\n\n") # DELETION del d["Pincapple"] print (d,end="\n\n") # 10 List 1 = List(d.items()) print(1,end="\n\n") # PRINT LENGTH print(1en(d)) # Combination of 2 Lists to Dict 11 = ["apple”, "Papaya" , "Guava" ,"Banana”] 12 = [100,120, 30,50] p = dict(zip(1a,12)) print(p) {'Mango 100 100, ‘Apple’: 80} {'Mango": [10, 20) 100, ‘Apple’: 80, ‘Guava’: 60} 90 dict_keys(['Mango", ‘Apple’, ‘Guava’, ‘Grape’, ‘Pineapple’ ]) dict_values([100, 80, 60, [10, 20], {‘small": 90, ‘Large’: 150)]) {'Mango": 100, "Apple": 80, "Guava": 60, ‘Grape": [10, 20]} [('mango", 100), (‘Apple’, 80), (‘cuava’, 60), (“6rape", [10, 201)] 4 {‘apple 100, "Papaya’: 128, ‘Guava’: 30, "Banana": 50) # Complicated Dictionary: “name":"A2", age" :40 } ] $ print (book,end="\n\n") print (book{"name" },end="\n\n") print (type(book)) {{name': ‘learning Python’, ‘page’: 328, ‘authors’: [{‘name': "Al", ‘age’: 30}, (iname': "a2", ‘age’? 40}]) learning Python # SETS # Basic et() s.add(1) s.add(1) print(s) # From List 5 = set([15,2,2,3,4,3,1,1]) print(s) 4 Lookup if 14 in 5: print ("present") else: print(*not present") print() # clear s.clear() print(s) a (4, 2,3, 4 a5} not present set() File Handling 4# Opening and Writing file = open("sample file.txt™,'w') content = “hello World\n" 1 = [content]*3 file.writelines(1) File.close() # Reading data=open(“sample_file.txt”,'r') print (data.read()) iro with open("sample_file.txt","r") as f: best print(f.closed) print (i,f.read()) hello World hello World hello world False 1 hello world hello World hello World CSV Library for CSV Files # writing CSV FILE File=open(“data.csv","w" encod: headers="number name, id ,text\n file.write(headers) utf-8") sesstr(i)#"\n" file.write(s) file.close() # Reading CSV FILe import csv Filezopen(“data.csv","r" ,encodin objzcsv.reader(File) for data in obj: utf-8") print (data) [‘number', ‘name’, ‘id’, ‘text'] [re", 'e", to, 10°] pay ay ay ay [ray ity 2) 2] (130) 138) 138, 3") [ay cay cay a] Pandas Library for CSV Files # Get pata Directly from Net using Link import pandas as pd data=pd.read_csv(‘http://archive. ics .uci.edu/ml/machine-learning-databases/balan data.head() B11 12 4a ORT 1 4 2 1R1 4 4 3 2Ri 444 BR1 1 4 5 data.tail(3) B11 12 13 els § § 3 s2 ls 5 5 4 eae 5 5 5 8 # PRINT ALL VALUES print(data.values[:]) (['r' 1222) pat aa1a] UR aa2a] ce UB # Print in Range print (data.shape,en 624 ROWS and each row has 5 entries \n\n*) print(data.values[1:,0:5]) (624, 5) <-- 624 ROWS and each row has 5 entries (ir 1423) [intial [R1iasy 553] 554] 55]) OOPS class Car pass = car() print(type(c)) 4# Bigger class class Person: #class variable, common for all objects of the same class nationality = "Indian" def _ init__(self,pname,clg): Self.name = pname self.college = clg def sayiti(self,name): print("Hello "+name) def _secretMethod(self): print("In Secret Method of ", self.name) def introduce(self): print(’My Name is “,self.name) print("r am from ",self.college) print("r am ",self nationality) self.__secretmethod() p.introduce() #p._secretMethod() p2 = Person("Jai’ p2.introduce() Lamborghini") Hello LOL My Name is Jai I am from Lamborghini Veneno Tam Indian In Secret Method of Jai My Name is Jai T am from Lamborghini Tam Indian In Secret Method of Jai Jai Chaudhry 2K18/SE/@69 ML-LAB-C2-G2

You might also like