Fundamentals of
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
Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
► 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
+ Addition x
+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x
%y
** Exponentiation x ** y
// Floor division x // y
Python Assignment Operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
Python Comparison Operators
Comparison operators are used to compare two values:
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
<= Less than or equal to x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
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:
Operator Description Example
is Returns True if both variables are the same object
x is y
is not Returns True if both variables are not the same object x is not y
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
print(a is b) # Output: True (a and b refer to the same object)
print(a is c) # Output: False (a and c refer to different objects)
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description Example
in Returns True if a sequence with the x in y
specified value is present in the object
not in Returns True if a sequence with the x not in y
specified value is not present in the object
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:
Operator Name Description Example
& AND Sets each bit to 1 if both bits are 1
x&y
| OR Sets each bit to 1 if one of two bits is 1
x|y
^ XOR Sets each bit to 1 if only one of two bits is 1
x^y
~ NOT Inverts all the bits
~x
<< Zero fill left shift Shift left by pushing zeros in from the right x << 2
and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost
x >> 2
bit in from the left, and let the rightmost
bits fall off
Operator Precedence
EXAMPLES
Parentheses has the highest precedence, meaning that expressions
inside parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
Multiplication * has higher precedence than addition +,
and therefor multiplications are evaluated before additions:
print(100 + 5 * 3)
Addition + and subtraction - has the same precedence,
and therefor we evaluate the expression from left to right:
print(5 + 4 - 7 + 3)
Python If ... Else
Python supports the usual logical conditions from mathematics:
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
These conditions can be used in several ways, most commonly in "if
statements" and loops.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python For Loops
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)