Python_Java Script (2)
Python_Java Script (2)
Integer (“int”)
An integer represents whole numbers, positive or negative, without any decimal points.
# Example of an integer
my_integer = 42
print(my_integer)
# Output: 42
# Example of a string
my_string = "Hello, Python!"
print(my_string)
# Output: Hello, Python!
04. List
Lists are ordered collections of items that are mutable (can be changed).
# Example of a list
my_list = [1, 2, 3, 'a', 'b', 'c']
print(my_list)
# Output: [1, 2, 3, 'a', 'b', 'c']
05. Tuple
Tuples are ordered collections similar to lists, but they are immutable (cannot be changed).
# Example of a tuple
my_tuple = (4, 5, 'x', 'y')
print(my_tuple)
# Output: (4, 5, 'x', 'y')
# Example of a dictionary
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict)
# Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}
07. Boolean (“bool”)
Booleans represent truth values, either True or False.
# Example of a boolean
my_boolean = True
print(my_boolean)
# Output: True
08. Set
Sets are unordered collections of unique elements.
# Example of a set
my_set = {1, 2, 3, 4, 4, 3, 2} # Note: Duplicates are automatically removed in a set
print(my_set)
# Output: {1, 2, 3, 4}
# Example of NoneType
my_none = None
print(my_none)
# Output: None
These are some of the fundamental data types in Python. Each type serves its purpose in different
scenarios and allows for flexible and versatile programming.
Output:
Integer data type: 42
Float data type: 3.14
String data type: Hello, Python!
List data type: [1, 2, 3, 'a', 'b', 'c']
Tuple data type: (4, 5, 'x', 'y')
Dictionary data type: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Boolean data type: True
Set data type: {1, 2, 3, 4}
NoneType data type: None
Output:
Enter the base: 10
Enter the height: 5
The area of the triangle is 25.00
def calculate_triangle_area(base, height):
# Area of a triangle formula: area = 0.5 * base * height
area = 0.5 * base * height
return area
Output:
The area of the triangle with base 8 and height 5 is: 20.0
<!DOCTYPE html>
<html>
<head>
<title>Triangle Area Calculator</title>
</head>
<body bgcolor=yellow>
<h1>Triangle Area Calculator</h1>
<p id="result"></p>
<script>
function calculateArea() {
// Get base and height values from user input
var base = parseFloat(document.getElementById('base').value);
var height = parseFloat(document.getElementById('height').value);
# Addition
addition = num1 + num2
print(f"Addition: {num1} + {num2} = {addition}")
# Subtraction
subtraction = num1 - num2
print(f"Subtraction: {num1} - {num2} = {subtraction}")
# Multiplication
multiplication = num1 * num2
print(f"Multiplication: {num1} * {num2} = {multiplication}")
# Division
division = num1 / num2
print(f"Division: {num1} / {num2} = {division}")
# Modulo (Remainder)
modulo = num1 % num2
print(f"Modulo: {num1} % {num2} = {modulo}")
Output:
Addition: 20 + 7 = 27
Subtraction: 20 - 7 = 13
Multiplication: 20 * 7 = 140
Division: 20 / 7 = 2.857142857142857
Modulo: 20 % 7 = 6
# Function to perform arithmetic operations
def perform_arithmetic_operations(num1, num2):
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
modulo = num1 % num2
Output:
Enter the first number: 21
Enter the second number: 11
Addition: 21.0 + 11.0 = 32.0
Subtraction: 21.0 - 11.0 = 10.0
Multiplication: 21.0 * 11.0 = 231.0
Division: 21.0 / 11.0 = 1.9090909090909092
Modulo: 21.0 % 11.0 = 10.0
Output:
Enter first number: 20
Enter second number: 10
The sum of 20 and 10 is 30.0
The subtraction of 20 and 10 is 10.0
The multiplication of 20 and 10 is 200.0
The division of 20 and 10 is 2.0
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body bgcolor= cyan>
<h1>Arithmetic Operations</h1>
<div id="results"></div>
<script>
function performOperations()
{
var num1 = parseFloat(document.getElementById('num1').value);
var num2 = parseFloat(document.getElementById('num2').value);
print("Before swapping:")
print("variable1 =", variable1)
print("variable2 =", variable2)
print("\nAfter swapping:")
print("variable1 =", variable1)
print("variable2 =", variable2)
Output:
Before swapping:
variable1 = 5
variable2 = 10
After swapping:
variable1 = 10
variable2 = 5
This Python program demonstrates how to swap the values of two variables (variable1 and
variable2). It uses a third variable temp to temporarily hold one of the values during
the swapping process. After the values are interchanged, the new values of the variables
are printed to display the swapping results.
# Accepting input from the user
variable1 = input("Enter the value for variable 1: ")
variable2 = input("Enter the value for variable 2: ")
print("\nBefore swapping:")
print("variable1 =", variable1)
print("variable2 =", variable2)
print("\nAfter swapping:")
print("variable1 =", variable1)
print("variable2 =", variable2)
Output:
Enter the value for variable 1: 45
Enter the value for variable 2: 90
Before swapping:
variable1 = 45
variable2 = 90
After swapping:
variable1 = 90
variable2 = 45
<!DOCTYPE html>
<html>
<head>
<title>Variable Swapping</title>
</head>
<body bgcolor=orange>
<h1>Variable Swapping</h1>
<p>Before Swapping:</p>
<p id="beforeSwap"></p>
<p>After Swapping:</p>
<p id="afterSwap"></p>
<script>
function swapVariables() {
var var1 = document.getElementById('variable1').value;
var var2 = document.getElementById('variable2').value;
Output:
This HTML file includes a simple form where users can input values for two variables.
Upon clicking the "Swap Variables" button, the swapVariables JavaScript function is
triggered. This function retrieves the values entered by the user and then swaps the
variables using destructuring assignment. The values before and after swapping are
displayed in the HTML elements with the IDs beforeSwap and afterSwap, respectively.
Odd and Even numbers:
If you divide a number by 2 and it gives a remainder of 0 then it is known as even
number, otherwise an odd number.
Even number examples: 2, 4, 6, 8, 10, etc.
Odd number examples: 1, 3, 5, 7, 9 etc.
Output:
Enter a number: 10
10 is Even number
Enter a number: 11
11 is Odd number
In this program:
The user is prompted to input a number.
The program checks if the entered number is divisible by 2 using the modulo operator
(%).
If the remainder is 0, it prints that the number is even; otherwise, it prints that
the number is odd.
Output:
Enter a number: 10
10 is an even number.
Enter a number: 11
11 is an odd number.
In this program:
The check_odd_even function takes a number as an argument and uses the modulo operator
(%) to check if the number is divisible by 2.
If the remainder is 0, it prints that the number is even; otherwise, it prints that
the number is odd.
The user is prompted to input a number, and the program calls the check_odd_even
function to determine and print whether the entered number is odd or even.
<!DOCTYPE html>
<html>
<head><title>Even Odd</title></head>
<body bgcolor="green">
<h1> Program to check number is even or odd </h1>
<script type="text/Javascript">
var a,b;
a=prompt("Enter your value:-");
b=parseInt(a) ;
// input is converted into number data type
if(b%2==0)
alert("Number is even");
else
alert("Number is odd");
</script>
</body>
</html>
Output:
OR
<!DOCTYPE html>
<html>
<head>
<title>Odd or Even Checker</title>
</head>
<body bgcolor=yellow>
<h1>Odd or Even Checker</h1>
<p id="result"></p>
<script>
function checkOddEven()
{
var num = parseInt(document.getElementById('number').value);
if (num % 2 === 0)
{
document.getElementById('result').innerText = num + " is an even number.";
}
else
{
document.getElementById('result').innerText = num + " is an odd number.";
}
}
</script>
</body>
</html>
Output: