Python - Operators

Operators:

  • Operators are symbols and used to perform some mathematics operations.
  • There are 6 kinds of operators,
1.Arithmetic operators: 
  • This is normal mathematic operations such as,

S.No

Operator

Expression

Example

Result

1.

+

Addition

If a=10 and b=5 then

a+b=10+5

15

2.

-

Subtraction

If a=10 and b=5 then

a-b=10-5

5

3.

*

Multiplication

If a=10 and b=5 then

a*b=10*5

50

4.

/

Division

If a=10 and b=5 then

a/b=10/5

2

5.

%

Modulo

If a=10 and b=5 then

a%b=10%5

0.05

6.

**

Power

If a=5 then a**2

25

7.

//

Floor division

If a=10 and b=5 then

a//b=7//5

 1

Example program for the arithmetic operator:
  1. a,b=input("Enter a and b value : ").split()
    print("Add of "+a+" and "+b+" value is : ",int(a)+int(b))
    print("Sub of "+a+" and "+b+" value is : ",int(a)-int(b))
    print("Mul of "+a+" and "+b+" value is : ",int(a)*int(b))
    print("Div of "+a+" and "+b+" value is : ",int(a)/int(b))
    print("Mod of "+a+" and "+b+" value is : ",int(a)%int(b))
    print("Floor divisions of "+a+" and "+b+" value is : ",int(a)//int(b))
    print(a+" power 3 value is : ",int(a)**3)
Output:
2. Relational Operators:
  • It is used to compare two or more data.
Note:
  • These logical operators mostly used in conditions.
3. Logical Operators:
  • It is used to combine simple relational statements into more complex expressions.

S.No

Operator

Expression

Example

Result

1.

and

Both expressions should be true

If  a=11,b=10 and c=12 then a>b and a>c

False

2.

or

One expression should be true

If  a=11,b=10 and c=12 then a>b or a>c

True

3.

not

Both expression false then return true

If  a=5,b=10 and c=12 then

a>b not a>c

True

4. Assignment Operators:
  • These operators are used to getting the result in the same variable(input/output).
Example program for the assignment operator:
  1. sal=int(input("Enter basic salary : "))

    sal+=5000

    print("Bouns with basic salary : ",sal)

Output:
5. Bitwise Operators:
  • It is used to manipulate the data at a bit level and it operates on integer only.
                                                                                  Binary table

Bitwise AND(&):
  • The truth table for ‘ & ‘is shown below(It means True=1 and false=0.)
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of the a&a value is 0000(i.e 0).
Example program for the bitwise AND operator:
  1. p,q=input("Enter two values : ").split()

    r=int(p)&int(q)

    print("Bitwise AND of p&q value is : ",r)

Output:
Bitwise OR(|):
  • The truth table for ‘ | ‘is shown below
a=7 of binary code:0111
b=8 of the binary code is:1000
The result of a|b value is 1111(i.e 15).
Example program for the bitwise OR operator:
  1. p,q=input("Enter two values : ").split()

    r=int(p)|int(q)

    print("Bitwise OR of p|q value is : ",r)

Output:
Bitwise Exclusive OR(^):
  • The truth table for ‘ ^ ‘is shown below
a=13 of binary code:1101
b=8 of the binary code is:1000
The result of a^b value is 0101(i.e 5).
Example program for the bitwise Exclusive OR operator:
  1. p,q=input("Enter two values : ").split()

    r=int(p)^int(q)

    print("Bitwise Exclusive-OR of p^q value is : ",r)

Output:
Bitwise Shift left (<<):
  • The bitwise shift left operator is used to move the bits from right to left in storage.
  • Each box called bits and a=6 and a<<1 then a=12.
Explanation:







Result: a of left shit value is 12.

Example program for the bitwise shift left operator:
  1. a=int(input("Enter a values : "))

    r=a<<1

    print("Bitwise left shit of a value is: ",r)

Output:
Bitwise Shift right(>>):
  • The shit right is similar to the left shit.
  • This operator is used to forward the bits from left to right in storage.
  • For example, If a=6 and a>>1 then its value is 3.

Explanation:


Result: a of right shit value is 3.

Example program for the bitwise shift right operator:
  1. a=int(input("Enter a values : "))

    r=a>>1

    print("Bitwise right shit of a value is: ",r)

Output:
Bitwise One's complement(~) or NOT:
  • The truth table for ‘ ~ ‘is shown below
If a=6(binary code is 0110),
and ~a(0111) value is 7. 
Using a negative signature for one's complement(~a=-7).
Example program for the bitwise one's complement operator:
  1. p=int(input("Enter a values : "))

    r=~p

    print("Bitwise NOT of ~p value is: ",r)

Output:

6. Special operators:

Python language provides some special type of operators like identity operator and membership operator.

(i) Identity operator:

  • An identity operator is used to checking both are equal or not equal.

Operator

Description

Example

Result

is

If both values are the same then return true.

If a=4 and b=5 then a is b

False

is not

If both values are not the same then return true.

If a=4 and b=5 then a is not  b

True

(ii) Membership operator:

  • An identity operator is used to checking a given value is present in a list or words.

Operator

Description

Example

Result

in

If the values are present in the given list or anything then return true.

If a=” Hello”

then ‘l’ in a

True

not in

If the values are not present not in the given list or anything then return true.

If a=” Hello”

then ‘i’ in a

True

Example program for the special operator:
  1. x=int(input("Enter x value: "))

    y=int(input("Enter y value: "))

    z=input("Enter any word: ")

    print("x is equal to y: ",x is y)

    print("x is not equal to y: ",x is not y)

    print("Character \'a\' in "+z+" : ",'a' in z)

    print("Character \'p\' not in "+z+" : ",'a' not in z)

Output:
Share:

Python - Basics

Basics of python language:

To write a c++ program, the following details are needed.

1. Command-line:

  • It describes what's going on inside a program, so that person looking at the source code does not have a hard time figuring it out.

Types of comments:

    (i) Single line comments:

     Syntax:

                 #Statements

     Example:

                   #print function

    (ii) Multiline comments:

     Syntax:

                  ‘’’Set Statements ‘’’

2.Variables:
  • A variable is nothing but it allocates the memory space and holds the values.

Syntax fro declaring variables:

                     variable_name=value;

Example:

                   a=0 #int data type

                   s=’ ‘ #String data type                     

Rules for naming the variables:

  • The first character must be an alphabet or an underscore (_).
  • No commas or blank spaces are allowed within a variable name.
  • No special symbols, an underscore can be used in a variable name.

3. Data types in python:

  • The most widely used data types in the input function is:

4. Output function:

  • This function is used to show details on the output screen for user understanding.

Syntax:

          print(“Statements”) or

          print(‘Statements’)

5. Escape sequence: 

  • This is used to align output on the screen.


Escape sequence
Description
Example
Output
\n
Newline or next line
print(“Hai! \n I am Sabrina.”)
Hai!
 I am Sabrina.
\t
Horizontal tab(one tab space)
print(“Hai\t Hello”)
Hai                         hello
\’
Display single-quotes
print(“I don\”t know”)
I don’t know
\”
Display double-quotes
print(“/“Hey/”’)
“Hey”
\\
Print slash
print(“a//b”)
a/b

















6. Input function:

  • This function is used to get value from users.

Syntax:

            Variable_name = data_type(input(“ Sentence “))

Example:

             a=int (input(“Enter a value : “))

Example program: save example.py

  1. #Write a python program to add of two elements

    name=input("Enter your name : ")

    a=int(input("Enter a value : "))

    b=int(input("Enter b value : "))

    c=a+b

    print("Add of and b value is : ",c)

Explanation:

name=input("Enter your name : ")

 or

name=str(input("Enter your name : ")) both are same for getting string values.

Output:

Example 2:

  • If need multiple inputs at one line, then refer to the below program.

Program:

  1. name=str(input("Enter calculation name : "))

    a,b=input("Enter a and b value : ").split()

    c=int(a)+int(b)

    print("Add a of and b value is : ",c)

Explanation:

  • split() function is used to separate a string from each space.
  • int(a)+int(b) means, a and b value has string so should be changed into number data type because here performing add operation.  

Output:

Example 3:

  • If displaying value at the sentence like “Add of a and b value is : 10” as “Add of 5 and 5 value is :10”

Program:

  1. name=str(input("Enter calculation name : "))

    a,b=input("Enter a and b value : ").split()

    c=int(a)+int(b)

    print(name+" of "+a+" and "+b+" value is : ",c)


Explanation:

  • In print function, the + operator is used to join variable of value to a string value.

Output:

Share:

Introduction of python programming language:

 Python programming language

What is python?

  • Python is a powerful high level, object oriented, interpreted language.
  • Proposed by Guido van Rossum in 1989.
  • It has simple, easy to use syntax and making it the perfect language for someone trying to learn computer programming for the first time.

Wherefrom the python has derived?

  • In the 1980s, Guido van Rossum was working on the Amoeba distributed OS (ADOS).
  • He has used ABC language (i.e., interpreted language). This is simple easy to understand syntax in ADOS.
  • So, he decided to create a language that was extensible (fast).
  • This is lead to the design new language which was later named “Python”.

Why the name python came?

  • Guido van Rossum was a fan of a comedy serial from the late seventies.
  • So, the name python was adopted from the same series “Monty Python’s Flying Circus.

Features of python language:

  • A simple language that is easier to learn.
  • Free and open source
  • Extensible
  • Large standard library to solve common task.
  • Object-oriented.  

Applications of python:

  • Web application (Flash, Pyramid, crome, etc)
  • Scientific and numerical computing (earthpy for earth science, astropy for astronomy, and so on).
  • Data analytics and machine learning.

Software companies that use python:

  • Google
  • Quora
  • Netflix
  • Facebook
  • Instagram
  • Dropbox etc

Reasons to choose python language:

  • Python interpreter act as a calculator because python has modes.

1.Shell mode (Python 3.5(32-bit):

  • Shell mode immediately returns the result of the command for you to enter in the shell.

2.Script mode (IDLE(Python 3.5 32-bit):

  • The script mode is used to write the script then run and also possible to immediately ran output.
  • It contains edit options and you can be also, save that py document.

Working with script mode:

Numbers:

String:

  • A string is a collection of characters or alphabetic and form a sentence or word called a string.
  • The strings are represented by single or double quotes (“word or sentence“ or
  • ’word or sentence‘).

Note:

Compiler:

  • It is used to merge all programs into machine code and checking errors.

Interpreter:

  • It is used to check error line by line and change it line by line machine codes.

If you need more details about the programming language, low-level language, middle-level Language, and high-level language means click the below links to learn. 
https://www.whereisstuff.com/2020/09/introductions-of-programming-language.html
Share:

Encapsulation in java

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public methods()

    {

    }

    }

Example program:
  1. class Account {

        private int account_number;

        private int account_balance;

     

        public void show_Data() {

            account_number=13456;

            account_balance=5000;

            System.out.println("Before deposit of "+"account XXXXX"+account_number+" number of balance :"+account_balance);

        }

     

        public void deposit(int a) {

            if (a < 0) {

                System.out.println("Given amount is wrong...");

            } else

                account_balance = account_balance + a;

            System.out.println("After deposit of current balance :"+account_balance);

        }

    }

     

    class Encapsulation{

        public static void main(String args[])

        {

            Account ac=new Account();

            ac.show_Data();

            ac.deposit(200);

        }

    }

Output:
Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me