11---
2- description : Comparison and Arithmetic Operations in Python.
2+ description : Comparison and Arithmetic Operators in Python.
33---
44
5- # Comparison and Arithmetic Operations
5+ # Comparison and Arithmetic Operators
66
7- A quick revision on what all concepts we have learned so far:
7+ A quick revision of the concepts we have learned so far:
88
99- Brief on Python history
1010- Variables
1111- Data types (strings, integers, floats, booleans)
12+ - Built in functions
1213- String concatenation & interpolation
1314
1415In this chapter we will be looking at what comparison and arithmetic operations are.
1516
1617## Comparison Operators
1718
18- Comparison Operators (also known as Relational Operators), are used to compare values. They're very
19- similar to how they're in mathematics.
19+ Comparison Operators (also known as Relational Operators) are used to compare values. They work very
20+ similar to how they do in mathematics.
2021
21- There are six types of comparison operators as follows :
22+ There are six comparison operators:
2223
2324| Operator Symbol | Name |
2425| ---------------- | ----------------------------- |
2526| ` > ` | Greater Than |
26- | ` < ` | Lesser Than |
27+ | ` < ` | Less Than |
2728| ` == ` | Equal To |
2829| ` != ` | Not Equal To |
2930| ` >= ` | Greater Than Or Equal To |
30- | ` <= ` | Lesser Than Or Equal To |
31+ | ` <= ` | Less Than Or Equal To |
3132
32- Comparison operators always return a boolean values (` True ` or ` False ` ) when used.
33+ Comparison operators always return a boolean value (` True ` or ` False ` ) when used.
3334
3435<figure markdown =" span " >
3536
3637![ Image title] ( /static/python-tutorial/06-operators.png ) { width="640" }
3738
3839<figcaption >
39- The either side of the operator is what we call an operand.
40+ Either side of an operator is what we call an operand. An operator is a symbol that performs an action on values, called operands .
4041</figcaption >
4142
4243</figure >
@@ -56,7 +57,7 @@ Running this will give us the following output: ```text False True False ```
5657This reads almost like plain English! 1 is not greater than 2, hence it returns and prints out
5758` False ` . 20 is larger than 1, which results the output being ` True ` , and so on.
5859
59- ### Lesser Than Operator (` < ` )
60+ ### Less Than Operator (` < ` )
6061
6162Checks if the right hand side operand is larger than the left hand side operand.
6263
@@ -94,16 +95,15 @@ False
9495
9596!!! warning
9697
97- Do not confuse between the Equivalence Operator (`==`) and the Assignment Operator (`=`). The
98- Equivalence Operator is used to check if both the operands are equal, whereas the Assignment
99- Operator is used to assign data to a variable.
98+ Do not confuse the Equal To Operator (`==`) with the Assignment Operator (`=`). The Equal To
99+ Operator is used to check if both the operands are equal, whereas the Assignment Operator is used to
100+ assign data to a variable.
100101
101102### Not Equal To Operator (` != ` )
102103
103104Checks if both the operands are _ not_ equal.
104105
105106``` python
106-
107107print (10 != 10 )
108108print (3 != 4 )
109109print (3 != 8 )
@@ -131,14 +131,14 @@ Output:
131131
132132``` text
133133True
134- True
135134False
135+ True
136136```
137137
138138Here, 90 is greater than 18 which gives us ` True ` as the output. 23 is not greater but it is equal
139139to 23 which also gives us ` True ` as the output.
140140
141- ### Lesser Than Or Equal To Operator (` <= ` )
141+ ### Less Than Or Equal To Operator (` <= ` )
142142
143143Checks if the right hand side operand is either greater than or equal to the left hand side operand.
144144
@@ -156,7 +156,47 @@ False
156156True
157157```
158158
159- ## Arithmetic Operators
159+ ### Comparison with Strings
160+
161+ Comparison operators aren't limited to numbers. They work with strings too. The most common use is
162+ checking whether two strings are equal:
163+
164+ ``` python
165+ print (" Alan" == " Alan" )
166+ print (" Alan" != " James" )
167+ ```
168+
169+ Output:
170+
171+ ``` text
172+ True
173+ True
174+ ```
175+
176+ !!! warning
177+
178+ String comparison is case sensitive, so `"alan"` and `"Alan"` are **not** equal:
179+
180+ ``` python
181+ print (" alan" == " Alan" )
182+ ```
183+
184+ Output:
185+
186+ ``` text
187+ False
188+ ```
189+
190+ Python compares strings character by character, so even a single difference, including an extra
191+ space, makes the strings unequal.
192+
193+ ??? note "Using ` < ` and ` > ` with strings"
194+
195+ The other operators work on strings as well. Python compares strings alphabetically, character by
196+ character, so `"apple" < "banana"` is `True` because `"a"` comes before `"b"`.
197+
198+ Be careful with uppercase and lowercase, though. Every uppercase letter is considered "smaller" than
199+ every lowercase letter, so `"Zebra" < "apple"` is `True`.
160200
161201## Arithmetic Operators
162202
0 commit comments