print("Addition of 58 and 26:", 58+26)
print("Subtract 26 from 58:", 58-26)
print("Multiply 58 and 36:", 58*26)
print("Divide 58 with 16:", 58/16)
print("Divide 58 with 16 and show integer value:", 58//16)
print("Divide 58 with 16 and show reminder value:", 58%16)
print("5 to the power of 3:", 5**3)
def welcome(name):
print(f"Hello, {name} Welcome to Pynfinity!")
welcome("World")
# Integer
age = 25
print("Age:", age)
# String
name = "Santosh"
print("Name:", name)
# Float
height = 5.9
print("Height:", height)
# Boolean
is_student = True
print("Is Student:", is_student)
fruits = ["apple", "banana", "cherry"]
print("List of fruits:", fruits)
# Accessing elements
print("First fruit:", fruits[0])
# Adding items
fruits.append("orange")
print("Updated list:", fruits)
# Removing items
fruits.remove("banana")
print("List after removing 'banana':", fruits)
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
# Loop through a list
for fruit in fruits:
print(fruit)
count = 0
while count < 5:
print("Count is:", count)
count += 1
def add_numbers(a, b):
return a + b
result = add_numbers(10, 20)
print("Result of addition:", result)
person = {
"name": "Santosh",
"age": 30,
"city": "India"
}
print("Person Dictionary:", person)
# Accessing a value
print("Person's name:", person["name"])
# Adding a new key-value pair
person["job"] = "Engineer"
print("Updated Person Dictionary:", person)
text = "Hello, Python!"
# String length
print("Length of string:", len(text))
# Convert to uppercase
print("Uppercase:", text.upper())
# Replace a substring
new_text = text.replace("Python", "World")
print("Replaced text:", new_text)
numbers = [1, 2, 3, 4, 5]
# Using list comprehension to square each number
squares = [num**2 for num in numbers]
print("Squared numbers:", squares)
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("Division successful!")
# A simple lambda function to add two numbers
add = lambda a, b: a + b
result = add(5, 7)
print("Result of addition:", result)
# Writing to a file
with open("sample.txt", "w") as file:
file.write("Hello, this is written with Python.")
# Reading from a file
with open("sample.txt", "r") as file:
content = file.read()
print("File content:", content)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# Creating an object of the Person class
person1 = Person("Santosh", 25)
person1.greet()
# A simple generator function to yield numbers
def count_up_to(max):
count = 1
while count <= max:
yield count
count += 1
# Using the generator
counter = count_up_to(5)
for num in counter:
print(num)
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
import json
# Convert Python dictionary to JSON
person = {"name": "Alice", "age": 30}
person_json = json.dumps(person)
print("JSON representation:", person_json)
# Convert JSON back to Python dictionary
person_dict = json.loads(person_json)
print("Python dictionary:", person_dict)