Common Sequence Operators In Python
For lists strings and tuples
In a previous lesson I introduced lists, strings and tuples.
In this lesson we are going to look at sequence operators that are common to all three.
They include: in, not in, +, *, min, max, len, indexing and slicing. We will be covering indexing and slicing in more detail in a later lesson.
In, not in
In and not in are used to find out if a particular item is in a given sequence.
lis = [1,2,3]
print (3 in lis)will return TRUElis = [1,3,3]
if 4 in lis:
print ("yes")
else:
print ("sorry not here")will return sorry not herestrin = “123”
if "1" not in strin:
print ("1 is in strin")
else:
print ("1" in strin)This is a bit tricky. “1” is in strin so the line print (“1 is in strin”) will be skipped. The line print (“1” in strin) will return True because once again “1” is in strin. If I had use 1 instead of “1”. an error would be generated telling me that I need to use a string and not an integer.
+
The plus operator will add together or concatenate two items of the same type. if the items are integers it will perform addition.
print (1+1) will return 2
print ("1"+"1" will return 22
print ((1,)+ (1,)) will return (1,1)
print ((1)+ (1)) will return 2
print ([1]+[1]) will return [1,1]
print ((1,)+(1) will return an error.1 or (1) are integers “1” is a string (1,) is a tuple [1] is a list. + will only concatenate items that are of the same type.
*This symbol is used to indicate multiply by. You can multiply integers and floats. It will also multiply sequences.
print ("A" * 3) will return AAA
print ("spam " * 1000) will fill your screen with spam spam spam ...
print ( [4] * 7) will return a l8ist containing 7 4s
print ( (9) * 6) will return 54
print ( (9,) * 6 will return a tuple containing 6 9sMin, Max
Min and max will return the highest or lowest value in a sequence. You may not mix strings and integers. However you may mix intergers and floats.
With numbers it is just the highest or lower value.
print (min (4, 3.5, 9,) will return 3.5With number symbols the letter “z” is lower ranking the the letter “a”. “A” is higher than “a”. the symbol “=” is lower ranking than the numeric symbol “7” , “100” is lower than “2”; “1” is lower than “200" and “10” is lower than “100”. The first digit is checked and if they are the same the second digit is checked. Finally the symbol “=” is lower ranking than the numeric symbols
print (min (“Alice”, “$?+=”, “78”, “350”)) will return $?+=Here is the order of the miscellaneous symbols.
! $ % & ( ) * + , - . / : ; < = > ? ^ _ ` { | } ~Jim McAulay🍁 says. If you are one of those people refusing to wear a mask because you are concerned about enough oxygen getting to your brain, don’t worry that ship has sailed.
41–40
09–08






