PYTHON NOTES
PYTHON NOTES
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
4. You have to use the same number of spaces in the same block of
code, otherwise Python will give you an error
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Multiline Comments
Python does not really have a syntax for multiline comments.
Since Python will ignore string literals that are not assigned to a variable, you
can add a multiline string (triple quotes) in your code, and place your comment
inside it:
Example: """
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
Variables are containers for storing data values. Remember that variable
names are case-sensitive.
Creating Variables
A variable is created the moment you first assign a value to it.
Example: x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even
change type after they have been set.
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example: x = 5
y = "John"
print(type(x))
print(type(y))
Example: x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to
extract the values into variables. This is called unpacking.
Random Number
Python does not have a random() function to make a random number, but
Python has a built-in module called random that can be used to make random
numbers:
print(random.randrange(1, 10))
Python Operators
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Assignment Operators
Assignment operators are used to assign values to variables:
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
**= x **= 3 x = x ** 3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
:= print(x := 3) x=3
print(x)
Comparison Operators
Comparison operators are used to compare two values:
== Equal x == y
!= Not equal x != y
Logical Operators
Logical operators are used to combine conditional statements:
not Reverse the result, returns False if not(x < 5 and x < 10)
the result is true
Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
<< Zero fill left Shift left by pushing zeros in from x << 2
shift the right and let the leftmost bits
fall off
>> Signed right Shift right by pushing copies of the x >> 2
shift leftmost bit in from the left, and let
the rightmost bits fall off
Operator Precedence
Operator precedence describes the order in which operations are performed.
If two operators have the same precedence, the expression is evaluated from
left to right.
The precedence order is described in the table below, starting with the highest
precedence at the top:
Operator Description
() Parentheses
** Exponentiation
^ Bitwise XOR
| Bitwise OR
is not in not in