Difference between revisions of "Python - Operators, Expressions"

From Computer Science
Jump to: navigation, search
(Expressions)
(Expressions)
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
 
=Operators=
 
=Operators=
 +
''See [https://youtu.be/XQgHjN9QTIA this video] for a demo using the basic arithmetic operators.''
 +
 
The following are a basic set of operators that most will intuitively know what they do.
 
The following are a basic set of operators that most will intuitively know what they do.
 
* '''Arithmetic operators:''' <code>+ - * / //</code>
 
* '''Arithmetic operators:''' <code>+ - * / //</code>
Line 41: Line 43:
 
** <code>str(2) + str(3)</code>
 
** <code>str(2) + str(3)</code>
 
** <code>2 + 3</code>
 
** <code>2 + 3</code>
 +
* '''Boolean operators''' - note that <code>and</code> is higher precedence than <code>or</code> and that compound Boolean expression should be made out of complete Boolean expressions that are and'ed, or'ed together.  To check whether a variable x is 'a' or 'A', you need <code>if x == 'a' or x == 'A':</code>.  If you tried using <code>if x == 'a' or 'A':</code> this always evaluates to True.
 
* '''Immutability''' - string and tuple variables are immutable, they cannot be changed.  This also means that any string/tuple methods do not change the value of the string but only return a new string/tuple.
 
* '''Immutability''' - string and tuple variables are immutable, they cannot be changed.  This also means that any string/tuple methods do not change the value of the string but only return a new string/tuple.
** <pre>x = 'Hello'
+
x = 'Hello'
x.upper()
+
x.upper()
print(x)
+
print(x)
x = x.upper()
+
x = x.upper()
print(x)</pre>
+
print(x)</code>
 +
 
 +
=Sample Quiz=
 +
''An example quiz over this material.  After watching the video and trying out yourself to make sure you understand.''

Revision as of 12:36, 4 February 2020

See also Python Programming - Getting Started

Following are terse descriptions for Python3 operators. For more information, see w3schools for a bit more explanation and python.org for the language reference.

Operators

See this video for a demo using the basic arithmetic operators.

The following are a basic set of operators that most will intuitively know what they do.

  • Arithmetic operators: + - * / //
    • Note that / is floating point division (3/2 is 1.5), while // is integer division (3//2 is rounded down to 1).
  • Assignment operators: =
  • Comparison operators: < <= == != >= >
    • Note that == tests if two values are equal, != tests if they are not equal (3 != 2 will be True, 3 == 2 will be False).
  • Logical operators: and or not
  • Membership operators: in, not in

More Operators

The following are more operators. These may not be obvious. Some examples are given, but you may need to read through your Python text to understand these.

  • Arithmetic operators: % **
    •  % is remainder (10 % 3 is 1, 17 % 3 is 2), and ** is exponentiation (10**3 is 1000, 2**3 is 8).
  • Assignment operators: += -= *= /= //= %= **= &= |= ^= >>= <<=
    • Each of these is shorthand. For example, x += 3 is a shorthand for x = x + 3
  • Identity operators: is, is not
    • Test whether two objects are the same, not just whether the values are the same. For simple variables/expressions, is will be the same as ==, but for lists/tuples/dictionaries is only gives True if the two things being compared are actually the same data in memory.
  • Bitwise operators: & | ^ ~ << >>
    • These operate on the bits of a number. You need to understand binary before you can understand these. Examples: 12 & 8 evaluates to 8, 12 | 7 evaluates to 15, 12 ^ 8 evaluates to 4, ~7 evaluates to -8 (same as -7-1), 3 << 2 evaluates to 12, 12 >> 1 evaluates to 6.

Expressions

There are many rules to keep in mind for how expressions are evaluated by Python. The following are some of the rules you need to remember, and some examples to put into python to see what they evaluate to

  • Operator precedence - inside of parenthesis first, then exponentiation, then multiplication/division/remainder, then addition/subtraction, then assignment. Full list of operator precedence - at python.org.
    • 1 + 2 * 3
    • 1 + 2 * 9 ** 0.5
    • (1 + 2) * 3
  • 0-based indexing - index into string/list/tuple starts at 0
    • 'hello'[0]
    • (1, 2, 3)[1]
  • Slices - x[i:j+1] evaluates to a subsequence of x starting at index i and ending at index j
    • 'hello'[1:3]
    • [1, 2, 3, 4, 5][2:4]
  • String constants - anything inside of quotes, and + for strings is concatenation
    • 'hi' + ' there'
    • '2' + '3'
    • str(2) + str(3)
    • 2 + 3
  • Boolean operators - note that and is higher precedence than or and that compound Boolean expression should be made out of complete Boolean expressions that are and'ed, or'ed together. To check whether a variable x is 'a' or 'A', you need if x == 'a' or x == 'A':. If you tried using if x == 'a' or 'A': this always evaluates to True.
  • Immutability - string and tuple variables are immutable, they cannot be changed. This also means that any string/tuple methods do not change the value of the string but only return a new string/tuple.
x = 'Hello'
x.upper()
print(x)
x = x.upper()
print(x)

Sample Quiz

An example quiz over this material. After watching the video and trying out yourself to make sure you understand.