Python Variable types

When you hear the word variable, you understand it to be an object or character that represents a value. These values can be numbers (0-9), alphabets (Aa-Zz), or alphanumerics (R5cY). In mathematics, an equal sign(=) is used to show the value of a variable, like this b=2+4. In Python Language programming, the computer needs to store code values which afterward will be located and interpreted to effect its purpose. The container/memory that stores or represents these values for readability is called a variable. In this way, a variable represents string literals or statements in Coding.

Example of Python Variable types:

gender = ‘F’

message = ”best friend ever”

In the examples above, gender and message are the variables, ‘F’ and “best friend ever” are the values of the variables

Note: there’s no command prompt for variable execution in python, it’s self executes once you typed in the variable symbol (=)

Uses Of Variables

The use of variables in python is straight to point and that is to reserve memory for values to be stored. And it’s used to represent string literals; a string is a list of characters that makes up a code line.

Types of Variables in Python

The Python Language has three variables, which are:

  • int(whole numbers)
  • Float(decimals)
  • Complex(complex numbers)

int(whole numbers)

int is short for integers. Integers refer to a whole number that is not in decimal. Progressively, int(integer) in Python means values in whole numbers such as 1, 20, 40000, 52673637827, etc. integers inputting has no limit in Python except that all computers have memory limits. You convert a value to integers by using the int() function

Example:

Input:

# integer 
print("int(123) is:", int(123))   
# float 
print("int(123.23) is:", int(123.23))   
# string 
print("int('123') is:", int('123'))

Output:

int(123) is: 123 
int(123.23) is: 123 
int('123') is: 123

Also, you can convert an object to an object integer by overriding _index_() and _int_() methods of the class to return a number. Both functions should produce the same results.

Example:

Input

class Person:
    age = 23
    def __index__(self):
        return self.age
    def __int__(self):
        return self.age
person = Person()
print(‘int(person) is:’, int(person))

Output:

int(person) is: 23

Float(decimals)

At this point it’s self-explanatory what the Float variable stands for, however, to clear your doubt. Float is a variable whose values are in decimals. It’s used to convert a string of numbers to a floating number or decimal number. Examples of floats in Python are 97.98, 32.3+e18, -and 32.54e100. The maximum amount of float in python is approximately 1.8 x 10308. Anything beyond this will be read as inf (indefinite) in Python. You affect float in python with the float() function.

How does Float Work In Python?

Example-1:

Input:

# for integers
print(float(10))  
# for floats
print(float(11.22))  
# for string floats
print(float(“-13.33”))  
# for string floats with whitespace
print(float(”     -24.45\n”))  
# string float error
print(float(“ABC”))

Output:

  10.0
11.22
-13.33
-24.45
ValueError: could not convert string to float:
‘ABC’

You can float with positive (+) numbers in Python using the float () function

Example-2:

Input:

“”” Desc: Python example to demonstrate float() function “””  
# Test Input
text input = [0, 1, 10000, 0.0, 1.1001, 1.000000000000001, 1.0000000000000001, 1.0000]   for each item in test input:
   print(“float({}) = {}”.format(each item, float(eachItem)))

Output:

float(0) = 0.0
float(1) = 1.0
float(10000) = 10000.0
float(0.0) = 0.0
float(1.1001) = 1.1001
float(1.000000000000001) = 1.000000000000001
float(1.0) = 1.0
float(1.0) = 1.0
float(1.0) = 1.0

You can as well float with negative (-ve) numbers using the float() function

Example-3:

Input:

“” Desc: Python example to demonstrate float() function on -ve numbers “””  
# Test Input
text input = [-1, -10000, -0.0, -1.1001, -1.000000000000001, -1.0000000000000001, -1.0000]   for each item in test input:
   print(“float({}) = {}”.format(each item, float(eachItem)))

Output:

float(-1) = -1.0
float(-10000) = -10000.0
float(-0.0) = -0.0
float(-1.1001) = -1.1001
float(-1.000000000000001) = -1.000000000000001
float(-1.0) = -1.0
float(-1.0) = -1.0

Complex (complex):

Complex numbers in python are made up of two parts: the real part and the imagery part. The imagery part is represented by j or J  after the number. You can create a complex number by identifying the imagery part and the real part to a variable. Or we can use the in-built complex( ) function to convert both code values to a complex number.

Example of how to convert to complex numbers methods 1 & 2

Method 1: Assigning the real and imaginary parts of the integers to a variable

Input:

a = 8 + 5j
print(type(a))

Output:

<class ‘complex’>

Method 2: Using the in-built complex ( ) function to create complex output

Input:

a = 8 b = 5 c = complex(8,5) print(type(c))

Output:

<class ‘complex’>

Now you’ve got an idea of each variable type and how to affect it in python, it’s necessary you know how to assign values to variables in python

Assigning Values to Variables in Python

As stated earlier, to be able to assign values to Variables in Python accurately, you must understand its arrangement. The left operand is for the variables while the right operand is for the values assigned to the variables. There’s no command to affect a variable, just introduce the (=) and python will interpret it.

Example:

#!/usr/bin/python3  
counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = “Simon”       # A string  
print (counter)
print (miles)
print (name)

This means that counter, miles, and name are the variables while 100, 1000.0, and “Simon” are the values assigned to the variables.

Output:

100
1000.0
Simon

Interestingly, you can assign a single value to multiple variables.

Example:

a = b = c = 1

Also, you can assign multiple objects to multiple variables on one line of code

Example:

a, b, c = 1, 2, “Simon”

Summary

You’ve just had a head start on the three variables of Python Language which are: integers (int) Float and complex. The python variables are referred to as envelopes or buckets that store bits of statements or values. Generally speaking, python Variables are used to identify a value in a code line. it’s of essence that you have an experience of what you’ve been taught by practicing now that you’ve been armed with the Python variables, you’re fit to learn the Python Numbers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment