What is Walrus Operator in Python

Walrus Operator is the one of the new features in python 3.8 ,it is a new method, using Walrus Operator we can assign values to variable as a part of an expression using the notation NAME := expression for example :

x_year= 2019

if (y_year:= x_year) < 2020:
    print(f"The value of y_year is {y_year} and is happend before 2020.")

here the value of x_year is assigned y_year if the condition satified(< 2020)

What is Walrus Operator in Python

Static methods in Python example

Static methods are almost similar to normal methods inside a class but the only difference is static methods can be called from out side the class without initializing the object of that class.This means a static method can be called without using an object, it can be called using CLASSNAME.STATIC_METHOD_NAME.

Lets see Static methods example

1) Static method example using @staticmethod

class Currency_convert:

    # create Dollar_Ringgit_convert static method
    @staticmethod
    def Dollar_Ringgit_convert(doll):
        return doll * 4.35

print('Dollar to Ringgit Convert :', Currency_convert.Dollar_Ringgit_convert(150))

2) Static method example using staticmethod()

class Currency_convert:

    # create Dollar_Ringgit_convert static method
    def Dollar_Ringgit_convert(doll):
        return doll * 4.35

Currency_convert.Dollar_Ringgit_convert = staticmethod(Currency_convert.Dollar_Ringgit_convert)

print('Dollar to Ringgit Convert :', Currency_convert.Dollar_Ringgit_convert(150))

static methods in python

machine learning project in python for beginners

Here is the simple machine learning project in python for beginners, it is actually some machine learning application to predict 2BHK apartment price in a specified city.So first we should explain what is machine learning, it is actually a technique that enable a system to learn from data.In simple it will learn from the data you have given and tell u next data based on the learning.
To do machine learning project in python you have to download anaconda you can download it from the website www.anaconda.com based on python version you can download any of the release.Then double click and install in your sytem.after installing open anaconda and create a new file and name it as myfirstML.py (any name you can give)

SO Lets start coding

1) First we are importing pandas module see code

import pandas

Of course you will ask me what is pandas ,in simple pandas is a library used for data manipulations and analysis.

2) Next assign given data in variable ,we have data of last 10 year price of 2BHK apartment

See below test.csv file

2010,180000
2011,220000
2012,240000
2013,250000
2014,260000
2015,290000
2016,320000
2017,360000
2018,400000
2019,500000
2020,550000

url='test.csv'
names=['year','price-for-2BHK']

3) Read CSV with the help of pandas library function

datas= pandas.read_csv(url,names=names)

4) Read CSV with the help of pandas library function

datas= pandas.read_csv(url,names=names)

5) Print data and make sure that we are fecthing data in a variable correctly

print(datas[['price-for-2BHK']])

6) Assign year and price-for-2BHK in respective varibale x and y


x=datas[['year']];
        
y=datas[['price-for-2BHK']]

7) Import train_test_split and split data using train_test_split

train_test_split is a function in sklearn model selection for spiliting data arrays in to two subsets , for training data and for testing data with this function no need to divide the data manually


from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test =train_test_split(x,y,test_size=0.1,random_state=101)

8) import LinearRegression from sklearn.linear_model

LinearRegression is statistical method used to create a linear model.This model describe the relationship between a dependent variable y as function of one or more independent variable


from sklearn.linear_model import LinearRegression

9) Use LinearRegression() and fit method

LinearRegression is statistical method used to create a linear model.This model describe the relationship between a dependent variable y as function of one or more independent variable


model= LinearRegression()
model.fit(X_train,Y_train)

10) Finally predict the price based on the year

print(model.predict([[2030]]))

Output

2BHK price in 2030 :855446.85990338

See full code

import pandas
url="test.csv"
names=['year','price-for-2BHK']

datas= pandas.read_csv(url,names=names)

print(datas[['price-for-2BHK']])

x=datas[['year']];
        
y=datas[['price-for-2BHK']]

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test =train_test_split(x,y,test_size=0.1,random_state=101)

from sklearn.linear_model import LinearRegression

model= LinearRegression()
model.fit(X_train,Y_train)

print(model.predict([[2030]]))