Python Concepts Examples
Python Concepts Examples
To execute Python scripts, open the command line and type python or python3 followed by the
python3 my_script.py
This runs the file my_script.py. If you want to start an interactive Python shell, just type python or
2. Tuples in Python
A tuple is an immutable collection in Python, meaning once created, its elements cannot be
Example:
print(my_tuple)
Output:
(1, 2, 3, 'hello')
3. Lambda in Python
A lambda function is a small, anonymous function defined with the lambda keyword. It can have any
Example:
square = lambda x: x * x
print(square(5))
Output:
25
4. Directories in Python
Python provides the os and os.path modules to work with directories. For example, creating a new
directory:
import os
os.mkdir('new_folder')
This will create a folder named 'new_folder' in the current working directory.
Classes are created using the class keyword. Here's an example of defining and using a class:
class Dog:
self.name = name
self.breed = breed
def bark(self):
print(my_dog.bark())
Output:
Buddy says woof!