Skip to main content

Value Manipulation and Conditional Statements | Python Programming By Akash

 1.   Assignment Operators:

Definition: It is a term for the symbols used to assign (give) a value to a variable. The most basic one is the equal sign (=). However, Python also has special 'shorthand' assignment operators that allow us to do math and assign the new result in one single step, which is essential for value manipulation.

List of Common Assignment Operators:

  1. = (Assign): Puts the value on the right into the variable on the left.

§  Example: x = 5

  1. += (Add and Assign): Adds a number to the current value and saves the new total.

§  Example: x += 2 (Same as x = x + 2)

  1. -= (Subtract and Assign): Subtracts a number from the current value and saves it.

§  Example: x -= 2 (Same as x = x - 2)

  1. *= (Multiply and Assign): Multiplies the current value and saves it.

§  Example: x *= 2 (Same as x = x * 2)

  1. /= (Divide and Assign): Divides the current value and saves it.

§  Example: x /= 2 (Same as x = x / 2)

For Example,

2.   Comparison Operators:

Definition: It is a set of operators used to compare two values against each other. They help the computer decide the relationship between two items (like checking if one number is bigger than another). The most important thing to remember is that the result of a comparison operator is always a Boolean value: True or False.

List of Common Comparison Operators:

a.   == (Equal to): Checks if two values are exactly the same.

    • Note: Do not confuse this with = (which assigns data). == checks data.

b.   != (Not Equal to): Checks if two values are different.

c.   > (Greater than): Checks if the left value is bigger.

d.   < (Less than): Checks if the left value is smaller.

e.   >= (Greater than or Equal to): Checks if it is bigger or the same.

f.    <= (Less than or Equal to): Checks if it is smaller or the same.

For Example,

3.   Logical Operators:

Definition: It is a set of keywords used to combine multiple conditions into a single check. They work with Boolean values (True/False) and help us verify if multiple requirements are met simultaneously. The three main keywords are and, or, and not.

The 3 Main Logical Operators:

a.   and (Both must be True):

§  It returns True only if both conditions are correct.

§  Example: True and True = True (Anything else is False).

b.   or (At least one must be True):

§  It returns True if either one of the conditions is correct.

§  Example: True or False = True.

c.   not (Reverse the result):

§  It reverses the result. It makes True into False, and False into True.

§  Example: not True = False.

For Example,

4.   Value Manipulation:

Definition: It is a technique used to modify or update the data stored inside a variable without creating a new variable. We use Assignment Operators (like +=, -=) to change the value directly. This is commonly used in programming to keep track of changing numbers, such as updating a player's score, counting remaining lives, or calculating a total bill.

How it works: Instead of writing score = score + 1, we simply write score += 1. This "manipulates" the existing value in the memory.

For Example,

5.   Conditional Statements (if / else):

Definition: It is a block of code used to make decisions in our program. Just like in real life where we say 'If it rains, I will take an umbrella', in Python we use these statements to tell the computer to run a specific piece of code only if a certain condition is True.

The 3 Main Keywords:

a.   if Statement:

§  This is the first check. The computer asks, "Is this condition True?" If yes, it runs the code inside.

b.   elif (Else If):

§  This is used if the first if failed. It means "Okay, the first one was False, so check this condition instead." You can have many elifs.

c.   else Statement:

§  This is the final backup plan. It means "If none of the above conditions were True, do this." It doesn't need a condition because it catches everything else.

6.   Indentation  Rule:

Definition: It is a strict rule in Python used to organize blocks of code. Unlike other programming languages that use brackets {} to separate sections, Python uses whitespace (4 spaces or tabs) at the beginning of a line. This tells the computer that a specific group of code lines belongs to a 'parent' statement (like an if condition or a function).

For Example, A Rollercoaster Ride Ticket System:

Note: Level 1 & Level 2 are the indentation levels




Comments