Table of Contents hide
Example : How to store Static/Constant values and display them in Python?
x = 10
y = "India"

print("The output is = ", x)
print('The output is = ', y)

Output:
The output is =  10
The output is =  India

-----------  OR  ------------

a = 10
A = "Python is case sensitive"

print("The output is = ", a)
print('The output is = ', A)

Output:
The output is =  10
The output is =  Python is case sensitive

-----------  OR  ------------

x = "India"
y = "is"
z = "Great"
print(x, y, z)
print(x + y + z)

Output:
India is Great
IndiaisGreat

-----------  OR  ------------

x, y, z = "India", "is", "Great"   # Multiple variable declaration
print(x, y, z)

Output:
India is Great

-----------  OR  ------------

x = y = z = "India is Great"    # Single assignment into Multiple variables
print(x, y, z)

India is Great
India is Great
India is Great

-----------  OR  ------------

x = 10
y = 20
z = x + y
print("The addition result is = ", z)
print('The addition result is = ', z)
print(f"The addition result is =  {z}")

Output:
The addition result is =  30
The addition result is =  30
The addition result is =  30

-----------  OR  ------------

x = 10.52
y = 20.32
z = x + y
print("The addition result is = ", z)

Output:
  The addition result is =  30.84

-----------  OR  ------------

x = 10.52
y = 20
z = x + y
print("The addition result is = ", z)

Output:
  The addition result is =  30.52
Example : How do you convert tempreture from Celsius into Fahrenheit and display them in Python?
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)

Output:
Enter temperature in Celsius: 302
Temperature in Fahrenheit: 575.6
Example : How to store Dynamic/User Input Values and display them in Python?
x = input()
y = input()
z = int(x)+int(y)
m = float(x)+float(y)
n= str(x)+str(y)

print("The addition result is = ", z)
print(f"The addition result is =  {z}")
print("The addition result is = ", m)
print("The addition result is = ", n)

Output:
10
20
The addition result is =  30
The addition result is =  30
The addition result is =  30.0
The addition result is =  1020

-----------  OR  ------------

x = input("Enter first Integer value = ")
y = input("Enter second Integer value = ")
z = int(x)+int(y)

print("The addition result is = ", z)
print(f"The addition result is =  {z}")

Output:
Enter first Integer value = 20
Enter second Integer value = 30

The addition result is =  50
The addition result is =  50
Example : A Python program of Simple Calculator.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

print("Sum output is :", num1 + num2)
print("Difference output is :", num1 - num2)
print("Product output is :", num1 * num2)
print("Division output is :", num1 / num2)

Output:

Enter first number: 50
Enter second number: 10
Sum output is : 60.0
Difference output is : 40.0
Product output is : 500.0
Division output is : 5.0
Example : How do you calculate the Simple Interest of Dynamic/User Input Values and display them in Python?
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time (years): "))

interest = (principal * rate * time) / 100
print("Simple Interest:", interest)
Example : How to find out the Square Root of static/dynamic values and display them in Python?
import math

number = 25
result = math.sqrt(number)

print("Square root of", number, "is:", result)

Output:
Square root of 25 is: 5.0

----------------  OR  -----------------

import math
num = float(input("Enter a number: "))
sqrt = math.sqrt(num)
print("Square root of", num, "is:", sqrt)

----------------  OR  -----------------

number = 16
result = number ** 0.5
print("Square root of", number, "is:", result)  # raising the number to the power of 0.5.

Output:
Square root of 25 is: 5.0

----------------  OR  -----------------

number = 16
result = pow(number, 0.5)
print("Square root of", number, "is:", result)

Output:
Square root of 25 is: 5.0

NB: The built-in pow() function can also be used to find the square root by passing 0.5 as the exponent.

----------------  OR  -----------------

import cmath

number = -16
result = cmath.sqrt(number)
print("Square root of", number, "is:", result)

Output:
Square root of -16 is: 4j

NB:
If we need to calculate the square root of a negative number, we can use the cmath module, which gives a result (as a complex number).
Example : How do you convert Lowercase letters into Uppercase letters and vice-versa and display them in Python?
text = input("Enter a string value : ")

# Convert to uppercase
upper_text = text.upper()
print("Uppercase form is :", upper_text)

# Convert to lowercase
lower_text = text.lower()
print("Lowercase form is :", lower_text)

Output:

Enter a string value : India
Uppercase form is : INDIA
Lowercase form is : india
Example : How do you convert Any letter of a Word/Sentence into its opposite form letters and vice-versa and display them in Python?
text = input("Enter a string/Sentence : ")

# Swap/Reverse the case
swapped_text = text.swapcase()
print("Swapped case:", swapped_text)

Output:

Enter a string/Sentence : WelCome India
Swapped case: wELcOME iNDIA
Example : How do you find out the GCD of user’s values in Python?
import math

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

gcd1 = math.gcd(num1, num2)
print("GCD of", num1, "and", num2, "is :", gcd1)

Output:
Enter first number: 10
Enter second number: 12
GCD of 10 and 12 is : 2
Example : How do you find out the LCM of user’s values in Python?
import math

def lcm(a, b):
    return abs(a * b) // math.gcd(a, b)

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("LCM of", num1, "and", num2, "is :", lcm(num1, num2))

Output:
Enter first number: 10
Enter second number: 20
LCM of 10 and 20 is : 20
Example : What are the Different Types of Division in Python?
x = 15
y = 2

print(x // y) #floor division(//) rounds the result down to the nearest whole number
print(x / y)  #simple division
print(x % y)  #remainder or modulo division

Output:
7
7.5
1
Example : How do we use Exponent/Power in Python?
x = 15
y = 2
print(x ** y)

Output:
225

------------  OR  ------------

number = 3
result = pow (number,3)
print("The power of", number, "is:", result)

Output:
The power of 3 is: 27
Example : How to display the Data Type Used by individual variables in Python?
x = 25
y = 20.35
z = "Hello India"

print(type(x))
print(type(y))
print(type(z))

Output:
<class 'int'>
<class 'float'>
<class 'str'>
Example : How to display the Implicit/Automatic DataType Conversion in Python?
x=40 # int object
y=50.5 # float object
z=x+y
print (z)

Output: 90.5

---------------  OR  ----------------

x=True;
y=40.5;
z=x+y;
print (z); # Here, True is equal to 1, and False is equal to 0.

Output : 41.5 
  
---------------  OR  ----------------

x=True
y=50.5
z=x+y
print (z) # Here, True is equal to 1, and False is equal to 0.

Output : 41.5    
Example : How to display the Explicit DataType Conversion in Python?
x = str(10) # In this case, x will be '10' i.e., String form.
print (x)
y = int(10) # In this case, y will be 10 i.e., Integer form.
print (y)
z = float(10) # In this case, z will be 10.0 i.e., Float form.
print (z)

Output: 
10
10
10.0

---------------  OR  ----------------

x = int("1001", 2) # convert binary into decimal/integer.
y = int("37", 8) # convert octal into decimal/integer.
z = int("9A3", 16) # convert hexadecimal into decimal/integer.
print(x,y,z)

Output : 9 31 2467
Example : How to store Collections Values into Individual Variables in Python?
animals = ["Tiger", "Lion", "Cat", "Dog"]
a, b, c, d = animals
print(a)
print(b)
print(c)
print(d)

Output:
Tiger
Lion
Cat
Dog
Example : How to find out the ASCII value of Characters/Symbols in Python?
char = input("Enter a character/symbol: ")
print("ASCII value of", char, "is:", ord(char))

Output:
Enter a character: $
ASCII value of $ is: 36
Example : How to find out Characters/Symbols from the ASCII value in Python?
# Get ASCII value from the user
ascii_value = int(input("Enter an ASCII value (0-127): "))

# Convert ASCII value to character
character = chr(ascii_value)

# Display the result
print("The character for ASCII value", ascii_value, "is:", character)

Output:

Enter an ASCII value (0-127): 110
The character for ASCII value 110 is: n
Example : How to Remove/Delete Python variables logically from the program?
num = 500
print (num)

del num
print (num)

Output:

500
Traceback (most recent call last):
  File "d:\Untitled-2.py", line 5, in <module>
    print (num)
           ^^^
NameError: name 'num' is not defined. 

Loading

Categories: Python Theory

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.