IKH

Operators

List supports following operators;

  • Mathematical operators
  • Comparison operators
  • Membership operators

Mathematical operators

  • We can use + and * operators for List objects.

Concatenation operator(+)

  • We can use + to concatenate 2 lists into a single list.

Example

Python
a=[10,20,30]
b=[40,50,60]
c=a+b
print(c)

Output

PowerShell
[10,20,30,40,50,60]

Note

  • To use + operator compulsory both arguments should be list objects, otherwise we will get TypeError.

Example

Python
a=[10,20,30]
b=a+40
print(b)

Output

PowerShell
TypeError: can only concatenate list (not "int") to list

Repetition Operator(*)

  • We can use repetition operator * to repeat elements of list specified number of times.

Example

Python
x=[10,20,30]
y=x*3
print(y)

Output

PowerShell
[10,20,30,10,20,30,10,20,30]

Comparison Operators

  • We can use comparison operators for List objects.

Example

Python
x=["Dog","Cat","Rat"]
y=["Dog","Cat","Rat"]
z=["DOG","CAT","RAT"]
print(x==y)
print(x==z)
print(x != z)

Output

Python
True
False
True

Note

  • Whenever we are using comparison operators(==, !=) for List objects then the following should be considered.
    • The number of elements.
    • The order of elements.
    • The content of elements (case sensitive).

Note

  • When ever we are using relatational operators (<, <=, >, >=) between List objects, only first element comparison will be performed.

Example

Python
x=[50,20,30]
y=[40,50,60,100,200]
print(x>y)
print(x>=y)
print(x<y)
print(x<=y)

Output

Python
True
True
False
False

Example

Python
x=["Dog","Cat","Rat"]
y=["Rat","Cat","Dog"]
print(x>y)
print(x>=y)
print(x<y)
print(x<=y)

Output

PowerShell
False
False
True
True

Membership operators

  • We can check whether element is a member of the list or not by using membership operators.
    • in operator
    • not in operator

Example

Python
n=[10,20,30,40]
print(10 in n)
print(10 not in n)
print(50 in n)
print(50 not in n)

Output

PowerShell
True 
False 
False 
True

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error