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















Select Menu