Saturday, 13 June 2020


Python : Working with and Saving data


Python : Loading data with pandas


Python : Writing files with open


Python : Reading files with open


Python : Object and Classes


Python : Functions


Python : Loops


Python : Conditions and Branching


Python : Dictionaries


Python : Sets


Python : Lists and Tuples


Python : String Operations


Python : Expressions and Variables


Python

Types: float

Python first program

Saturday, 28 March 2020

Sum, Subtraction and Multiplication using Class and Object in Python

Sum, Subtraction and Multiplication using Class and Object in Python

I am just learning basic python. I i have committed any mistake please comment there and help me to learn more Thanks

Write code in caluclatorclass.py:


class Calculator:
    enterFirstNumber='Please enter first Number'
    enterSecondNumber='Please enter second Number'
    invalidValueString='Invalid value is passed to'
    def __init__(self):
        print('calculator __init_')
    def sum(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            print('Sum is:',number1+number2)
        except Exception  as e:
            print('{invalidValueString} sum.'.format(invalidValueString=invalidValueString))
    def subtraction(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            subtractionValue=number1-number2
            print('Subtraction is:{subtractionValue}'.format(subtractionValue=subtractionValue))
        except Exception  as e:
            print('{invalidValueString} subtraction.'.format(invalidValueString=invalidValueString))
    
    def multiplication(self):
        try:
            number1=float(input(self.enterFirstNumber))
            number2=float(input(self.enterSecondNumber))
            print('Multiplication is:{0}'.format(number1*number2))
        except Exception  as e:
            print('{invalidValueString} multiplication.'.format(invalidValueString=invalidValueString))


Write code in number-examples.py:

from calculatorclass import  Calculator
class Person:
    calculator=None
    def __init__(self,userName):
        #creats Calculator instance here.
        self.calculator=Calculator()
        #capitalize()/title() capitalizes the first Letter of string. 
        print('Welcome {userName}!'.format(userName=userName.capitalize())) 
        
    def whileloop(self):
        self.displayWhatUserCanInput()
        done=False
        while done==False:
            userinput=input('Enter 5 to exit and any other number to continue.')
            if  int(userinput)==5:
                print('Bye!')
                done=True
            else:
                self.displayWhatUserCanInput()
                done=False
    def displayWhatUserCanInput(self):
        print('''Please enter   1 for sum
               2 for subtraction
               3 for multiplication
                ''')
        try:
            options={
                1:self.calculator.sum,
                2:self.calculator.subtraction,
                3:self.calculator.multiplication
            }
            num=input('Please enter 1,2 or 3')
            func=options.get(int(num),'Invalid value found!')
            func()
        except Exception as ex:
            print (ex)
            print('Invalid value found')

class Welcome:
    def __init__(self):
        print('Welcome to the python demo calculator')
        userName=input('Please enter your name:  ')
        if not userName:
            print('You didn\'t enter your name')
        else:
            person=Person(userName)
            person.whileloop()

#Call welcome class here.
welcome=Welcome()

My observation:-

Here i found while creating classes and calling it.

1. If you have __init__() method in class to call it write abc=ClassName
2. If you have __init__(self) method in class  to call it write abc=ClassName()
3. If you have __init__(self,argument) method in class  to call it write abc=ClassName(argument_value) 

to execute it:-

write in terminal:-

py number-examples.py

Sunday, 22 March 2020

Sum, Subtraction and Multiplication Basic Python program


1. Here i used dictionary  mapping to achieve switch statement.
2. Used format method of the string.
3. Print multiple string in single print method by """.
4. Used while loop to iterate over the user input.


def sum():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        print('Sum is:',number1+number2)
    except Exception  as e:
        print('Invalid value is passed to sum.')
    
def subtraction():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        subtractionValue=number1-number2
        print('Subtraction is:{subtractionValue}'.format(subtractionValue=subtractionValue))
    except Exception  as e:
        print('Invalid value is passed to sum.')
def multiplication():
    try:
        number1=float(input('Please enter first Number'))
        number2=float(input('Please enter second Number'))
        print('Multiplication is:{0}'.format(number1*number2))
    except Exception  as e:
        print('Invalid value is passed to sum.')
options={
        1:sum,
        2:subtraction,
        3:multiplication
    }
def displayWhatUserCanInput():
    print("""
            Please enter number
            1. For Sum
            2. For Subtraction
            3. For Multiplication
            """)
    try:
        i=int(input("Enter the option 1,2 or 3"))
        func=options.get(i,'Invalid value')
        func()
    except Exception as e:
        print('Invalid entry is passed.')
def whileloop():
    displayWhatUserCanInput()
    done=False
    while done==False:
        userinput=input('Enter 5 to exit and any other number to continue.')
        if  int(userinput)==5:
            print('Bye!')
            done=True
        else:
            displayWhatUserCanInput()
            done=False
whileloop()

Friday, 28 February 2020

What are module and package in python?


What is module?
Python basic tool for organizing code is module.
you load modules by using import keyword.

to import module
import my_module

What is package?
A package in python is just special type of module that define characteristics
 of a package is that it contain other modules including other packages,

So, packages are way to define hierarchies of modules in Python.This allow you
to group modules with similar functionalities together in ways that
communicates their cohesiveness.


Thursday, 27 February 2020

How to set/export a directory in python sys path?

SET YOUR DIRECTORY PATH INTO PYTHON SYS PATH


Suppose you are creating module in python in a directory and you want this module to be accessed from any directory you open python IDLE or any editor.
e.g.
I have one test.py file created having below code snippet only.
def printname():
    print('hello world!')

That code is contained on this location. E:\Study\python\higher_order_functions\test.py
Now I have opened my IDLE or Terminal from other directory let’s say from E:\Study\python.
if you import test
It gives error: No module named 'test'
To overcome from this issue and you want this file to be accessed from every directory and make directory(E:\Study\python\higher_order_functions) global:
1   Open terminal from directory(E:\Study\python\higher_order_functions)
2   type py and hit enter button
3    if you are using window OS write set PYTHONPATH= higher_order_functions
4   if you are using Linux OS write export PYTHONPATH= higher_order_functions
5   That’s it. Now your higher_order_functions directory is set in python sys path.

To check if it is set in python sys path
1.     Import sys
2.     sys.path
3.     it gives you array of paths in that your directory would also be listed.
sys.path