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

Random Python Code 1

The document contains various Python code snippets demonstrating functions, classes, and error handling. Key features include a prime-checking function, a Dog class with a bark method, list comprehensions for squaring numbers, and file writing. Additionally, there are examples of using the math library and handling exceptions.

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 1

The document contains various Python code snippets demonstrating functions, classes, and error handling. Key features include a prime-checking function, a Dog class with a bark method, list comprehensions for squaring numbers, and file writing. Additionally, there are examples of using the math library and handling exceptions.

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/ 3

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

import math
print(math.sqrt(16))

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!')

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

from datetime import datetime


print(datetime.now())

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

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

import math
print(math.sqrt(16))

from datetime import datetime


print(datetime.now())

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

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

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

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

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())

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)

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

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!')

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!')

You might also like