python substring

  • In python, we have the string concept from that we have a small topic called substring.
  • "substring " is the string that is a part of the main string.
  • In other words, we can say that extraction of the characters from the string.
  • We can access the string by using the index and substring by using the concept of slicing.
  • For the creation of the list of substrings, we use the split().
  • It follows string[start:end: step] in slicing concept by this the substring is formed.
  • startIt is the starting index of the substring. By default, it is 0.
  • end: It is the ending index of the substring. By default, it is the length of the string.
  • step: It is used to increment the index by the given numbers.
  • We use some of the functions in the substrings.
  • In operator: It is used to find the substring that is present in the string.
  • Count(): It is used to count the number of times the substring occurred in the string.It is denoted by the "*" symbol.

#Write a python program to find the substring from a string.

  1. s="hello every one"
  2. print(s[:])
  3. print(s[:5])
  4. print("h" in s)
  5. s=s*3
  6. print(s)

output: hello every one
             hello
              true
              hello every one hello every one hello every one.


python string operations

  • In python, we have some operations that perform the specified operations on strings.
  • In this, the string must enclose with the codes that might be single or double.
  • Here are some python string operations.

1.  String assignment operator:

  • In this operator, the string is assigned to any of the variables.
  • The string may be in single or double codes.
  • It is represented by " =" equal to symbol.
Syntax: string ="any variable."

Example:  string1="python"
                  string2="java"

In the above example, we assign a "python" to string 1 and "java" to string 2 by using the" =" symbol.

# write a python program for the string assignment operator.

  1. str1="welcome to python"
  2. str2="Today topic is string operator"
  3. str3="python is easy  language"
  4. print(str1)
  5. print(str2)
  6. print(str3)
output:  welcome to python
             Today topic is string operator
             Python is easy to language



2. Cancatenate operator:

  • This operator has used to cancate the strings.
  • Concatenation means joining or combing the strings by using the plus symbol between the strings.
  • It is represented by the symbol " +".
       Syntax:  (str1+str2)

       Example:   "Hello"  +"world"
  •  Result is : "Helloworld"

#Write a python program using string cancatenate operator.

  1. str1="a"
  2. str2="is"
  3. str3="alphabet"
  4. print(str1+str2+str3)
output: aisaphabet


3. String repetition operator: 

  • In this operator, the string is repeated the number of times that we declare in the code.
  • It is represented by the symbol "*".
Syntax:  str*n    Here n is the number of times that the string can be repeated.

Example: str="apple"
                 str*2
The result is: apple apple
  • here we declare the string as apple it should be repeated for 2 times.

#Write a python code using the string repetition operator.

  1. str="welcome"
  2. print(str*5)
output: welcome welcome welcome


       

       4.Slicing operator:  

  • In this slicing operator, the strig is divided according to the given index values.
  • The string can y accessed by using the index.
  •  It is having positive index and negative index values.
  • Positive values start from  0 from left to right.
  • The negative index starts from -1 from right to left.
  • [:] this gives all values in the index 0 to end. 
  • The slicing never shows the error, if the index is out of range, it shows an empty list[].  

Syntax: str[]

Example:   str="string"
                   str[0]=s
                   str[1]=t
                   str[-1]=g
  • In the above example, the s index is 0 so it returns s when we write str[0].

#write a python code for slicing operator.

  1. str="coding"
  2. print(str[0])
  3. print(str[-1])
  4. print(str[1:6])
  5. print(str[1:])
  6. print(str[:3])
  7. print(str[1:-4])
  8. print(str[-3])
output:  c
             g
             oding
             oding
             cod
              o
              i                                                                                 


                                       

5. String comparison operator:

  • In the string comparison operator, we compare the strings.
  • In this, we have 2 operator
     1.Equal to(==): If both the strings are the same then it returns true else false.
  • It is represented by the symbol "==".
     Syntax:str1==str2.

       2.not equal(!=): If both strings are not the same then it true else false.
  • It is represented by the symbol "!=".
Syntax:  str1!=str2

#write a python coding using the python string comparison operator.

  1. str1="my name is A"
  2. str2="my name is B"
  3. str3="my name is A"
  4. str4="apple"
  5. str5="apple"
  6. print(str1==str2)
  7. print(str1==str1)
  8. print(str4==str5)
  9. print(str4!=str1)
  10. print(str3!=str2)
output: false
            true
            true
            true
            true



6. Membership operator:

  • In the membership operator, we check the string that is declared in the code is present in the given list or not.
  • There are 2 operators in membership.
  1. In:  It tells that the string is present in the given list.If it is present returns true else false.
  2. Not in: It tells that the string is not present in the given list returns true else false.
Example: str="string"
                r in the str
       The result is: true

#write python code using membership operator.

  1. str="Hello world"
  2. print("H" in str)
  3. print('s' in str)
  4. print("a" not in str)
output:  true
             false
             true



7. Escape sequence operator:

  • This operator is used when an unallowed character is present in the given string.
  • It is represented by the "\" symbol called backslash. 

Write a python program for escape sequence operator.

  1. str="welcome to \"python\" classes"
  2. print(string)
output: welcome to "python" classes





8.String formatting operator:

  • It is used to format the string as per our requirement.
  • There are different types of formats.
  • It is denoted by that "%" symbol.
  • Each format has its own meaning.
%d:  used for sign decimal integer.
%s: used for string.
%f: used for floating.
%u: used for unsigned decimal integer.
%c: used for the character.

#write a python code using the string formatting operator.

  1. name="abc"
  2. marks=78
  3. str1='hi % s'%(name)
  4. print(str1)
  5. str2='hi% s,my marks is %f'%(name,marks)
  6. print(str2)
output: hiabc,my marks is 78.000000


python string methods

 Python string methods:

  • Before going to string methods first of all we have to know what is meant by string.
  • A "String is a set of or group of characters."
  • The string always must be closed with single, double, or triple codes.
  • Triple codes (''' ''') are used for multi-line purposes.
  • In python, we are having some string functions, that are used to operate on strings.
  • As the string is immutable it always returns the new values only. The old values cannot be replaced.
  • The string function is defined as str().

Example:

#1.write python code using the str() methods.

  1. s'"Hello"
  2. print(s[0])
  3. print(s[1])
  4. print(s[2])
  5. print(s[3])
  6. print(s[4])
output:  H
              e
              l 
              l
             o


#2. write a python code using str() functions.

  1. str="Hello"
  2. print(str.lower())
  3. print(str.upper())
  4. print(str.count(str))
output: hello
             HELLO
              1



  • In the following table, we will find more functions and their respective actions in the string.

MethodDescription
len()used to determine the length of the string.
isalnum()used to define the string is alphanumeric.
islower()To define a string is in small letters.
isupper()To define a string is in capital letters.
isnumeric()The string consists of only numbers.
istitle()The string is in the title case.
isspace()The string has only white space.
find()Searches the string value and returns the position.
format()Formats specified values in a string
format_map()Formats specified values in a string
index() returns the position of string where it was found
count()Returns the number of times string occurs.
stratswith() the string starts with the given prefix.
isdecimal() all characters in the string are decimals
isdigit() all characters in the string are digits
isidentifier()Returns True if the string is an identifier
endswith() all characters in the string end with the given suffix. 
encode()it encodes the string.
isprintable()all characters in the string are printable.
capitalize()it returns all the capital strings.
center()Returns the center string. 
_contain_() the string we check is contained in another string or not.
join()Joins the elements of the string.
ljust()Returns a left-justified version of the string
hash()Returns the hash value of the given object.
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace() string where a value is replaced with another value.
rfind()Search the string and returns the last position.
rindex()Search the string and returns the last position.
rjust()Returns a right-justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim  of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
id()Returns the identity of the string.
strip() It trims the space between the strings.
swap case()In this, the lower case becomes the upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into the upper case.
zfill()Fills string with a specified number of 0's at the beginning.

C initialize an array

  • Array is collection of similar data items.
  • An array can hold multiple values of same type of data
  • In c programming language an array can be declared as datatype arrayName [ arraySize ]; 
  • In c , while declaring an array we need to specify type of array , name of array and size of the array.
  • One can directly initialize an array while declaring itself with some values
  • int arrayVariable[2]=[10,20];
  • int count[]=[1,2,3,4,5]
  • directly initialize an array of particular : count[3]=10
  • Let us discuss about how to initialize an array with some default values.
  • Using for loop or while loop we can iterate an array and by accessing each index we can assign values 
  • If we are not initializing an array and try to print all the values then it will print some garbage values.
  • c initialize array of ints

#1Write a C program to declare an array and print default values
  1. #include <stdio.h>
  2. // write a c program to explain how to initialize an array
  3. int main () {

  4.    int number[ 15 ]; 
  5.    int i,k;
  6.    
  7.    
  8.    for (k = 0; k < 15; k++ ) {
  9.       printf("Array[%d] = %d\n", k, number[k] );
  10.    }
  11.  
  12.   getch();
  13. }


Output:
  1. Array[0] = -1551778920
  2. Array[1] = -2
  3. Array[2] = 1971427546
  4. Array[3] = 1971495162
  5. Array[4] = 5189464
  6. Array[5] = 5181048
  7. Array[6] = 2686776
  8. Array[7] = 1971494685
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 2686832
  12. Array[11] = 200
  13. Array[12] = 192
  14. Array[13] = 7161720
  15. Array[14] = 48


#2 write a C Program to initialize an array values to 0 (zeros).

initialize an array in c


Output:
  1. Array[0] = 0
  2. Array[1] = 0
  3. Array[2] = 0
  4. Array[3] = 0
  5. Array[4] = 0
  6. Array[5] = 0
  7. Array[6] = 0
  8. Array[7] = 0
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 0
  12. Array[11] = 0
  13. Array[12] = 0
  14. Array[13] = 0
  15. Array[14] = 0

spilt function in python

 Split function: 

  • The split() function is used to split the string by separating them with the split().
  • The split()  method contains the different separators  like comma(,) colon(:) semicolon(;)  and space also.
  • By default when we write split() function it will separate the values by "space ".
  • After splitting the string it returns a list of strings.
Syntax: str.split(separator,max split)
  • Example:
  • y=[int(x) for x in input("enter the 2 numbers:").slipt(",")]
  • Here, the 2 numbers are 10 2 by using split(",") these numbers are separated by"," as 10,5.
  • There are  3 parameters in the split() method
  1. separator: It separates the string by using separators if we do not use then by default it takes space.
  2. max split:  It is used to split the string by the max number of times. By default, it takes 1 i.e no limit for splitting. 
  3. Returns: After splitting the string it returns the list of string values.

# write a python code using the split().

  1. a="apple is a fruit"
  2. b=" dog is a animal"
  3. c="python is easy to learn"
  4. print(a.split())
  5. print(b.split())
  6. print(c.split())
output: ['apple' , 'is' , 'a' , 'fruit' ]
            ['dog'  ,'is'  ,'a', 'animal']
            ['pthon'  , 'is'   , 'easy'  ,'to' , 'learn']



python Ternary operator

Ternary operator: 

  • The ternary operator is defined as the operator that evaluates the conditions to find the final result.
  • It evaluates more than one expression.
  • A  single line code is written according to the condition.
syntax:  x= first value if condition else second value.
In this syntax, if the first value satisfies the condition then it returns the x value, if the first value doesn't satisfy the condition then it goes to the else part and checks the second condition.

Example: x=20 if 5<10 else 30 we get the result as 20.
Because here condition 5<10 is grue so it returns the x value as 10.

  • There is another syntax for the ternary operator when there are more than 2 conditions i.e;
  • syntax: x= first value if condition 1 else second value if condition 2 else third value.
Example: x=1 if 20>30 else 2 if 50>60 else  10. 
                The result is  10.
  • In the ternary operator, we can perform a nested loop also.
  • We can reduce the length of the code by using this ternary operator, in this operator we write a code in a single line.
  • So time is saved and less complexity in the code.

#write a python program for integers using the ternary operator.

  1. a=8
  2. b=9
  3. max= b if b>a else a.
  4. print(max)
output: 9



python not operator

 Python not operator:

  • python not operator is the complement of the given value i.e; if the value is true then it returns false and if the value is false then it returns true.
  • When we  apply the not operator we get result  in  boolean type .
  • example:
                 x=10 
                 not x, then the result is false because x=10 is true so not x is false.
  • syntax:(not x).

 # write a python program using not operator.

  1. x="python"
  2. y="java''
  3. print(not x==y)
output: true

 

python and operator

 python And operator:

  • The python And operator is one of the logical operators.
  • In And operator, if both the operands are true then it returns true, otherwise false.
  • And operator is applicable for boolean type and non-boolean type.
  • For example: 1 And  " a"  then the result is "a".  
  •  It is represented by the  "&" symbol.
  •  It is written as  x &y
syntax:(a&b).

# write a python program using And operator.

  1. x=23
  2. y=44
  3. print(x>10 and y>10)
  4. print(x<40 and y<40)
  5. print(x&y)
  output:   true
                false
                 4

Types of operators in python

 Operators: 

  • Operators are nothing but the special symbols used in between the operands.
  • In python, we use the operators to perform some arithmetic, logic, and some more operations.
  • These operations are performed on the variables, values, arguments nothing but the operands.
  • We can perform operations on one or more operands.
  • In python, we have six operators.
  • Arithmetic( to add),comparison(to compare), logical( perform logical operations), bitwise(cal bitwise operations), assignment(assign values), special(check address and content ).

1.Arithmetic operator:

  • It is also called a math operator and it applicable for all int and float types values.
write a python program using the math operators/arithmetic operators.



2. comparison operator:

  • It compares and shows the relation of variables.
write a python code using comparison operators.




3. Logical operator: 

  •  This operator performs the logical operations on variables.
  • There are 3 logical operators.
write a python program using logical operators.



4. Bitwise operator:

  • It is used to perform bit-wise calculations.
  • We have 6 operators and applied to int and bool.
write a python code using bitwise operators.



5. Assignment operator:

  • It assigns the values to variables.
  • we have 12 operators in it.
write a python program using the assignment operators.



6.Special operator:

  • In, not in and is, is not are special operators.
  • Membership operators and identity operators are 2 types in it.
  • Membership operator is used to giving member is present in the list or not.
  • eg: list=[1,2,3]
  • pint(1 is a list )
  • output: true.
write a python program using the special operator.



python math operators

Math operators:

  • In python, we have various arithmetic operators.
  • These are called math operators because they perform different types of mathematical operations.
  • Here  we have seven types of arithmetic operators:
  1. +     addition
  2. -      subtraction.
  3. /       division.
  4. %     modulus.
  5. *       multiplication.
  6. //       floor division.
  7. **      exponential.
1.Addition(+): It is used to add two or more operands.
It is represented by the "+". symbol.
syntax:(a+b).

2.subtraction(-): It subtracts the value of the  one operand to other operands 
It is represented by the symbol "-".
syntax:(a-b).

3.Division(/): It divides one operand with another operand and gives the remainder. The result is always in float type only.
It is represented by the symbol "/'.
syntax:(a/b).

4.modulus(%): It is the remainder of the operands.
It is represented by the symbol "%".
syntax:(a%b).

5.Multiplication(*): It multiply the two or more operands.
It is represented by the symbol "*".
syntax:(a*b).

6.floor division(//): It divides the operands and get the result in the whole number only.
It is represented by the symbol "//'.
syntax:(a//b).

7.exponential(**): It is used to calculate the power of the values.
It is represented by the symbol "**'.
syntax:(a**b).


Example: write a python program for integers using math operators.

  1. x=3
  2. y=4
  3. print(x+y)
  4. print(x-y)
  5. print(x/y)
  6. print(x%y)
  7. print(x*y)
  8. print(x//y)
  9. print(x**y)
output:     7
               -1
               0.75
                3
               12
               0
               81




python division operator

  • The division operator in python is used to perform the division operations.
  • It divides the left-side values with the right-side value and gives the remainder.
  • In other languages, we have only one division operator but in python, we have 2 division operators.
  1. division.
  2. floor division.
 1.Division: In division when we divide values we get the result in floating type.
It is denoted by the symbol "/".
 syntax:(x/y).

Example: write a python code for integers using a division operator.

  1. x=20
  2. y=4
  3. print(x/y)
output:5.0



2.floor division(//):  In the floor division when we divide the values we get the whole number only.
It is denoted by the symbol "//".
syntax:(x//y).

Example: write a python program for integer values using floor division.

  1. x=20
  2. y=4
  3. print(x//y)
output: 5















Python modulo operator


In Python, the modulo operator is represented by the percent sign (%). It is used to find the remainder of a division operation between two numbers. 
Here's an example of  modulo operator In Python

x = 7
y = 3
remainder = x % y
print(remainder)

This will print 1 as the remainder of 7 divided by 3 is 1.
modulo operator to check if a number is even or odd:


x = 7
if x % 2 == 0:
    print(x, "is even")
else:
    print(x, "is odd")
This will print "7 is odd" because the remainder of 7 divided by 2 is 1.

The modulo operator can also be used to wrap around a range of values. For example, you can use it to ensure that an index is always within the bounds of an array, even if the index is negative or greater than the size of the array:


index = -1
size = 5
correct_index = index % size
print(correct_index)

This will print 4 as the correct index, because -1 % 5 = -1, but we want the index to be between 0 and 4.

It's worth noting that, the modulo operation is defined as the remainder of the division of one number by another, i.e x % y = x - y * floor(x/y)
where floor is the floor division operator, that returns the quotient of the division and also discards any remainder.

  • Modulo operator:  The modulo is defined as "the remainder of the two arguments ".
  • It is denoted by the symbol   "%".
  • When we divide any number with 0 i.e;    x%0 the result is zero division error.
  • We can use int, float, and double values also.
  • Those int, float, double may be either positive or negative.
syntax:(x%y)
x is the first argument and y is the second argument.

#1.write a python program using the modulo operator.

  1. x=10.0
  2. y=2.0
  3. print(x%y)
output: 0.0


#2.write a python program for a value divides with 0 using the modulo operator.
  1. x=10.0
  2. y=0
  3. print(x%y)
output: zero division error


Python bitwise operators

  • Bitwise operator:  In python, the bitwise operator is used to performing the bitwise calculations on integers.
  • It can apply to boolean type also.
  • It will be converted into binary numbers and bitwise operations in integers first on that binary number.
  • As it performs the bit to bit operations the name is called a bitwise operator.
  • There are six types of bitwise operators in python they are:
  1. &   And.
  2. |      or.
  3. ^      xor.
  4. ~     bitwise not
  5. >>   right shift.
  6. <<   left shift.
1.And(&): If both bits are then it returns true, else false.
It is denoted by the "&" symbol.
syntax: (x&y).

Example: write a python program for integer values using And operator.

  1. x=4
  2. y=5
  3. print(x&y)
output: 4


2. or(|):  If any one of the bits is true then the result is true, else false.
           It is denoted by the symbol "|".
syntax:(x|y).

Example: write a python program for integer values using or. 

  1. x=10
  2. y=5
  3. print(x|y)
output: 15


3. xor(^): when both bits are different returns true, else false.
It is denoted by the "^" symbol.
syntax:(x^y)

Example: write a python code for integers using xor operator.

  1. x=1
  2. 2
  3. print(x^y)
output: 3


4. Bitwise not (~): It is the complement of the bit if the bit is 1 if the result is 0, if the bit is 0 then it result is 1.
It is denoted by the "~" symbol.
syntax:(~x)

Example: write a python code for bool type using bitwise not operator.

  1. x=true
  2. print(~true)
output: -2.



5.Right shift(>>): It shifts one bit right and fills the left side vacant cells with significance values.
It is denoted by ">>".
syntax:(x>>).

Example: Write a python program using the right shift operator.

  1. x=11
  2. y=2
  3. print(x>>y).
output: 2



6.Left shift(<<): It shifts one bit left and the right side vacant cells fill with 0.
It is denoted by the "<<" symbol.
syntax:(x<<)

Example: write a python code for int values using the left shift operator.

  1. x=11
  2. y=2
  3. print(x<<y)
output:44


python comparison operators

  • In python, the comparison operator is used to compare one value with the other value.
  • A comparison operator can be known as a relational operator as it shows the relation between the two values.
  • It returns the result in bool type i.e True or False.
  • It is applicable for string, boolean, numbers(int type).
  • In the comparison, if one comparison is true then the result will be true, and if one comparison is false all the results will be false.
  • Eg; 10<20>30, the result is false.
  • We have six different types of comparison operators/ relational operators
  1. ==  Equal to.
  2. != not equal to.
  3. >  Greater than.
  4. <   Less than.
  5. >= Greater than or Equal to.
  6. <= Less than or equal to.
  1. Equal to(==): If the values on both sides of operands are equal then the result is true, else false.
        Equal operator(==)  always check the content comparison. 
 syntax: (a==b).

Example: write a python code for comparison/relational operator using equal to the operator for string type.

  1. x='AA'
  2. y='aa'
  3. print(x==y)
output: false


2.     Not Equal to (!=): If both values in operands are not equal it results in true, else false.
syntax: (a!=b).

Example: write a python program using the not equal operator for an int data type.

  1. x=10
  2. y=20
  3. if(x!=y):
  4. print("x not equal to y")
output: x not equal to y.



3. Less than(<): If the value left operand is less than the right operand we use less than.
syntax: (a<b).

Example: write a python program using less than operator for string type.

  1.  x="a"
  2. y="A"
  3. print(x<y)
output: false.



4. Greater than(>):  If the value of the left operand is greater than the right operand then the result is true. 
syntax: (a>b).

Example: write a python program for string data type using greater than the operator.

  1. x="a"
  2. y="A"
  3. print(x>y)
output: true

5.Less than or equal to(<=): If the value in the left operand is less than or equal to right to operand it returns a true value.
syntax:(x<=y).

Example: write a python code for int data type using less than or equal to operator.

  1. x=100
  2. y=200
  3. if(x<=y):
  4. print("x less than or equal to y")
output: x less than or equal to y.


6. Greater than or equal to(>=): If the values in the left operand are greater than or equal to the right operand then the result is true else false.
syntax: (a>=b).

Example: write a python program for int data type using >= operator.

  1. 1000
  2. 200
  3. if(x>=y):
  4. print("x is greater than or equal to y")
output: x is greater than or equal to y.

Logical operators in python

  • In python, we have the logical operator.
  • "Logical operator" used to perform the different logical operations on the value of variables.
  • The logical operators are applicable for boolean and non-boolean types.
  • Boolean types are:
  • True, False
  • non-boolean types are :
  1. 0
  2. 1
  3. empty string. 
  • Here True means 1 and Flase means 0.      
  • There are three different types of logical operators, they are :
  1. And
  2. or
  3. Not
  •   And operator: And operator is used when both arguments are True then the result is True.
  • syntax:  x and y

Example: write a python code for And operator.

1.a=10
2.b=20
3.print(a>30 And b<30)
4.False



  •  or operator: or operator is used when at least any one of the arguments is true then the result will be True.
  • syntax: x or y

Example: Write a python code using the or operator.

  1. a=2
  2. b=5
  3. print(a<3 or b>)



  • not operator: not operator is the opposite to the result of an argument, if it is true then the result is False and vice-versa.
  • syntax: x not y

Example: write a python code using the not operator.

  1. x=5
  2. y=10
  3. print(not x==y)
  4. True

Assignment operator in python

  •  In python, we have the assignment operator
  • Definition: The assignment operator is used to assigning a value to variables.
  • eg:x=100
  • Here we just assign a value of 100 to the x variable.
  • we use the '"="  symbol to assign the values to the variables.
  • After assigning the values to the variables it performs the different types of operations.
  • The assignment operator mixed with another operator is called compound operator.
  • The different types of compound operators are:
  1. +=       
  2. -=      
  3. *=     
  4. /=      
  5. %= 
  6. //=    
  7. **=   
  8. &=
  9. |=
  10. ^=
  11. >>=
  12. <<=

Example: write a python program  using assignmnet operators.

  1. x=5
  2. x+=5
  3. print(x)
  4. 10
  5. x=2
  6. x-=2
  7. print(x)
  8. 0
  9. x=2
  10. x*=12
  11. print(x)
  12. 144
  13. x=10
  14. x/=5
  15. print(x)
  16. 2.0
  17. a=4
  18. a%=2
  19. print(a)
  20. 0

















Identity operator in python

  • Identity operator, before going to identity operator let's have a small idea about its background.
  • We have multiple operators from them special operator is the one, in that special operator we are having two types of operators :
  • 1.Identity operator.
  • 2.Membership operator.
  • Now let's talk about the identity operator,
  • "Identity operator" is used to compare whether the value in the first argument may be or may not be the same in the second argument.
  • In identity operator we have two  types, they are:
  1. is
  2.  is not
  • "is" operator is used to saying that the value is present in the corresponding argument.
  • If the value is present in the corresponding argument then it returns True, else False.
  • is operator always checks the address comparison
  • "is not " operator is used to saying that the value is not present in the corresponding argument.
  • If the value not presents then it returns the True otherwise False.

Example: write a python program using is identity operator.

  1. a="apple"
  2. b="apple"
  3. print(a is b)
  4. True


#2.write a python program using isnot operator.

  1. a="Apple"
  2. b="apple"
  3. print(a isnot b)
  4. True









































Python power operator

     let us know in detail about the python power operator.

  1. Power operator  is one of the arithmetic operator .. 
  2.  In python the power operator  can  be used  to calculate the  power of a number or  exponential .  
  3.  The power  operator in python is denoted by " ** " symbol. 
  4.  It always returns the float types values  only.
  5.  The power operator is having the  second highest  priority in the operator precedence as first priority goes to parenthesis. 
  6.  The power operator can be calculated in two ways :
  7.             1.  using **

                2.  using pow()function.

    • For two arguments we write the power as (a,b) i.e  a to the power of b
    • For three it is written as (a,b,c) i.e x to the power of b, modulus c.
    • The syntax for 3 arguments is (a,b,c).

    Now let us  go to  some practical examples:

    #1.write a python program to calculate the power of a number

    1. x=12**2
    2. print(x)
    3. 144


    #2.write a python program to calculate the power of a number.

    1. x=1**2
    2. print(x)
    3. 1










    Python not equal operator

    • Let's discuss python, not equal operators.  
    •  In python, there are different types of operators available,in that we have not equal operators.
    • Not equal to operator comes under the comparison operator as we compare the one variable with the other.
    • Python not equal to the operator is used for the same data type values.
    • When we compare one variable with another variable by using not equal operators we will get the boolean type values. bool type is True or False. if the values are not equal then it returns True value and if the values are equal then it returns the False value.
    • The syntax for not equal to is !=  mostly used in the python 3 versions.

    #1. write a python program to describe not equal to operator using integer

    1. a=2.5
    2. b=1.0
    3. c=5.0
    4. print(a!=b)
    5. true
    6. print(b!=c)
    7. true
    8. print(a!=b!=c)
    9. true

    python not equal operator

    #2. write a python program to describe not equal to an operator using string

    • Python, not equal operator  using string
    1. x='apple'
    2. y='ball'
    3. print(x!=y)
    output: true


    Select Menu