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

Types of Functions

Function
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Types of Functions

Function
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Types of functions

November 22, 2023

[1]: # Using all() with a list

numbers = [1, 3, 5, 7, 9]
# Check if all numbers in the list are odd
result = all(x % 2 != 0 for x in numbers)
print(result)

True

[2]: # Using any() with strings


words = ['apple', 'banana', 'cherry']

# Check if at least one word in the list starts with 'a'


result = any(word.startswith('a') for word in words)

print(result)

True

[3]: # str()
float_number = 3.14
string_float = str(float_number)
print(string_float)

3.14

[4]: # Using chr() to convert ASCII code to character


ascii_code = 65 # ASCII code for 'A'
character = chr(ascii_code)
print(character)

[9]: # Create a bytearray from a sequence of integers


byte_array = bytearray([65, 66, 67, 68]) # ASCII values for 'A', 'B', 'C', 'D'

# Display the initial bytearray


print("Initial bytearray:", byte_array)

# Modify an element in the bytearray

1
byte_array[1] = 70 # ASCII value for 'F'

# Display the modified bytearray


print("Modified bytearray:", byte_array)

Initial bytearray: bytearray(b'ABCD')


Modified bytearray: bytearray(b'AFCD')

[11]: # Callable()
def my_function():
print("Hello, callable!")

# Check if the object is callable


result = callable(my_function)

# Display the result


print("Is my_function callable?", result)

Is my_function callable? True

[12]: # classmethod()
class MyClass:
class_variable = "I am a class variable"

def __init__(self, instance_variable):


self.instance_variable = instance_variable

@classmethod
def class_method(cls):
print("This is a class method")
print("Accessing class variable:", cls.class_variable)

obj = MyClass("I am an instance variable")

MyClass.class_method()

obj.class_method()

This is a class method


Accessing class variable: I am a class variable
This is a class method
Accessing class variable: I am a class variable

[14]: # complex()

# Creating a complex number with real and imaginary parts


complex_num = complex(2, 3)

# Displaying the complex number

2
print("Complex Number:", complex_num)

# Accessing the real and imaginary parts


real_part = complex_num.real
imaginary_part = complex_num.imag

# Displaying the real and imaginary parts separately


print("Real Part:", real_part)
print("Imaginary Part:", imaginary_part)

Complex Number: (2+3j)


Real Part: 2.0
Imaginary Part: 3.0

[16]: # delattr()

class Car:
def __init__(self, make, model):
self.make = make
self.model = model

# Create an instance of the Car class


my_car = Car("Toyota", "Camry")

# Display attributes before deletion


print("Before Deletion - Make:", my_car.make)
print("Before Deletion - Model:", my_car.model)

# Delete the 'model' attribute using delattr()


delattr(my_car, 'model')

# Display attributes after deletion


# Accessing my_car.model would result in an AttributeError at this point
print("After Deletion - Make:", my_car.make)

# Attempting to access the deleted attribute would raise an AttributeError


# print("After Deletion - Model:", my_car.model) # Uncommenting this line␣
↪would result in an AttributeError

Before Deletion - Make: Toyota


Before Deletion - Model: Camry
After Deletion - Make: Toyota

[17]: # Using filter with a function


def is_even(x):
return x % 2 == 0

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3
even_numbers = filter(is_even, numbers)

print("Original Numbers:", numbers)


print("Even Numbers:", list(even_numbers))

Original Numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


Even Numbers: [2, 4, 6, 8, 10]

[18]: # Creating a frozenset from a list


my_list = [1, 2, 3, 4, 5]
frozen_set1 = frozenset(my_list)
print(frozen_set1)

frozenset({1, 2, 3, 4, 5})

[19]: # Using memoryview with bytes


my_bytes = b"Hello, World!"

# Create a memory view object


mv = memoryview(my_bytes)

# Accessing the underlying buffer using slicing


print(mv[7]) # Output: 87 (ASCII code for 'W')
print(mv[0:5]) # Output: b'Hello'

87
<memory at 0x0000022A5BCB8280>

[20]: # property()

class Circle:
def __init__(self, radius):
self._radius = radius # Use a private attribute _radius

@property
def radius(self):
return self._radius

@property
def diameter(self):
return 2 * self._radius

@property
def area(self):
return 3.14 * self._radius ** 2

# Create an instance of Circle


my_circle = Circle(radius=5)

4
# Access properties
print("Radius:", my_circle.radius)
print("Diameter:", my_circle.diameter)
print("Area:", my_circle.area)

Radius: 5
Diameter: 10
Area: 78.5

[22]: # raw_input()

user_input = input("Enter something: ")


print("You entered:", user_input)

Enter something: python


You entered: python

[23]: # repr()

x = 42
representation = repr(x)

print("Original object:", x)
print("String representation:", representation)

Original object: 42
String representation: 42

[24]: # staticmethod()

class MathOperations:
@staticmethod
def add(x, y):
return x + y

@staticmethod
def multiply(x, y):
return x * y

# Calling static methods on the class


result_sum = MathOperations.add(5, 3)
result_product = MathOperations.multiply(4, 6)

print("Sum:", result_sum)
print("Product:", result_product)

Sum: 8
Product: 24

5
[ ]:

You might also like