1.
Executing Python from the Command Line
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
print("Hello from Python!")
Output:
Hello from Python!
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.
my_tuple = (42, 'apple', 3.14)
print(my_tuple)
print(my_tuple[1]) # Accessing the second element
Output:
(42, 'apple', 3.14)
apple
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
explicitly defining them.
square = lambda x: x ** 2
print(square(5))
add = lambda a, b: a + b
print(add(3, 7))
Output:
25
10
4. Creating Classes in Python
A class is a blueprint for objects in Python. You define a class using the class keyword, and classes
can contain attributes (variables) and methods (functions).
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
return f"{self.name} says woof!"
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.bark())
Output:
Buddy says woof!
5. Relational and Logical Operators
Relational Operators: Compare two values. Examples include == (equal), != (not equal), > (greater
than), < (less than), >= (greater than or equal to), <= (less than or equal to).
Logical Operators: Used to combine multiple conditions: and, or, not.
x = 10
y = 20
# Relational operators
print(x == y) # False
print(x < y) # True
# Logical operators
print(x < y and x != y) # True
print(not (x == y)) # True
Output:
False
True
True
True