What Are Variables in Python? Unlocking Essential Insights for a Bright 2024

what are variables in python

Let’s discuss what are variables in python and their types ?

  • We know that python is completely object oriented and hence we do not need to declare variable name or their type before using them.
  • Every variable in python is an object.
  • In python, an assignment statement creates new variable and assign value.
  • Variable name may consists of alphabets, digits and underscore( _ ).
  • Note that, variable names are case-sensitive for e.g. Salary, salary, SALARY are three different variables.
  • It must start with alphabet and underscore can be used to connect multiple words in the variable name.
  • Programmer should select meaningful variable names so that it must reflect about its purpose.
  • Python programming language does not support declaring variables. They are just containers for storing values.
  • It is created when user has some value to assign to it.
  • Python has two basic types of variablesNumbers and String.

What Are Variables in Python?

1. Numbers

  • Python supports three types of numbers:
    1) Integer or fixed point numbers
    2) Reals or Floating point numbers
    3) Complex numbers


    What Are Variables in Python? – Example of Numeric Variable
a = 10  # integer
b = 20
c = a + b
c

Output: 30

price = 25.50  # float
qty = 7        # int
amount = price * qty
amount

Output: 178.5

x=5
z=x+y             #error-variable y is not defined

Output:
Traceback(most recent call last):
File"<pyshell#54>",line 1, in <module>
z=x+y
NameError:name 'y' is not defined

marks = 259
out_of = 300
percentage = marks * 100 / out_of
print("percentage =", percentage)

Output:
Percentage = 86.3333333333333333

What Are Variables in Python?

Complex Numbers

  • Complex numbers are written in x+yj format where x is real part and y is imaginary part. (j2=1)

What Are Variables in Python? Example of Complex Numbers

c1 = 2 + 3j
c2 = 5 - 2j
c3 = c1 + c2
print(c3)

Output:
(7+1j)

c1 = 2 + 3j
c2 = 5 - 2j
c3 = c1 + c2
print(c1*c2)

Output:
(16+11j)


Product of complex numbers in above example

c1 = 2 + 3j
c2 = 5 - 2j
result = c1 - c2
print(result)

Output: (-3+5j)

What Are Variables in Python?

2. Strings

  • String literal is a value that consists of alphabets, digits and special symbols also.
  • String can be enclosed in single quotes (‘…’) or double quotes(“…”).
  • Backslash(\) can be used to escape quotes and interpret following character as a special character.
  • Example:
    ‘Dont worry’ —– correct
    ‘Don’t worry —- wrong
    “Don’t worry” —- correct
    “‘Don\’t worry”’ —- correct
  • String is usually enclosed in single quote but if it contains a single quote and no double quotes then can be enclosed in double quote.
  • Use + to concatenate string variables and/or literals.
  • If we don’t want \(slash) to interpret special character then while printing, can include r before the first quote.

What Are Variables in Python? Example of String Variable & Constants

print('I said,\'All is well\'')

Output: I said,'All is well'

good='Good'
day="Day"
print(good+""+day)

Output: Good Day

'Py''thon'

Output:'Python'

print("I said,\"All is well\"")

Output:I said,"All is well"

good="Good"
day='''Day'''
print(good+""+day)

Output: Good Day

s = '''This is a multi-line
string. And hence in triple quotation'''

Type Conversion

  • It is process of converting value of one data type to another compatible data type.
  • As per google, In computer science, type conversion, type casting, type coercion and type juggling are different ways of changing an expression from one data type to another.
  • Python support both type of type conversion:
    1) Implicit type conversion
    2) Explicit type conversion

Implicit Type Conversion

  • This is done mechanically and implicitly by python.
  • While converting, it is necessary to take care to avoid data loss and hence python promotes conversion from lower data type to higher data type i.e. from integer to float.

Program 1: Implicit Type Conversion

a=5       #integer
b=1.234   #float
c=a+b
print(a+b)
print("Datatype of c is ",type(c))
Output:
6.234
Datatype of c is <class'float'>

Explicit Type Conversion

  • We know that python is object oriented.
  • Hence python allows user to convert data type of an object to required data type.
  • Python supports explicit type conversion using predefined function int(), float(), str() etc.
  • Explicit type conversion is also called as Type Casting.

Program 2: Explicit Type Conversion

a=125             #integer
b=254.25          #float
c="23"            #string
d="135.69"        #string
sum1=a+int(c)     #int+int(string)
print(sum1)
sum2=b+float(d)   #float+float(string)
print(sum2)
Output:
148
389.94


Program 3: Explicit Type Conversion

s1=11     #integer
s2="22"   #string
s3=str(s1)+s2
print(s3)
print("Type of result is", type(s3))
Output:
1122
Type of result is <class'str'>

2 thoughts on “What Are Variables in Python? Unlocking Essential Insights for a Bright 2024

Leave a Reply

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