0% found this document useful (0 votes)
8 views3 pages

Random Python Code 2

The document contains Python code snippets demonstrating exception handling, class definitions, list comprehensions, file operations, and mathematical functions. It includes repeated definitions of a Dog class, functions for checking prime numbers and calculating factorials, as well as writing to a file. Additionally, it showcases error handling for division by zero 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)
8 views3 pages

Random Python Code 2

The document contains Python code snippets demonstrating exception handling, class definitions, list comprehensions, file operations, and mathematical functions. It includes repeated definitions of a Dog class, functions for checking prime numbers and calculating factorials, as well as writing to a file. Additionally, it showcases error handling for division by zero 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

try:

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

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

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

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

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

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

with open('file.txt', 'w') as f:


f.write('Hello, world!')

from datetime import datetime


print(datetime.now())

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

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

from datetime import datetime


print(datetime.now())
import math
print(math.sqrt(16))

with open('file.txt', 'w') as f:


f.write('Hello, world!')

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

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

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

with open('file.txt', 'w') as f:


f.write('Hello, world!')

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

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

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

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

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

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

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

You might also like