0% found this document useful (0 votes)
5 views

Python Concepts Examples

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Concepts Examples

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Concepts and Examples

1. How to execute Python from the command line?

To execute Python scripts, open the command line and type python or python3 followed by the

script name. Example:

python3 my_script.py

This runs the file my_script.py. If you want to start an interactive Python shell, just type python or

python3 and press Enter.

2. Tuples in Python

A tuple is an immutable collection in Python, meaning once created, its elements cannot be

modified. They are defined using parentheses ().

Example:

my_tuple = (1, 2, 3, 'hello')

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

number of arguments, but only one expression.

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.

5. How to create classes in Python

Classes are created using the class keyword. Here's an example of defining and using a class:

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!

You might also like