Welcome to the first lesson of the Python programming language course module. In this lesson, we will learn about the basic concept of Python as a Programming language and the fundamental concepts in its mathematical and arithmetic operations.
Core Concept of Python
Python is an interpreted, high-level, general-purpose programming language. It has a simple syntax that is easy to learn and read, making it a popular choice for beginners.
One of the key features of Python is its support for mathematical and arithmetic operations, which allows programmers to perform calculations and manipulate numerical data.
Variables
Variables are used to store data in Python. A variable is created when you assign a value to it using the equal sign (=). For example, the following code creates a variable called "x" and assigns it the value of 10:
x = 10
Data Types
Python has several data types, including integers, floats, strings, and booleans.
Integers are whole numbers without a decimal point, and floats are numbers with a decimal point. Strings are sequences of characters enclosed in quotation marks, and booleans are either True or False.
Here are some examples of assigning values to variables with different data types:
# Integer
x = 10
# Float
y = 3.14
# String
name = "John"
# Boolean
is_true = True
Basic Mathematical Operations
In Python, several basic mathematical operations can be performed, including addition, subtraction, multiplication, and division. Let's take a closer look at each of these operations:
- Addition: The addition operator (+) is used to add two or more numbers together. For example, the following code adds two numbers and stores the result in a variable:
x = 5
y = 4
z = x + y
print(z) # Output: 9
- Subtraction: The subtraction operator (-) is used to subtract one number from another. For example, the following code subtracts one number from another and stores the result in a variable:
x = 5
y = 4
z = x - y
print(z) # Output: 1
- Multiplication: The multiplication operator (*) is used to multiply two or more numbers together. For example, the following code multiplies two numbers and stores the result in a variable:
x = 5
y = 4
z = x * y
print(z) # Output: 20
- Division: The division operator (/) is used to divide one number by another. For example, the following code divides one number by another and stores the result in a variable:
x = 5
y = 4
z = x / y
print(z) # Output: 1.25
In Python 3, the division operator (/) always returns a float. If you want to perform integer division, you can use the double forward slash operator (//). This is also known as the floor division.
Eg:
# Integer division
x = 10
y = 3
z = x // y
print(z) # Output: 3
The modulo operator (%) returns the remainder of a division operation. For example:
x = 10
y = 3
z = x % y
print(z) # Output: 1
x = 2
y = 3
z = x ** y
print(z) # Output: 8
- Parentheses
- Exponentiation
- Multiplication and Division (performed left to right)
- Addition and Subtraction (performed left to right)
For example:
x = 10
y = 5
z = (x + y) * 2
print(z) # Output: 30
In this example, the expression in parentheses is evaluated first, then the multiplication is performed.
In-place operator
In Python, an in-place operator is a shorthand way of performing an arithmetic or bitwise operation on a variable and updating its value in-place. In other words, the result of the operation is stored back into the same variable without needing to create a new variable.
Here's an example of using an in-place operator to update the value of a variable:
x = 5
x += 3
print(x)
In this example, the += operator is an in-place operator that adds 3 to the value of x and stores the result back into x. The output of this code will be 8, which is the updated value of x.
Python supports several in-place operators for performing arithmetic and bitwise operations, including:
- += for addition
- -= for subtraction
- *= for multiplication
- /= for division
- //= for integer division
- %= for modulus (remainder)
- **= for exponentiation
- &= for bitwise AND
- |= for bitwise OR
- ^= for bitwise XOR
- <<= for bitwise left shift
- >>= for bitwise right shift
Using in-place operators can be a more concise and readable way of updating variable values in Python, especially when working with loops or other situations where you need to perform a repetitive operation on a variable.
The Walrus Operator
The Walrus operator, also known as the assignment expression, is a new feature introduced in Python 3.8 that allows you to assign values to variables as part of an expression. It is represented by the := symbol.
Here's an example of using the Walrus operator:
while (name := input("Enter your name: ")) != "quit":
print("Hello,", name)
In this example, the Walrus operator is used to assign the value of the input() function to the name variable and also check if it's not equal to "quit" as part of the while loop condition.
This allows you to perform the assignment and comparison in a single line of code.
The Walrus operator can also be used in other contexts where you want to perform an assignment as part of an expression. For example:
if (count := len(my_list)) > 0:
print("The list contains", count, "elements.")
In this example, the Walrus operator is used to assign the length of my_list to the count variable and also check if it's greater than 0 as part of the if statement condition.
The Walrus operator can be a useful tool for writing more concise and readable code in certain situations. However, it should be used judiciously and with care, as overuse or misuse of the operator can make the code harder to read and understand.
Conclusion
In this lesson, we have discussed the core concepts of Python, with a focus on performing mathematical and arithmetic operations.
We have covered variables, data types, arithmetic operators, the modulo operator, the exponentiation operator, and the order of operations.
Note: You do not have to worry much about some keywords used in this lesson which we have not discussed yet. You will meet them and get to understand them in our upcoming lessons.
Understanding these concepts is essential for performing mathematical and arithmetic operations in Python, and it lays the foundation for more complex programming