0% found this document useful (0 votes)
5 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)
5 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
You are on page 1/ 2

lambda_func = lambda x: x * 2

print(lambda_func(10))

import math
print(math.sqrt(16))

class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f'{self.name} 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):
self.name = name
def bark(self):
print(f'{self.name} 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('file.txt', 'w') as f:
f.write('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(datetime.now())

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

from datetime import datetime


print(datetime.now())

import math
print(math.sqrt(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('file.txt', 'w') as f:


f.write('Hello, world!')

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy