AndroidCodingWorld is all about the fundamentals of Programming Language, Software Design, Problem Solving Skills(Big-O Complexity), Core-Java, Kotlin, Ktor ,Data-Structures, Algorithms, Design-Patterns, Android Application Development, Microservices, Python Development(Back-End,FLASK), Spring Boot, Microservices, REST API's
Friday, April 5, 2019
Using Underscore in Numeric Literals Java
Tuesday, December 26, 2017
Object Oriented Approach in Python
So Classes and Why do we need them
In Java it require that you write code in inside class but in Python it does not required.you can write a code inside a class but you don't have to.In Python we define a class using class keyword similar to define a function using def keyword.
Let's take an complete object-oriented example:
classess.py
students =[]
class Student:
school_name = "Abhinaw Demo"
def __init__(self,name,student_id=1):
self.name=name
self.student_id=student_id
students.append(self)
def __str__(self):
return "Student "+ self.name
def get_name(self):
return self.name.capitalize()
def get_school_name(self):
return self.school_name
class NewSchoolStudent(Student):
school_name="Inheritance Abhinaw Demo"
def get_school_name(self):
return "This is inheritance"
def get_name(self):
original_value= supper().get_name()
return original_value + "New School"
abhinaw= NewSchoolStudent("Abhinaw")
print(abhinaw.get_school_name())
print(abhinaw.get_name())
This demo code includes classes,adding methods to our classes,Constructor,Instance and Class Attributes and Inheritance and Polymorphism.
Lambda Functions in Python
So Lambda Functions are just an anonymous function.they are very simple just saves your space and time.they are useful in case of higher order function.Lets take an example:
def double(a):
return a*2
Convert to Lambda Function
double = lambda a: a*2
Friday, December 22, 2017
Generator Functions - Yield Python
So Yield is a keyword.first let's take an example.
student = []
def read_file():
try:
f=open("students.txt","r")
for student in read_students(f):
students.append(student)
f.close()
except Exception:
print("Could not read file")
def read_students(f):
for line in f:
yield line
read_file()
print(students)
So what I have done is created a single function read_students and what it does
it iterated over the file and yield the single line from that file.
Opening,Reading and Writing Files Python
Let's save our data in a file and read from that file again when we re-open our app.
So let's take an example:
Let's take an example:
students =[]
def get_students_titlecase():
students_titlecase = []
for student in students:
students_titlecase .append( student["name"].title())
def print_students_titlecase():
students_titlecase = get_students_titlecase()
print(students_titlecase)
def add_student(name,student_id=1):
student ={"name:name", "student_id": student_id}
students.append(student)
def save_file(student):
try:
f=open("students.txt","a")
f.write(student + "\n")
f.close()
except Exception:
print("Could not save")
def read_file():
try:
f=open("students.txt","r")
for student in f.readlines():
add_student(student)
f.close()
except Exception:
print("Could not read file")
read_file()
print_students_titlecase()
student_name=input("Enter student name: ")
student_id= input("Enter student ID: ")
add_student(student_name,student_id)
save_file(student_name)
where a= append some text
r = reading text file
So when you run the program.the output will be something like this
Output:
Could not read file
[]
Enter student name:
Nested Functions and Closures in Python
Let's take an example:
def get_students():
students=["Abhinaw","Aman"]
def get_students_titlecase():
students_titlecase = []
for student in students:
students_titlecase.append(student.title())
return student_titlecase
students_titlecase_names=get_students_titlecase()
print(students_titlecase_names)
This is called Nested function and basically the inner function can access the outer students So this is called closures.
Create a simple python console app
Let's create an app where user will provide the student name and Student I'd from the console.
eg: student.py
students =[]
def get_students_titlecase():
students_titlecase = []
for student in students:
students_titlecase = student["name"].title()
def print_students_titlecase():
students_titlecase = get_students_titlecase()
print(students_titlecase)
def add_student(name,student_id=1):
student ={"name:name", "student_id": student_id}
students.append(student)
student_list = get_students_titlecase()
student_name=input("Enter student name: ")
student_id= input("Enter student ID: ")
add_student(student_name,student_id)
print_students_titlecase()
So in console you have to something like this and output will be
Enter student name: Abhinaw
Enter student ID:3455
Abhinaw
this is very simple Python app taking console input and also we can modify the program.
Thursday, December 21, 2017
Functions Arguments in Python
Functions Arguments
So argument is parameters where you can provide the values to functions and also useful in case you don't have to make variable global.So let's modify our existing code for input argument.
functions.py :
students = []
def get_students_titlecase():
student_titlecase = []
for student in students:
students_titlecase = student.title()
return students_titlecase
def print_students_titlecase():
students_titlecase = get_students_titlecase()
print(students_titlecase)
def add_student(name) :
students.append(name)
student_list = get_students_titlecase()
add_student("Abhinaw")
So now modify our add_student method:
def add_student(name,student_id=332)
students.append(name)
So here you will notice student_id=332 is basically a default or optional student_id if any user does not provide student_id then it will add the value automatically but if any user provides then in that case it will be overridden by the userinput value.
Now let's add student Dictionaries to our add_student method:
def add_student(name , student_id=222)
student = {"name:" name, "student_id", student_id}
students.append(student)
So call this function like this
add_student(name="Abhinaw" , student_id=14)
In Python there can be multiple variable arguments such as inbuild print function.
print("hello", "World", 34, None , "Wow')
you must have question here like how come it possible let me create our own function just to cater above.
def var_args(name, *args)
print(name)
print(args)
just to call the function
var_args("Abhine","Android Developer",None, True)
you can add any number of argument.
There is also a concept called kwargs.
let's have a look.
def var_args(name,**kwargs):
print(name)
print(kwargs["description"],kwrgs["feedback"])
var_args("Abhinaw" ,description="Loves Android", feedback=None,androidcodingworld_subscriber=True)
it is called kwargs argument and defined with **kwrgs.So it is not a list it is Dictionaries thats where it differs from above example.
basically it was an example of variable arguments you can add as much as you can in the form of list or can also make it Dictionaries.