Python Theory
Python Theory
Variables
A variable in a program is uniquely identified by a name (identifier). Variable in Python refers to an object — an item or
element that is stored in the memory. Value of a variable can be a string (e.g., ‘b’, ‘Global Citizen’), numeric (e.g., 345)
or any combination of alphanumeric characters (CD67). In Python we can use an assignment (the = sign is the
assignment operator) statement to create new variables and assign specific values to them.
Data Types
Every value belongs to a specific data type in Python. Data type identifies the type of data values a variable can hold
and the operations that can be performed on that data
Operators :
An operator is a character or set of characters that can be used to perform the desired operation on the
operands and produce the final result.
The final result completely depends on the type of operators used.
For example, consider you want to perform a mathematical calculation of 5+3. Now both 5 and 3 are
called operands, + is the operator that performs addition and the final result is 8.
The different types of operators supported by Python are as follows (Important)
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
Arithmetic Operators (7 operators)
Arithmetic operators are used in performing mathematical operations such as Addition, Subtraction,
Multiplication, Division, Modulus,
> Greater than: True if the left operand is greater than the right x>y
< Less than: True if the left operand is less than the right x<y
<= Less than or equal to: True if left operand is less than or equal to the right x <= y
Assignment Operators
Assignment operator assigns or changes the value of the variable on its left.
Example : =
Logical Operators
There are three logical operators supported by Python. These operators (and, or, not) are to be written in lower case
only. The logical operator evaluates to either True or False based on the logical operands on either side. Every value
is logically either True or False. By default, all values are True except None, False, 0 (zero), empty collections "", (), [],
{}, and few other special values. So if we say num1 = 10, num2 = -20, then both num1 and num2 are logically True.
1. **Division (/):**
- The `/` operator is used for division, and it returns the quotient of the division operations.
2. **Modulus (%):**
- The `%` operator is the modulus operator, and it returns the remainder of the division
operation.
In summary:
- `/` is the division operator that gives the quotient of a division operation.
- `%` is the modulus operator that gives the remainder of a division operation.
In this example, x == y checks whether the values of x and y are equal. Since
x is 10 and y is 5, the result is False.
● What it is: A syntax error occurs when you don't follow the proper structure or
grammar rules of the programming language.
● Why it happens: It's like a language grammar mistake. The code doesn't make
sense to Python because you've written it in a way it doesn't understand.
● Example: Forgetting to close a parenthesis, using a wrong keyword, or
misspelling a built-in function.
● Result: The code won't run at all, and you'll see an error message before the
program starts.
Logical Error:
● What it is: A logical error occurs when the code is syntactically correct, but it
doesn't produce the expected result due to a mistake in the logic or algorithm.
● Why it happens: It's like a flaw in your plan. The code runs, but it doesn't do
what you intended it to do.
● Example: Adding instead of subtracting, using the wrong variable, or not
considering a specific condition in your code.
● Result: The code runs, but it doesn't produce the right output, and you might
not get an error message. Fixing logical errors involves rethinking and
adjusting your program.