0% found this document useful (0 votes)
9 views2 pages

Random Python Code 3

The document contains various Python code snippets demonstrating the use of lambda functions, classes, exception handling, and mathematical functions. It includes implementations of a Dog class, factorial and prime-checking functions, and file writing. Additionally, it showcases list comprehensions and the use of the datetime module.

Uploaded by

dineshrmsingh
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)
9 views2 pages

Random Python Code 3

The document contains various Python code snippets demonstrating the use of lambda functions, classes, exception handling, and mathematical functions. It includes implementations of a Dog class, factorial and prime-checking functions, and file writing. Additionally, it showcases list comprehensions and the use of the datetime module.

Uploaded by

dineshrmsingh
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

lambda_func = lambda x: x * 2

print(lambda_func(10))

import math
print([Link](16))

class Dog:
def __init__(self, name):
[Link] = name
def bark(self):
print(f'{[Link]} says woof!')

def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)

class Dog:
def __init__(self, name):
[Link] = name
def bark(self):
print(f'{[Link]} says woof!')

try:
x = 1 / 0
except ZeroDivisionError:
print('Cannot divide by zero')

try:
x = 1 / 0
except ZeroDivisionError:
print('Cannot divide by zero')

try:
x = 1 / 0
except ZeroDivisionError:
print('Cannot divide by zero')

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
with open('[Link]', 'w') as f:
[Link]('Hello, world!')

for i in range(10):
print(f'Number: {i}')

def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)

lambda_func = lambda x: x * 2
print(lambda_func(10))

from datetime import datetime


print([Link]())

for i in range(10):
print(f'Number: {i}')

from datetime import datetime


print([Link]())

import math
print([Link](16))

lambda_func = lambda x: x * 2
print(lambda_func(10))

def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)

lambda_func = lambda x: x * 2
print(lambda_func(10))

data = [1, 2, 3, 4, 5]
squares = [x**2 for x in data]
print(squares)

with open('[Link]', 'w') as f:


[Link]('Hello, world!')

def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)

You might also like