Introduction#
The logic of programming comes from the operators here we will see the built-in python operators that are the building blocks for more complex codes.
Operators#
The most important functionality is the ability to perform mathematical operations. Python covers the most basic arithmetic operations.
Arithmetic operator#
1
2
3
4
5
6
| a = 3
b =5
print(a+b)
print(a*b)
print(a-b)
print(a/b)
|
Output:Another operator available is the modulo (%) operator, which returns the integer remainder of the division:
a % b = d
Output:Using two multiplication symbols makes a power relationship.
Output:Comparison and logic operators#
In order to work with iterable there is the need to look a specific value, the comparison operator help with it, they return Booleans (True or False).
| Operator | Name |
|---|
| == | Equal |
| != | Not equal |
| > | Greater than |
| < | Less than x < y |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| and | Returns True if both statements are true |
| or | Returns True if one of the statements is true |
| not | Reverse the result, returns False if the result is true |
| is | Returns True if both variables are the same object |
| is not | Returns True if both variables are not the same object |
| in | Returns True if a sequence with the specified value is present in the object |
| not in | Returns True if a sequence with the specified value is not present in the object |
Comparison#
1
2
3
4
5
6
7
8
9
| x = 5
y = 3
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)
|
Output:1
2
3
4
5
6
| False
True
True
False
True
False
|
Logic#
1
2
3
4
5
6
| x = 5
y = 3
print(x > 0 and x < 10)
print(x < 5 or x > 10)
print(not(x > 3 and x < 10))
|
Output:Identity#
1
2
3
4
5
6
| x = 5
y = 3
print(x > 0 and x < 10)
print(x < 5 or x > 10)
print(not(x > 3 and x < 10))
|
Output:1
2
3
4
5
6
7
8
| x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
print(x is y)
print(x == y)
print(x is not y)
|
Output:1
2
3
4
| True
False
True
True
|
- is - Checks if both variables point to the same object in memory
- == - Checks if the values of both variables are equal
Membership#
1
2
3
4
| fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)
print("pineapple" not in fruits)
|
Output:List operation#
the operators do not work in the same way in list, for example:
1
2
3
4
5
| a = [5, 3, 2, 8]
b = [6, 9, 2, 3]
print(a+b)
print(4*a)
|
Output:1
2
| [5, 3, 2, 8, 6, 9, 2, 3]
[5, 3, 2, 8, 5, 3, 2, 8, 5, 3, 2, 8, 5, 3, 2, 8,]
|
String#
The strings are naturally lists, because they are a list of characters, the list operation will work in the same way as it does for the list.
1
2
3
4
5
| a = "hello"
b = "World"
print(a + " " + b)
print(4*a)
|
Output:1
2
| hello world
hellohellohellohellohello
|
It is important to notice that the operation must be realized with the same data type. It will not work if you try to add a boolean with a float, although it will work between float and integer.
The strings have two types of format structure: the raw strings (r-string) are strings that does not admit the modifiers, the backslash is treated as literal character; the formatted strings (f-string) allow embedded expressions directly inside literals using braces or argument specifiers.
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.“number of digits"f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
1
2
3
| name = "John"
age = 23
print(f"%s is %d years old." % (name, age))
|
Output:1
2
| print("C:\new\test")
print(r"C:\new\test")
|
Output:1
2
3
| C:
ew est
C:\new\test
|