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 to Remove/Delete Python variables?
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.
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 : 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 : 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 in Python?
x = 15
y = 2
print(x ** y)
Output:
225
0 Comments