avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Summary

The provided web content discusses common sequence operators in Python, including in, not in, +, *, min, max, len, indexing, and slicing, detailing their usage with lists, strings, and tuples.

Abstract

The web content introduces several sequence operators that are applicable to lists, strings, and tuples in Python. These operators include membership (in, not in), concatenation (+), repetition (*), and functions like min, max, and len. The article explains how these operators work with different sequence types, emphasizing that the operands must be of the same type for operations like concatenation and repetition. It also notes that min and max can be used with mixed integers and floats but not with strings and integers together. The content includes examples to illustrate the use of these operators and functions, and it concludes with a humorous remark about the importance of wearing masks during the pandemic.

Opinions

  • The author emphasizes the versatility of sequence operators across different data types in Python, highlighting their importance for sequence manipulation.
  • The use of practical examples is intended to aid in understanding how these operators function in real-world scenarios.
  • The author playfully suggests that those concerned about oxygen intake while wearing masks have likely already experienced brain oxygen deprivation, implying a pro-mask stance amidst the pandemic.

Common Sequence Operators In Python

For lists strings and tuples

Photo by Austin Neill on Unsplash

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 TRUE
lis = [1,3,3]
if 4 in lis:
     print ("yes")
else:
     print ("sorry not here")
will return  sorry not here
strin = “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 9s

Min, 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.5

With 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

Python
Programming For Beginners
Technology
Illumination
Jim Mcaulay
Recommended from ReadMedium