Wk2_material - Python Programming (Advanced Concepts)
Wk2_material - Python Programming (Advanced Concepts)
1
• Assignment operation
• Membership operation
• Identity operation
Comparison (Relational) Operations These operations are used to compare the value of two
operands:
• Equal to (==): True if both operands are equal.
• Not equal to (!=): True if operands are not equal.
• Greater than (>): True if the left operand is greater than the right operand.
• Less than (<): True if the left operand is less than the right operand.
• Greater than or equal to (>=): True if the left operand is greater than or equal to the right
operand.
• Less than or equal to (<=): True if the left operand is less than or equal to the right operands.
The comparison operate compares two values and gives either TRUE or FALSE as the output.
Identity Operations These operations compare the memory locations of two objects:
• Is (is): True if both operands refer to the same object.
• Is not (is not): True if operands refer to different objects.
Each type of operation enables specific manipulations and checks within Python code, making it
versatile for a wide range of programming tasks from basic arithmetic to complex conditional logic.
2
0.2.1 If Statement
The if statement is the simplest form of a conditional statement. It evaluates a condition, and if
the condition is True, the indented block of code under it is executed.
x = 10
if x > 5:
print("x is greater than 5")
3
line.
x = 5
result = "Greater than 2" if x > 2 else "Not greater than 2"
print(result)
4
Syntax:
while condition:
# Block of code
Example:
i = 0
while i < 5:
print(i)
i += 1
0.4 FUNCTIONS
Functions in Python are defined blocks of reusable code designed to perform a specific task or a set
of related tasks. They are fundamental to Python programming, enabling modular, organized, and
efficient code development. By defining functions, programmers can break down complex processes
into manageable pieces, enhance code readability, and avoid repetition.
In Python, a function consists of several parts:
1. Function Name: This is the identifier for the function, which is used to call it later in the
code. It should be descriptive of what the function does, following the naming conventions
in Python (usually lowercase with words separated by underscores).
2. Parameters (or Arguments): These are optional. Parameters are the input values that
you pass into the function. A function may have zero or more parameters. They are specified
within parentheses following the function name.
5
3. Function Body: This is the block of code that performs the specific task of the function.
It’s indented and begins after the colon following the function definition.
4. Return Statement (Optional): This is used to return a value from the function back to the
caller. Not all functions need to return a value, in which case they can omit this statement.
If present, the return statement typically comes at the end of the function body.
Here’s a simple example:
def greet(name):
return "Hello, " + name + "!"
# Function call
print(greet("Alice"))
In this example: - greet is the function name. - name is the parameter. - "Hello, " + name +
"!" is the function body. - return "Hello, " + name + "!" is the return statement.
When you call greet("Alice"), "Alice" is passed as the argument to the name parameter, and
the function returns "Hello, Alice!", which is then printed.
Additionally, you might see documentation strings (docstrings) used to describe the purpose and
usage of a function. These are optional, but they are helpful for documenting your code. They
appear as a string literal as the first statement in a function body.
result = add(5, 3) # 8
6
0.4.4 Return Values
Functions can return values using the return statement. A function stops executing when it
encounters a return statement, and the value specified is returned to the caller.
def multiply(x, y):
return x * y
7
0.7 Attributes and Methods for the various Data Types in Python
0.7.1 Integer (int)
• bit_length: Returns the number of bits necessary to represent the integer in binary, exclud-
ing the sign and leading zeros.
8
• items(): Returns a view of the dictionary’s key-value pairs.
• get(key[, default]): Returns the value for the specified key. If the key is not found,
returns the default value or None if not specified.
• pop(key[, default]): Removes and returns the value for the specified key. If the key is not
found and a default value is provided, returns the default value; otherwise, raises a KeyError.
• popitem(): Removes and returns an arbitrary key-value pair from the dictionary.