Thursday, 27 March 2025
Work Diary - 2025
Tuesday, 8 March 2022
Python#07
Class in Python
A Class in Python is a logical grouping of data and functions. It gives the freedom to create data structures that contains arbitrary content and hence easily accessible.
Class” is a logical grouping of functions and data. Python class provides all the standard features of Object Oriented Programming.
Class inheritance mechanism : A derived class that override any method of its base class. A method can call the method of a base class with the same name. Python Classes are defined by keyword “class” itself. Inside classes, you can define functions or methods that are part of the class
Everything in a class is indented, just like the code in the function, loop, if statement, etc.
The self argument in Python refers to the object itself. Self is the name preferred by convention by Pythons to indicate the first parameter of instance methods in Python
Python runtime will pass “self” value automatically when you call an instance method on in instance, whether you provide it deliberately or not
In Python, a class can inherit attributes and behavior methods from another class called subclass
class ametClass():
def aMethod(self):
print("Men Money Machine & Matters")
def bMethod(self, param):
print("Hey I am testing class method :" + param)
def main():
c = ametClass()
c.aMethod()
c.bMethod('I am param porul')
main()
Result:
Men Money Machine & Matters
Hey I am testing class method :I am param porul
####inheritance
class aClass():
def aMethod(self):
print('A ha : Base class method 1')
def bMethod(self, str):
print('oh oh Base class method 2 '+str)
class childClass(aClass):
def cMethod(self):
aClass.aMethod(self) # Note instance of base class method
print('A ha : Child class Method1.....')
def dMethod(self, str):
print('Oh Oh Child class method 2'+str)
def main():
c1 = aClass()
c1.aMethod() # Note child inherited from aClass
c1.bMethod('Base class ..nna') # Note child inherited from aClass
c2 = childClass()
c2.aMethod()
c2.bMethod('Besh besh Child Class Method1')
c2.cMethod()
c2.dMethod('Nanna Irukku Child Class Method 2')
main()
Result
A ha : Base class method 1
oh oh Base class method 2 Base class ..nna
A ha : Base class method 1
oh oh Base class method 2 Besh besh Child Class Method1
A ha : Base class method 1
A ha : Child class Method1.....
Oh Oh Child class method 2Nanna Irukku Child Class Method 2
# Constructors : Defining and initalizing values
class Cadet:
name = " " # Class Variables
def __init__(self, name):
self.name = name
def printWelcome(self):
print('Welcome to Kalvi Koil, AMET '+ self.name)
Cadet1 = Cadet('Anbu')
Cadet1.printWelcome()
Result :
Welcome to Kalvi Koil, AMET Anbu
The above sample codes explain the how to define a class, define class method, inherit a class, overloading a method, initialize the class using constructor.
Happy Learning by simple super code!
Python#06
Python Yield
# Yield is just like return but with generator(?) objects instead of values
# Generators are special functions that have to be iterated to get the values
# To get the values the
# To get the values the objects has to be iterated
#Syntax yield expression
# #Sample yield function
# def demoyield():
# yield "Welcom to AMET - Pioneer in Maritime Education"
#
# out = demoyield()
# print(out)
# #<generator object demoyield at 0x000002802AC5DA50>
# # iterate to get the value in the above object
# #Generators are functions that return an iterable generator object. The values from the generator object are fetched one at a time instead of the full list together and hence to get the actual values you can use a for-loop, using next() or list() method
# for i in out:
#. print(i)
# Example 2
# def gen():
# yield 'G'
#. yield 'A'
# yield 'N'
# yield 'D'
# yield 'H'
# yield 'I'
# yield ' '
# yield 'M'
# yield 'A'
# yield 'H'
# yield 'A'
# yield 'N'
#
# demo = gen()
#
# for i in demo:
#. print(i)
#
#example 3 difference between normal function and generator function
#Normal
def normalfun():
return 'Hello Zoox Robo Taxi to Universe - normal function'
def genfun():
yield "'Hello Zoox Robo Taxi to Universe - generator function"
print(normalfun())
print(genfun())
# Hello Zoox Robo Taxi to Universe - normal function
#until it prints the whole string normalfun()wont stop
# <generator object genfun at 0x00000298E53AD900>
#but gen iterates so we can use the generator obhject to get the values and also , 'pause' and 'resume'
print(next(genfun()))
#Note next function
print(list(genfun()))
Python#05
File Operations in Python
# Simple Excel file read
import pandas as pd
csv_file = pd.read_csv('csv.csv')
print(csv_file)
# # printed the following
# h1 h2 h3 h4
# 0 1 2 3 4
# 1 5 6 7 8
# 2 9 10 11 12
# 3 13 14 15 16
# ##################
#Excel file sheet1 reading specific
excel_file = pd.read_excel('sample.xlsx', sheet_name='Sheet1')
print(excel_file)
# Print out
# Unnamed: 0 slno name age sal
# 0 1 1 1 1 1
# 1 2 2 2 2 2
# 2 3 3 3 3 3
# 3 4 4 4 4 4
# read multiple sheets
df_sheet_multi = pd.read_excel('sample.xlsx', sheet_name=['Sheet1', 'Sheet2'])
print(df_sheet_multi)
#Printout
# {'Sheet1': Unnamed: 0 slno name age sal
# 0 1 1 1 1 1
# 1 2 2 2 2 2
# 2 3 3 3 3 3
# 3 4 4 4 4 4, 'Sheet2': Unnamed: 0 slno name age sal
# 0 1 11 11 11 11
# 1 2 22 33 44 55
# 2 3 66 66 66 66
# 3 4 77 77 77 77}
print(25*'-','Sheet1')
print(df_sheet_multi['Sheet1'])
# ------------------------- Sheet1
# Unnamed: 0 slno name age sal
# 0 1 1 1 1 1
# 1 2 2 2 2 2
# 2 3 3 3 3 3
# 3 4 4 4 4 4
print(25*'-','Sheet2')
print(df_sheet_multi['Sheet2'])
# ------------------------- Sheet2
# Unnamed: 0 slno name age sal
# 0 1 11 11 11 11
# 1 2 22 33 44 55
# 2 3 66 66 66 66
# 3 4 77 77 77 77
# To Create and write contents
f = open("demo.txt", "w")
f.write("I have written the contents!")
f.close()
#open and read the file after the appending:
f = open("demo.txt", "r")
print(f.read())
#prints => I have written the contents!
f.close()
f = open("demo.txt", "a")
f.write("Now I have written the contents more again !")
f.close()
#In demo.txt => I have written the contents!Now I have written the contents more again !
import os.path
file_exists = os.path.exists('readme.txt')
print(file_exists)
#False
import os.path
file_exists = os.path.exists('demo.txt')
print(file_exists)
#True
f = open("demo1.txt", "w")
f.write("I have written the contents!")
f.close()
f = open("demo2.txt", "w")
f.write(str(list(csv_file)))
f = open("demo2.txt","r")
print(f.read())
#f.close()
print(f.read())
#Print ['h1', 'h2', 'h3', 'h4']
Thursday, 24 February 2022
List & Lambda
Let us Review Python a bit and look at List & Lambda Function!
Python is the easiest popular programming language. Anything and everything you can do with python! Everyone knows about Google. Google uses Python extensively.
We learn some fundamentals by examples.
Ex.1 Print
Code | result |
print("Hello, Friends!") | Hello, Friends! |
Types and functions
Python automatically recognizes the types of data. Here are a few examples:
Character Strings: ‘ODL’, ‘AMET’
List of Objects: [10, 20, 30, 40], [‘Male’, ‘Female’]
In addition, Python has special datum meaning nothing: None
Python is rich in libraries, built-in-function. Now let us learn about variables
Ex.2 Add two constants
Code | Result |
print(5+5) | 10 |
Ex. 3 Add variables
Code | Result |
X,Y = 5,5 | |
print(X+Y) | 10 |
X,Y = 5,’FIVE’ print(X+Y) | SyntaxError: invalid character '’' (U+2019) |
Note multiple assignments in a single statement
Ex. 4 Add string variables
Code | Result |
X,Y = ‘5’,’5’ | |
print(X+Y) | 55 |
X,Y = ‘AMET’,’UNIVERSITY’ |
|
print(X+Y) | AMET University |
Let us print some useful Multiplication Table
Ex 5. For loop with range -
Multiplication Table for 5: Here we use for loop with simple syntax as shown:
Code | Result |
for x in range(1,11): print(x*5) | 5 |
for x in range(1,11,2): print(x*5) #Note range function (start,stop,step) | 1 |
for x in range(1,10): txt = '{} * {} = {}' print(txt.format(x,5,x*5)) #note simple formatting | 1 * 5 = 05 |
For repetitive steps, we used for loop. See the syntax range. Note range (start, stop, step)
The start is inclusive and the stop is excluded with the increment step.
Ex 6. For Loop iteration
code | result |
for x in "AMET": print(x) | A |
for x in ['marine’, ‘nautical science']: print(x) | marine |
Let us check how PVM (Python Virtual Machine) handles internally through some simulator Click This Link Python Tutor - Visualize Python and paste the above code in the space given and see the steps forward. |
|
Ex 7. Introspection to Python variable types
#Ex 7 basic and collection variables
var_i = 5
var_f = 5.0
var_str = "AMET"
print(type(var_i))
print(type(var_f))
print(type(var_str))
print(25*'-','Collection Variables')
# Numeric collection variables
var_list = [1,2,3]
var_tuple = ("one", "two", "three")
var_set = {1,2,3}
var_dict = {"1":"one", "2": "Two", "3":"Three"}
print(type(var_list))
print(type(var_tuple))
print(type(var_set))
print(type(var_dict))
#
<class 'int'>
<class 'float'>
<class 'str'>
------------------------- Collection Variables
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
Happy Learning Lambda🎨😊
Work Diary - 2025
Learnt: Date Link 28.01.2025 https://ametodl.blogspot.com/p/experience-with-deepseek-...
-
🚀 Setting Up Angular Routing - The Fun Way! 😆 Hey there, future Angular ninjas! 🥷 If you’re new to Angular and struggling with routing...
-
ET o Prediction using ChatGPT and Python Prompt: Help me to do research in the topic " Estimation of Reference Evapotranspiratio...
-
Why is it called Angular? Because it's written between < >! Get it? (Okay, bad joke, but now you won’t forget it!) Angular is a p...