Python Explanations Extended
Python Explanations Extended
To execute Python code from the command line, you need to have Python installed. You can run a
Python script using the python or python3 command followed by the script name. Example:
python3 script.py
Example:
# script.py
Output:
2. Tuples in Python
A tuple is an immutable collection of elements, typically used to store related data. Tuples can hold
mixed data types and are defined using parentheses (). You cannot change the elements in a tuple
once defined.
print(my_tuple)
Output:
3. Lambda in Python
A lambda function is a small, anonymous function. It can take multiple arguments but only contains
a single expression. Lambda functions are often used where short functions are needed without
square = lambda x: x ** 2
print(square(5))
add = lambda a, b: a + b
print(add(3, 7))
Output:
25
10
A class is a blueprint for objects in Python. You define a class using the class keyword, and classes
class Dog:
self.name = name
self.breed = breed
def bark(self):
print(my_dog.bark())
Output:
Relational Operators: Compare two values. Examples include == (equal), != (not equal), > (greater
than), < (less than), >= (greater than or equal to), <= (less than or equal to).
x = 10
y = 20
# Relational operators
print(x == y) # False
# Logical operators
Output:
False
True
True
True