1.3. Operators#

1.3.1. Arithmetic Operators#

Math sign

Python sign

name

+

+

addition

-

-

subtraction

*

*

multiplication

/

/

division

^

**

exponentiation

mod

%

modulus

//

floor division

Arithmetic operators: These operators perform mathematical operations, such as addition, subtraction, multiplication, and division. Examples:

x = 5
y = 2

print(x + y) # Output: 7 
print(x - y) # Output: 3
print(x * y) # Output: 10
print(x / y) # Output: 2.5
print(x**y)  # output: 25
7
3
10
2.5
25

1.3.2. Error codes in python#

When you run a Python script or code in a cell, the code is executed line by line, starting from the first line and moving down the code.

If an error occurs, Python will stop executing the code at the line where the error occurs and will display an error message. The first line of the error will indicating the line number where the error occurred. This is often the most informative as it tells you where in your code the problem is. The last line in the error message will tell you what the problem in this line is.

The code results in a TypeError because you are trying to add a variable of type integer (a) to a variable of type string (b). In python, you can only add two variables of the same type. You can’t add an int to a string. If you want to concatenate the string and the int you can convert the int to string before adding them together.

Here is an example:

a = 0
b = "hello"
c = str(a) + b

print (c)
0hello

Or you can use the format() method to insert the value of a into the string b.

a = 5
b = "hello {}"
c = b.format(a)

print (c)
hello 5

Or you can use f-strings (formatted string literals) that are available from python 3.6 and above. You can show a nummerical output with any string you want using f-strings. The code you need to type for f-strings: f ‘ text {output} ‘. The output has to be inside the curly brackets –> { }

a = 5
b = "hello"
c = f"{b} {a}"

print (c)
hello 5

1.3.3. Comparison Operators


#

In Python, you often want to compare a value with another. For that, you use comparison operators.

Math sign

Python sign

Meaning

\(=\)

\(==\)

Equal to

\(>\)

\(>\)

Greater than

\(<\)

\(<\)

Less than

\(\geqslant\)

\(>=\)

Greater than or equal to

\(\leqslant\)

\(<=\)

Less than or equal to

\(\neq\)

\(!=\)

Not equal to

Comparison operators: These operators compare two values and return a Boolean value (True or False). Examples:

x = 5
y = 2
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False
False
True
True
False
True
False

1.3.4. Control flow statements in Python#

There are several control flow statements in Python that are used to control the flow of execution of a program. The most important ones are:

if statement: The if statement is used to check a certain condition, and if the condition is true, the code within the if block will be executed. If the condition is false, the code within the if block will be skipped. For example:

x = 5
if x > 0:
    print("x is positive")
x is positive

if-else statement: The if-else statement is an extension of the if statement, which allows you to specify a block of code to be executed if the condition is true, and a different block of code to be executed if the condition is false. Example:

x = -2
if x > 0:
    print("x is positive")
else:
    print("x is non-positive")
x is non-positive

if-elif-else statement: The if-elif-else statement is an extension of the if-else statement, which allows you to check multiple conditions and execute different code blocks based on the first condition that is true. This is how you use them:

x = 0
if x > 0:
    print("x is positive")
elif x == 0:
    print("x is zero")
else:
    print("x is negative")
    

x2 = -2
if x2 > 0:
    print("x2 is positive")
elif x2 == 0:
    print("x2 is zero")
else:
    print("x2 is negative")
x is zero
x2 is negative

1.3.5. Indexing#

Indexing is a method in Python to access individual elements in a list by their position. This is a fundamental feature of Python’s list data structure, allowing you to retrieve specific elements from the list. Elements are stored in a sequential manner and can be accessed using their index (integer value indicating their position).