Value Assignment in Python

pyaplogo

Value assignment in Python refers to the process of assigning a value to a variable using the equals (=) operator. The value can be a number, string, or any other data type. Once assigned, the variable can be used in expressions, statements, or functions to perform various operations in the program.

Let's take a look at how python assigns values to a variable, We understood the fact that we can have multiple assignments in a single line, so let’s have a look at how it is done.

#assign variables
counter = 100
miles = 1000.0
name = "John"

#to print the variables
print("variable integer :", counter)
print("variable float :", miles)
print("variable string :", name)

Also, we said that python is contextually sensitive, we don’t need to specify a data type upfront. Python will assign data types and store them appropriately. Python assigns 100 to the counter, it is an integer so we will show the integer later. Miles will be 1000.0 which means it stores float a real number and the name is given a value john it takes as a string variable. Now remember that in python we don’t have a data type, it is taken as a string variable, and as a string of length on as we mention, you can print the variable.

Also, we talk about assigning values in multiple variables in the same statement, nowhere do all the variables have the same value, we have var1, var2, and var3 all assigned a value of 90. Let us print each variable and see that each would be 90. It's not necessary that you have to have the same value in all cases, We can assign different types of values and different types of different variables in the same statement, now when we do it var1 has the value 100var2 has 92.75var3 has a value john, three different values with three different variables.var3 is a string variablevar2 is a real float number and var1 is a number.so let us run and see the output.
#Multiple Assignment applicable
#Assign
var1 = var2 = var3 = 90

#To check the variable value
print("var1 :", var1)
print("var2 :", var2)
print("var3 :", var3)

#Assign
var1, var2, var3 = 100, 92.75, "john"

#To check the variable value
print("var1 :", var1)
print("var2 :", var2)
print("var3 :", var3)

We have quite a lot of functions available let's go and import the math module here so that we can able to perform various math functions
# Methods with numbers

# To import the module
import math

# to print absolute value
print("abs(-45) :", abs(-45))

# to print smallest interger not less than -45.17
print("math.ceil(-45.17) :", math.ceil(-45.17))

# to print exponential of - 45.17
print("math.exp(-45.17) :", math.exp(-45.170))

# to print absolute value
print("math.fabs(-45.17) :", math.fabs(-45.17))

# to print largest integer not greater than -45.17
print("math.floor(-45.17) :", math.floor(-45.17))

# to print largest of its arguments
print("max(80, 100, 1000) :", max(80, 100, 1000))

# to print smallest of its arguments
print("min(80, 100, 1000) :", min(80, 100, 1000))
# to print value of 100 power of 2
print("math.pow(100, 2) :", math.pow(100, 2))

# to print rounded to 3 digits
print("round(80.234567, 3) :", round(80.234567, 3))

# to print square root
print("math.sqrt(100) :", math.sqrt(100))
Next Post Previous Post