Fop Lec 4
Fop Lec 4
Programming
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
Python Operators
► Arithmetic operators
► Assignment operators
► Comparison operators
► Logical operators
► Identity operators
► Membership operators
► Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name
Example
== Equal
x == y
!= Not equal
x != y
> Greater than
x>y
< Less than
x<y
>= Greater than or equal to x >= y
Operator Description
Example
and Returns True if both statements are true x < 5 and x
< 10
or Returns True if one of the statements is true x<5
or x < 4
not Returns False if the result is true
not(x < 5 and x < 10)
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
Example:
a = [1, 2, 3]
b = a # b refers to the same list object as a
c = [1, 2, 3] # c is a different list object with the same content
Example:
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # Output: True
print('orange' in fruits) # Output: False
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and
works more like an iterator method as found in other object-orientated
programming languages.
With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)